Histogram height

In python matplotlib, the hist() returns an array containing the bin heights of the histogram. How can I do this in julia? as far as i know, the corresponding histogram() function doesn’t return anything. What is the quickest way to get the bin heights of a normalised histogram?

Hi there!

I would recommend the StatsBase.jl package:

https://juliastats.org/StatsBase.jl/stable/empirical/

julia> using StatsBase, LinearAlgebra

julia> bins = [0,1,7]; # a small and a large bin

julia> obs = [0.5, 1.5, 1.5, 2.5]; # one observation in the small bin and three in the large

julia> h = fit(Histogram, obs, bins)
Histogram{Int64,1,Tuple{Array{Int64,1}}}
edges:
  [0, 1, 7]
weights: [1, 3]
closed: left
isdensity: false

julia> # observe isdensity = false and the weights field records the number of observations in each bin

julia> normalize(h, mode=:density)

normalize returns a new, normalized histogram objectz

1 Like