Plots histogram2d not displaying correct number of bins

Hi Guys :slightly_smiling_face:

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.

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

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

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

333

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 :pray:t5: :pray:t5: :+1:t3: :blush:

Thanks!

the nbins is approximate

Is there a way to make it definite?

give it a range may do it

2 Likes

Bingo!

Thank you @jling :+1:t5: