A.Yazdiha
A.Yazdiha

Reputation: 1378

How to plot StatsBase.Histogram object in Julia?

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

deg_hist = degree_histogram(g)

I want to make a plot of this but i am new to plotting in Julia. The object returned is a StatsBase.Histogram which has the following as its inner 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 I can make use of this object to plot the histogram?

Upvotes: 3

Views: 3540

Answers (2)

Tom Breloff
Tom Breloff

Reputation: 1802

I thought this was already implemented, but I just added the recipe to StatPlots. If you check out master, you'll be able to do:

julia> using StatPlots, LightGraphs

julia> g = Graph(100,200);

julia> plot(degree_histogram(g))

For reference, the associated recipe that I added to StatPlots:

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

Upvotes: 7

Alexander Morley
Alexander Morley

Reputation: 4181

Use the histogram fields .edges and .weights to plot it e.g.

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 could create/extend a plotting function adding a method like so:

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

Then you will be able to call your plotting functions directly on the histogram object.

Upvotes: 3

Related Questions