# How to extract the most populated bin from a histogram

**URL:** https://discourse.julialang.org/t/how-to-extract-the-most-populated-bin-from-a-histogram/44081
**Category:** General Usage
**Tags:** statistics
**Created:** [August 1, 2020, 10:33am UTC](https://discourse.julialang.org/t/how-to-extract-the-most-populated-bin-from-a-histogram/44081 "2020-08-01T10:33:10Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![Tofi](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tofi/32/11919_2.png) [@Tofi](https://discourse.julialang.org/u/Tofi)
#### Post date: [August 1, 2020, 10:33am UTC](https://discourse.julialang.org/t/how-to-extract-the-most-populated-bin-from-a-histogram/44081/1 "2020-08-01T10:33:11Z")

</div>

Hello,

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

```julia
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

---

<div class="post-metadata">

### Author: ![Tofi](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tofi/32/11919_2.png) [@Tofi](https://discourse.julialang.org/u/Tofi)
#### Post date: [August 1, 2020, 10:50am UTC](https://discourse.julialang.org/t/how-to-extract-the-most-populated-bin-from-a-histogram/44081/2 "2020-08-01T10:50:28Z")

</div>

OK, I found the solution:

```julia
# 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]

```

---

<div class="post-metadata">

### Author: ![Mattriks](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mattriks/32/351_2.png) [@Mattriks](https://discourse.julialang.org/u/Mattriks)
#### Post date: [August 1, 2020, 2:35pm UTC](https://discourse.julialang.org/t/how-to-extract-the-most-populated-bin-from-a-histogram/44081/3 "2020-08-01T14:35:12Z")

</div>

`?argmax`

---

<div class="post-metadata">

### Author: ![Tofi](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tofi/32/11919_2.png) [@Tofi](https://discourse.julialang.org/u/Tofi)
#### Post date: [August 2, 2020, 4:42am UTC](https://discourse.julialang.org/t/how-to-extract-the-most-populated-bin-from-a-histogram/44081/4 "2020-08-02T04:42:42Z")

</div>

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

```julia
i = argmax(hist.weights)

```
