How to build a StatsBase.Histogram object in Julia?

I use package ( LightGraphs.jl) in Julia, and it has a predefined histogram method that creates a degree distribution on the network g.

deg_hist = degree_histogram(g)

I want to plot this, but I'm new to the conspiracy in Julia. The returned object is StatsBase.Histogramone that has the following internal fields:

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

Can you help me, how can I use this object to build a histogram?

+3
source share
2 answers

Use the histogram fields .edges and .weights to build it, for example.

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])

or you can create / extend the charting function by adding this method:

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

.

+2

, , 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
+5

Source: https://habr.com/ru/post/1659824/


All Articles