# Histogram counts from Makie

**URL:** https://discourse.julialang.org/t/histogram-counts-from-makie/106323
**Category:** Visualization
**Tags:** makie
**Created:** [November 16, 2023, 3:50pm UTC](https://discourse.julialang.org/t/histogram-counts-from-makie/106323 "2023-11-16T15:50:36Z")
**Posts on this page:** 6
**Page:** 1

<div class="post-metadata">

### Author: ![panbroggi](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/panbroggi/32/24761_2.png) [@panbroggi](https://discourse.julialang.org/u/panbroggi)
#### Post date: [November 16, 2023, 3:50pm UTC](https://discourse.julialang.org/t/histogram-counts-from-makie/106323/1 "2023-11-16T15:50:36Z")

</div>

Hi everyone!

Do you know how I can recover the bin count from an histogram, e.g. the following?

```julia
hist(rand(100))

```

---

<div class="post-metadata">

### Author: ![fedoroff](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/fedoroff/32/53209_2.png) [@fedoroff](https://discourse.julialang.org/u/fedoroff)
#### Post date: [November 16, 2023, 4:54pm UTC](https://discourse.julialang.org/t/histogram-counts-from-makie/106323/2 "2023-11-16T16:54:07Z")

</div>

Something like this, I guess:

```julia
h = hist(rand(100))

bins = collect(h.plot[1][])

```

---

<div class="post-metadata">

### Author: ![jules](https://avatars.discourse-cdn.com/v4/letter/j/41988e/32.png) [@jules](https://discourse.julialang.org/u/jules)
#### Post date: [November 16, 2023, 4:56pm UTC](https://discourse.julialang.org/t/histogram-counts-from-makie/106323/3 "2023-11-16T16:56:55Z")

</div>

You can also fit a histogram with Statsbase first, and then plot that instead.

---

<div class="post-metadata">

### Author: ![Dan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dan/32/42581_2.png) [@Dan](https://discourse.julialang.org/u/Dan)
#### Post date: [November 16, 2023, 8:45pm UTC](https://discourse.julialang.org/t/histogram-counts-from-makie/106323/4 "2023-11-16T20:45:26Z")

</div>

As jules suggested, creating the Histogram using StatsBase is the best method, as this is what Makie does as well if not supplied with a Histogram. In any case, to be more explicit, and follow the way Makie creates the bin weights:

```julia
julia> plt = hist(rand(100))

julia> fit(StatsBase.Histogram, plt.plot[1][],
  Makie.pick_hist_edges(plt.plot[1][],plt.plot.bins[])).weights
15-element Vector{Int64}:
 10
  5
  7
  6
  9
  8
  5
  6
  9
  5
  4
  6
  7
  7
  6

```

---

<div class="post-metadata">

### Author: ![panbroggi](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/panbroggi/32/24761_2.png) [@panbroggi](https://discourse.julialang.org/u/panbroggi)
#### Post date: [November 17, 2023, 8:23am UTC](https://discourse.julialang.org/t/histogram-counts-from-makie/106323/5 "2023-11-17T08:23:22Z")

</div>

Thank you.  
From your replies, I understand that at the moment there is no simple way to plot the histogram and then get the bin counts without recomputing it. The only way to compute them only once is to compute the counts in advance and then produce a barplot. Is this right?

---

<div class="post-metadata">

### Author: ![mkborregaard](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mkborregaard/32/556_2.png) [@mkborregaard](https://discourse.julialang.org/u/mkborregaard)
#### Post date: [November 17, 2023, 8:46am UTC](https://discourse.julialang.org/t/histogram-counts-from-makie/106323/6 "2023-11-17T08:46:08Z")

</div>

@jules Looks like the histogram functionality (i.e. bin selection) of Makie could use a bit of love. We could to begin with maybe move the bin selection framework [https://github.com/JuliaPlots/Plots.jl/blob/42244b6aea99327acb07055d57bff2da41104555/src/recipes.jl#L719-L782](https://github.com/JuliaPlots/Plots.jl/blob/42244b6aea99327acb07055d57bff2da41104555/src/recipes.jl#L719-L782) from Plots to PlotUtils?  
Or we used to talk about moving it to StatsBase.
