# Plotly Histogram nbinx does not provide the right number of bins in Python

**URL:** <https://community.plotly.com/t/plotly-histogram-nbinx-does-not-provide-the-right-number-of-bins-in-python/1126>\
**Category:** 📊 Plotly Python\
**Created:** [May 25, 2016, 4:15pm UTC](https://community.plotly.com/t/plotly-histogram-nbinx-does-not-provide-the-right-number-of-bins-in-python/1126 "2016-05-25T16:15:39Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Antoine](https://avatars.discourse-cdn.com/v4/letter/a/7ab992/32.png) [@Antoine](https://community.plotly.com/u/Antoine)\
**Post date:** [May 25, 2016, 4:15pm UTC](https://community.plotly.com/t/plotly-histogram-nbinx-does-not-provide-the-right-number-of-bins-in-python/1126/1 "2016-05-25T16:15:39Z")

</div>

With the following code I get 15 bins instead of 20 as expected.

```
import pandas as pd
from plotly.offline import plot
from plotly.graph_objs import Histogram

df = pd.read_csv('https://raw.githubusercontent.com/AntoineGautier/Data/master/tmp.txt')

plot(dict(data=[Histogram(x=df.ws, nbinsx=20)],
          layout=dict(xaxis=dict(dtick=1), bargap=0.25)
         ))
```

---

<div class="post-metadata">

**Author:** ![molofishy](https://avatars.discourse-cdn.com/v4/letter/m/f6c823/32.png) [@molofishy](https://community.plotly.com/u/molofishy)\
**Post date:** [August 4, 2016, 9:52pm UTC](https://community.plotly.com/t/plotly-histogram-nbinx-does-not-provide-the-right-number-of-bins-in-python/1126/2 "2016-08-04T21:52:53Z")

</div>

I also noticed this. Did you manage to find a solution?

---

<div class="post-metadata">

**Author:** ![etienne](https://sea2.discourse-cdn.com/flex024/user_avatar/community.plotly.com/etienne/32/17_2.png) [@etienne](https://community.plotly.com/u/etienne)\
**Post date:** [August 8, 2016, 3:26pm UTC](https://community.plotly.com/t/plotly-histogram-nbinx-does-not-provide-the-right-number-of-bins-in-python/1126/3 "2016-08-08T15:26:54Z")

</div>

Setting `nbinx` specifies the maximum number of desired bins **not** the exact number of bins to show. WIth `nbinx` set, plotly still attempts to round the number of bins to _nice_ numbers. See the full attribute reference [here](https://plot.ly/python/reference/#histogram-nbinsx) for more info.

To set the exact number of bins, you’ll need to use a combination of `xbins.start`, `xbins.end` and `xbins.size`.

---

<div class="post-metadata">

**Author:** ![yoghurtshawn](https://avatars.discourse-cdn.com/v4/letter/y/848f3c/32.png) [@yoghurtshawn](https://community.plotly.com/u/yoghurtshawn)\
**Post date:** [August 5, 2020, 1:55pm UTC](https://community.plotly.com/t/plotly-histogram-nbinx-does-not-provide-the-right-number-of-bins-in-python/1126/4 "2020-08-05T13:55:28Z")

</div>

I tried to set the xbins.start, end, size, but no working. Do you have an example?

---

<div class="post-metadata">

**Author:** ![cerenaaa](https://avatars.discourse-cdn.com/v4/letter/c/ed8c4c/32.png) [@cerenaaa](https://community.plotly.com/u/cerenaaa)\
**Post date:** [August 9, 2020, 10:39pm UTC](https://community.plotly.com/t/plotly-histogram-nbinx-does-not-provide-the-right-number-of-bins-in-python/1126/5 "2020-08-09T22:39:18Z")

</div>

This finally worked for me:

fig = go.Figure()  
fig.add\_trace(go.Histogram(x=penetration.p\_member, name=‘population’, histnorm=‘percent’,  
xbins=dict(start=‘0’,end=‘0.4’, size=0.05)  
),

```
         )

```

fig.add\_trace(go.Histogram(x=df\_test88.toPandas().p\_member, name=‘sample\_88’, histnorm=‘percent’,  
xbins=dict(start=‘0’,end=‘0.4’, size=0.05)  
))  
fig.update\_layout(barmode=‘overlay’)

# Reduce opacity to see both histograms

fig.update\_traces(opacity=0.75)  
fig.show()

---

<div class="post-metadata">

**Author:** ![flores12](https://sea2.discourse-cdn.com/flex024/user_avatar/community.plotly.com/flores12/32/4257_2.png) [@flores12](https://community.plotly.com/u/flores12)\
**Post date:** [December 30, 2020, 9:53pm UTC](https://community.plotly.com/t/plotly-histogram-nbinx-does-not-provide-the-right-number-of-bins-in-python/1126/6 "2020-12-30T21:53:44Z")

</div>

Does anybody know more about how the algorithm finds the optimal bin sizes? I have a multi plot which I want to standardize the y-axis ranges to, which means that I need to be able to count the number of data within a bin.

---

<div class="post-metadata">

**Author:** ![HashRocketSyntax](https://sea2.discourse-cdn.com/flex024/user_avatar/community.plotly.com/hashrocketsyntax/32/8972_2.png) [@HashRocketSyntax](https://community.plotly.com/u/HashRocketSyntax)\
**Post date:** [August 15, 2024, 7:10pm UTC](https://community.plotly.com/t/plotly-histogram-nbinx-does-not-provide-the-right-number-of-bins-in-python/1126/7 "2024-08-15T19:10:09Z")

</div>

The automatic behavior that ignores nbins is _extremely_ misleading for overlapping distributions.

 ![image](https://us1.discourse-cdn.com/flex024/uploads/plot/original/3X/c/8/c86116a8d5a450c970156b7e28c54009195d6c91.png)

I feel like I ran into this previously and had to create a custom dataframe that binned things manually.

 ![image](https://us1.discourse-cdn.com/flex024/uploads/plot/original/3X/d/3/d32aeecaefe88f97139228e12a977a96ea482577.png)

The best workaround that I have found for this is to specify a higher number of bins than you actually want (e.g. if you want 7, then use a value of 9).
