# Plots histogram2d not displaying correct number of bins

**URL:** <https://discourse.julialang.org/t/plots-histogram2d-not-displaying-correct-number-of-bins/58328>\
**Category:** General Usage\
**Tags:** plotting, vscode, plotlyjs\
**Created:** [March 31, 2021, 8:13pm UTC](https://discourse.julialang.org/t/plots-histogram2d-not-displaying-correct-number-of-bins/58328 "2021-03-31T20:13:57Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![MattWillFlood](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mattwillflood/32/20722_2.png) [@MattWillFlood](https://discourse.julialang.org/u/MattWillFlood)\
**Post date:** [March 31, 2021, 8:13pm UTC](https://discourse.julialang.org/t/plots-histogram2d-not-displaying-correct-number-of-bins/58328/1 "2021-03-31T20:13:58Z")

</div>

Hi Guys 🙂

I’m trying to plot a 2D histogram using the plots function `histogram2D`.  
[backend: plotlyjs, environment: VSCode]

My aim is to plot the 2D histogram for different numbers of bins (\*same number of bins along each axis). With the following code, `histogram2d` works with `nbins=2` and `show_empty_bins=true`.

```julia
using Plots
using Plots.PlotMeasures

Sig = rand(1000)
x1 = Sig[1:end-1];
x2 = Sig[2:end];
p1 = plot(x1,x2,seriestype =:scatter,c=:magenta, 
        markerstrokecolor=:magenta, markersize = 2,
        legend=false, aspect_ratio=:equal)
p2 = histogram2d(x1,x2, nbins=2, aspect_ratio=:equal,
                   colorbar_entry=false, seriescolor=:cool,show_empty_bins=true)
xx = plot(p1,p2,layout=(1,2),legend=false, framestyle=:grid, margin=7mm)

```

![111](https://global.discourse-cdn.com/julialang/original/3X/0/8/0803d1c9cb4eef70acff721d9c9aded7d32787de.png)

The problem is that `histogram2d` returns the same 2x2 plot when _ **`nbins = 3 or 4`** _  
Only when _ **`nbins = 5`** _ does the plot return the correct bins.  
 ![222](https://global.discourse-cdn.com/julialang/original/3X/6/c/6ca8388caa95516b0d8462f7eef437d2647f4765.png)

After _ **`nbins = 5`** _, the plot doesn’t change again for _ **`nbins = 6:9`** _, but only until _ **`nbins = 10`** _

![333](https://global.discourse-cdn.com/julialang/original/3X/6/8/6859b19ae2d91d6771d771fb3ae24960212f1f8a.png)

I’ve tried using a Tuple for nbins and playing around with the formatting but nothing seems to work…  
Has anyone else come across this problem?  
Is there something I’ve overlooked?

Any help at all would be terrific 🙏🏾 🙏🏾 👍🏼 😊

Thanks!

---

<div class="post-metadata">

**Author:** ![jling](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jling/32/212909_2.png) [@jling](https://discourse.julialang.org/u/jling)\
**Post date:** [March 31, 2021, 8:16pm UTC](https://discourse.julialang.org/t/plots-histogram2d-not-displaying-correct-number-of-bins/58328/2 "2021-03-31T20:16:49Z")

</div>

the `nbins` is approximate

---

<div class="post-metadata">

**Author:** ![MattWillFlood](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mattwillflood/32/20722_2.png) [@MattWillFlood](https://discourse.julialang.org/u/MattWillFlood)\
**Post date:** [March 31, 2021, 8:18pm UTC](https://discourse.julialang.org/t/plots-histogram2d-not-displaying-correct-number-of-bins/58328/3 "2021-03-31T20:18:08Z")

</div>

Is there a way to make it definite?

---

<div class="post-metadata">

**Author:** ![jling](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jling/32/212909_2.png) [@jling](https://discourse.julialang.org/u/jling)\
**Post date:** [March 31, 2021, 8:22pm UTC](https://discourse.julialang.org/t/plots-histogram2d-not-displaying-correct-number-of-bins/58328/4 "2021-03-31T20:22:02Z")

</div>

give it a range may do it

---

<div class="post-metadata">

**Author:** ![MattWillFlood](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mattwillflood/32/20722_2.png) [@MattWillFlood](https://discourse.julialang.org/u/MattWillFlood)\
**Post date:** [March 31, 2021, 8:28pm UTC](https://discourse.julialang.org/t/plots-histogram2d-not-displaying-correct-number-of-bins/58328/5 "2021-03-31T20:28:36Z")

</div>

Bingo!

Thank you @jling 👍🏾
