Hi everyone!

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

```
hist(rand(100))
```

Hi everyone!

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

```
hist(rand(100))
```

Something like this, I guess:

```
h = hist(rand(100))
bins = collect(h.plot[1][])
```

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

1 Like

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

1 Like

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?

@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 from Plots to PlotUtils?

Or we used to talk about moving it to StatsBase.