2016-11-04 30 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包,我需要解决,但这似乎很方便。 –