How to extract the most populated bin from a histogram

Hello,

I am using the fit function from the StatsBase package in order to calculate the histogram for an array of numbers. For example:

l = rand(100)
hist = fit(Histogram, l)

Is there a way to extract the most populated bin in hist ( i.e the bin with largest number of data points)?

Thanks in advance

OK, I found the solution:

# get the height of the most populated bin:
bin_max = maximum(hist.weights)

# get the position of this bin in the weights list:
i = findfirst(x -> x==bin_max, hist.weights)

# get the values of the edges corresponding to this position
bin_pos = hist.edges[1][i:i+1]
3 Likes

?argmax

2 Likes

You’re right! The first two lines in my answer above can be replaced by

i = argmax(hist.weights)