2016-11-04 140 views
4

我在Julia使用了一個包(LightGraphs.jl),它有一個預定義的直方圖方法,可以創建網絡的度分佈g如何在Julia中繪製StatsBase.Histogram對象?

deg_hist = degree_histogram(g) 

我想做一個這個陰謀,但我是新的陰謀在朱莉婭。返回的對象是一個StatsBase.Histogram它具有以下作爲其內部字段:

StatsBase.Histogram{Int64,1,Tuple{FloatRange{Float64}}} 
edges: 0.0:500.0:6000.0 
weights: [79143,57,32,17,13,4,4,3,3,2,1,1] 
closed: right 

你能幫助我,我怎麼可以使用這個對象來繪製直方圖?

回答

3

使用直方圖字段.edges和.weights來繪製它。

using PyPlot, StatsBase 
a = rand(1000); # generate something to plot 
test_hist = fit(Histogram, a) 

# line plot 
plot(test_hist.edges[1][2:end], test_hist.weights) 
# bar plot 
bar(0:length(test_hist.weights)-1, test_hist.weights) 
xticks(0:length(test_hist.weights), test_hist.edges[1]) 

,或者你可以創建/擴展繪圖功能添加一個方法,像這樣:

function myplot(x::StatsBase.Histogram) 
... # your code here 
end 

然後你就可以直接調用柱狀圖對象上的繪圖功能。

6

我認爲這已經實施,但我只是將配方添加到StatPlots。如果你看看主人,你就能夠做到:

julia> using StatPlots, LightGraphs 

julia> g = Graph(100,200); 

julia> plot(degree_histogram(g)) 

僅供參考,相關的食譜,我加入到StatPlots:

@recipe function f(h::StatsBase.Histogram) 
    seriestype := :histogram 
    h.edges[1], h.weights 
end 
+0

謝謝,我有一個問題與StatPlots包,我需要解決,但這似乎很方便。 –