# Getting bins from Plots.jl histogram

**URL:** https://discourse.julialang.org/t/getting-bins-from-plots-jl-histogram/55452
**Category:** General Usage
**Tags:** plotting, statistics, plots
**Created:** [February 17, 2021, 11:03am UTC](https://discourse.julialang.org/t/getting-bins-from-plots-jl-histogram/55452 "2021-02-17T11:03:05Z")
**Posts on this page:** 14
**Page:** 1

<div class="post-metadata">

### Author: ![OvidiusCicero](https://avatars.discourse-cdn.com/v4/letter/o/73ab20/32.png) [@OvidiusCicero](https://discourse.julialang.org/u/OvidiusCicero)
#### Post date: [February 17, 2021, 11:03am UTC](https://discourse.julialang.org/t/getting-bins-from-plots-jl-histogram/55452/1 "2021-02-17T11:03:05Z")

</div>

I can make a histogram plot by

```julia
histogram(rand(1000))

```

Can I get the values of the resulting binning?

---

<div class="post-metadata">

### Author: ![rafael.guerra](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rafael.guerra/32/216610_2.png) [@rafael.guerra](https://discourse.julialang.org/u/rafael.guerra)
#### Post date: [February 17, 2021, 12:25pm UTC](https://discourse.julialang.org/t/getting-bins-from-plots-jl-histogram/55452/2 "2021-02-17T12:25:23Z")

</div>

Check [StatsBase.jl](https://juliastats.org/StatsBase.jl/stable/empirical/#Histograms-1):

```julia
using StatsBase, Plots
h = fit(Histogram, rand(100), nbins=10)
plot(h)

```

Then access bin values with `h.weights`:

```julia
julia> h
Histogram{Int64, 1, Tuple{StepRangeLen{Float64, Base.TwicePrecision{Float64}, Base.TwicePrecision{Float64}}}}
edges:
  0.0:0.1:1.0
weights: [7, 9, 9, 15, 12, 6, 10, 18, 6, 8]
closed: left
isdensity: false

```

---

<div class="post-metadata">

### Author: ![nilshg](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nilshg/32/2283_2.png) [@nilshg](https://discourse.julialang.org/u/nilshg)
#### Post date: [February 17, 2021, 12:45pm UTC](https://discourse.julialang.org/t/getting-bins-from-plots-jl-histogram/55452/3 "2021-02-17T12:45:49Z")

</div>

I recall from a So question I answered some time ago that the book construction is different in plots and StatsBase ([histogram2d - Return the frequency in a bin of a 2D histogram in Julia - Stack Overflow](https://stackoverflow.com/questions/58336730/return-the-frequency-in-a-bin-of-a-2d-histogram-in-julia)) - not sure that was ever aligned

---

<div class="post-metadata">

### Author: ![rafael.guerra](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rafael.guerra/32/216610_2.png) [@rafael.guerra](https://discourse.julialang.org/u/rafael.guerra)
#### Post date: [February 17, 2021, 1:06pm UTC](https://discourse.julialang.org/t/getting-bins-from-plots-jl-histogram/55452/4 "2021-02-17T13:06:53Z")

</div>

@nilshg, thanks for the link. For 2D histograms, the StatsBase functionality seems to be more limited, with no user control on the 2d bins (at least it is not documented).

```julia
# computing 2D histograms
data = (randn(10_000), randn(10_000))
h = fit(Histogram, data, nbins=40) # how to finely control on size of 2d-bins ??
y = diff(h.edges[1])/2 .+ h.edges[1][1:end-1]
x = diff(h.edges[2])/2 .+ h.edges[2][1:end-1]
heatmap(x, y, h.weights)

```

---

<div class="post-metadata">

### Author: ![OvidiusCicero](https://avatars.discourse-cdn.com/v4/letter/o/73ab20/32.png) [@OvidiusCicero](https://discourse.julialang.org/u/OvidiusCicero)
#### Post date: [February 17, 2021, 1:17pm UTC](https://discourse.julialang.org/t/getting-bins-from-plots-jl-histogram/55452/5 "2021-02-17T13:17:32Z")

</div>

yes, they don’t do the same all the time ☹

---

<div class="post-metadata">

### Author: ![rafael.guerra](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rafael.guerra/32/216610_2.png) [@rafael.guerra](https://discourse.julialang.org/u/rafael.guerra)
#### Post date: [February 17, 2021, 1:23pm UTC](https://discourse.julialang.org/t/getting-bins-from-plots-jl-histogram/55452/6 "2021-02-17T13:23:45Z")

</div>

@OvidiusCicero, please provide an example for 1D where problem arises, it will be useful.

---

<div class="post-metadata">

### Author: ![OvidiusCicero](https://avatars.discourse-cdn.com/v4/letter/o/73ab20/32.png) [@OvidiusCicero](https://discourse.julialang.org/u/OvidiusCicero)
#### Post date: [February 17, 2021, 1:50pm UTC](https://discourse.julialang.org/t/getting-bins-from-plots-jl-histogram/55452/7 "2021-02-17T13:50:49Z")

</div>

It’s difficult to provide the simulation data but I need to manually set `nbins` to get the same number of bins (this makes a pipeline more difficult)

at the moment I plot the result of `fit` with bar

---

<div class="post-metadata">

### Author: ![rafael.guerra](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rafael.guerra/32/216610_2.png) [@rafael.guerra](https://discourse.julialang.org/u/rafael.guerra)
#### Post date: [February 17, 2021, 1:59pm UTC](https://discourse.julialang.org/t/getting-bins-from-plots-jl-histogram/55452/8 "2021-02-17T13:59:35Z")

</div>

I could not find obvious problems but did not try hard enough.  
See example of perfect overlap below:

```julia
using StatsBase, Plots
data1d = rand(100)
histogram(data1d, bins=10, label="histogram plot", legend=:topleft, ylims=(0,20))
h = fit(Histogram, data1d, nbins=10)
plot!(h, seriestype=:steps, lw=3, lc=:blue, label="StatsBase histogram")
savefig("histogram2d_vs_StatsBase_histogram.png")

```

![histogram_plot_vs_StatsBase_histogram](https://global.discourse-cdn.com/julialang/original/3X/1/3/13e73a4b5fd9989ab7532c47f91228d48cc56fe4.png)

---

<div class="post-metadata">

### Author: ![joa-quim](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/joa-quim/32/227_2.png) [@joa-quim](https://discourse.julialang.org/u/joa-quim)
#### Post date: [February 17, 2021, 2:47pm UTC](https://discourse.julialang.org/t/getting-bins-from-plots-jl-histogram/55452/9 "2021-02-17T14:47:32Z")

</div>

You can get 2D histograms with GMT

```julia
using GMT

# Compute a grid with counting's
G = blockmean(rand(100,2) * 100, region=(0,100,0,100), inc=10, npts=:n, grid=true);
# Plot it
bar3(G, fill=[0,115,190], lw=0.25, fmt=:png, show=true)
# Convert to x,y,z. Empty cells would have NaNs, the *skip_NaN* option takes care of it
D = grd2xyz(G, skip_NaN=true));
# Data is in the *data* field
D[1].data
65×3 Matrix{Float64}:
  20.0 100.0 1.0
  30.0 100.0 1.0
  50.0 100.0 1.0
  70.0 100.0 1.0
  90.0 100.0 1.0
   0.0 90.0 2.0
...

```

 ![GMTjl_tmp](https://global.discourse-cdn.com/julialang/original/3X/e/5/e58b69bc1709fae4570e0d7dba0f3d2c28c9a793.png)

---

<div class="post-metadata">

### Author: ![joa-quim](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/joa-quim/32/227_2.png) [@joa-quim](https://discourse.julialang.org/u/joa-quim)
#### Post date: [February 17, 2021, 2:59pm UTC](https://discourse.julialang.org/t/getting-bins-from-plots-jl-histogram/55452/10 "2021-02-17T14:59:49Z")

</div>

Sorry, something is not right. It’s not giving the countings.

EDIT; Now it is but I have to see why the `npts=true` alone had not worked as it should.

---

<div class="post-metadata">

### Author: ![OvidiusCicero](https://avatars.discourse-cdn.com/v4/letter/o/73ab20/32.png) [@OvidiusCicero](https://discourse.julialang.org/u/OvidiusCicero)
#### Post date: [February 17, 2021, 9:27pm UTC](https://discourse.julialang.org/t/getting-bins-from-plots-jl-histogram/55452/11 "2021-02-17T21:27:50Z")

</div>

![grafik](https://global.discourse-cdn.com/julialang/original/3X/8/7/8781b2c12374bf91cd8be960866005b717c7c4ff.png)

I can’t make a minimal working example out of it but the default binning of Ploots.jl is different from StatsBase so you need to explicitly select `nbins` and `bins` respectively to get the same results.

Otherwise, the selected solution works

---

<div class="post-metadata">

### Author: ![mgiugliano](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mgiugliano/32/5818_2.png) [@mgiugliano](https://discourse.julialang.org/u/mgiugliano)
#### Post date: [November 29, 2021, 9:18am UTC](https://discourse.julialang.org/t/getting-bins-from-plots-jl-histogram/55452/12 "2021-11-29T09:18:12Z")

</div>

Thank you. Is it however usual in Julia that the returned data structures are so “bloated”?

In order to get to the numerical values of the bins, I had to resort to type

```julia
collect(h.edges[1])

```

after discovering that h.edges is a 1-element Tuple, containing a range. And yet, it is not even the same length of h.edges ☹

---

<div class="post-metadata">

### Author: ![rafael.guerra](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rafael.guerra/32/216610_2.png) [@rafael.guerra](https://discourse.julialang.org/u/rafael.guerra)
#### Post date: [November 29, 2021, 9:40am UTC](https://discourse.julialang.org/t/getting-bins-from-plots-jl-histogram/55452/13 "2021-11-29T09:40:47Z")

</div>

@mgiugliano, the developpers of StatsBase will have the correct answer to your question.  
As an user, I can only remark that from the source code annotation: _edges are an iterator that contains the boundaries of the bins in each dimension_, which for the 1D case corresponds to objects like (example): `(0.0:0.1:1.0,)`. But do not know why it has been defined this way.

---

<div class="post-metadata">

### Author: ![dfc123](https://avatars.discourse-cdn.com/v4/letter/d/87869e/32.png) [@dfc123](https://discourse.julialang.org/u/dfc123)
#### Post date: [January 7, 2022, 4:57pm UTC](https://discourse.julialang.org/t/getting-bins-from-plots-jl-histogram/55452/14 "2022-01-07T16:57:02Z")

</div>

I don’t why it was defined that way either, but in contrast to “bins,” which appears to be treated as a suggestion, inputting edges (as a “StepRangeLen” tuple in the StatsBase Histogram “fit”) fixes the bin size, which is a nice feature, and it can also be used in two dimensions, which you mentioned n the post, above, from 3 Feb 2021, e.g.,

```julia
firstEdge = 0.0
lastEdge = 10.0
binSize = 1.0
EdgeRange = (firstEdge:binSize:lastEdge)
h = fit(Histogram, (x, y), weights(W), (EdgeRange, EdgeRange))

```

Did you find that if you first plot just h directly, “plot (h)”, and compare to using h.weights an input to heatmap (again the 3 Feb post), x and y are flipped? Seems crazy, but I had to “transpose(h.weights)” to get the same orientation in the heatmap. Am I missing something? I’m using Julia 1.7.0

```julia

```
