# Histogram Bin Size with Plotly Express

**URL:** <https://community.plotly.com/t/histogram-bin-size-with-plotly-express/38927>\
**Category:** 📊 Plotly Python\
**Created:** [May 5, 2020, 12:36pm UTC](https://community.plotly.com/t/histogram-bin-size-with-plotly-express/38927 "2020-05-05T12:36:27Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![vcmorini](https://avatars.discourse-cdn.com/v4/letter/v/5f9b8f/32.png) [@vcmorini](https://community.plotly.com/u/vcmorini)\
**Post date:** [May 5, 2020, 12:36pm UTC](https://community.plotly.com/t/histogram-bin-size-with-plotly-express/38927/1 "2020-05-05T12:36:28Z")

</div>

Plotly Express Histogram is very cool because it gives us the possibility to easily set the facets, change the number of bins and nicely merges with Dash, **but I would like to define the bin size**.  
I’ve seen this feature in go.Histogram() but not in px.Histogram  
Has anyone found a workaround?  
Cheers!  
Ref: [https://plotly.com/python/plotly-express/](https://plotly.com/python/plotly-express/)

> **[Histograms](https://plotly.com/python/histograms/)**
>
> How to make Histograms in Python with Plotly.

---

<div class="post-metadata">

**Author:** ![vcmorini](https://avatars.discourse-cdn.com/v4/letter/v/5f9b8f/32.png) [@vcmorini](https://community.plotly.com/u/vcmorini)\
**Post date:** [May 5, 2020, 1:01pm UTC](https://community.plotly.com/t/histogram-bin-size-with-plotly-express/38927/2 "2020-05-05T13:01:14Z")

</div>

I’ve just figured out a workaround that worked:

```auto
            
            bin_width= 50
            # here you can choose your rounding method, I've chosen math.ceil
            nbins = math.ceil((df["data"].max() - df["data"].min()) / bin_width)
            fig = px.histogram(df, x="data", nbins=nbins)

```

---

<div class="post-metadata">

**Author:** ![joseph.damiba](https://avatars.discourse-cdn.com/v4/letter/j/2acd7d/32.png) [@joseph.damiba](https://community.plotly.com/u/joseph.damiba)\
**Post date:** [May 5, 2020, 1:02pm UTC](https://community.plotly.com/t/histogram-bin-size-with-plotly-express/38927/3 "2020-05-05T13:02:27Z")

</div>

Thanks for using the forum!

I think that what you are looking for is the fact that `px.histogram()` accepts an `nbins` parameter which allows you to set the size of the bins in your histogram.

For example:

```python
import plotly.express as px
df = px.data.tips()
fig = px.histogram(df, x="total_bill", nbins=20)
fig.show()

```

 ![Screen Shot 2020-05-05 at 9.00.39 AM](https://us1.discourse-cdn.com/flex024/uploads/plot/original/2X/1/16f443f74af4d6dc9d2b4404d3e6b0b7a63b4207.png)

This is documented at [https://plotly.com/python/histograms/#choosing-the-number-of-bins](https://plotly.com/python/histograms/#choosing-the-number-of-bins)

I hope this answers your question!

---

<div class="post-metadata">

**Author:** ![vcmorini](https://avatars.discourse-cdn.com/v4/letter/v/5f9b8f/32.png) [@vcmorini](https://community.plotly.com/u/vcmorini)\
**Post date:** [May 5, 2020, 1:08pm UTC](https://community.plotly.com/t/histogram-bin-size-with-plotly-express/38927/4 "2020-05-05T13:08:27Z")

</div>

Hi and thank you,  
the question was related with the size of the bins instead of number of bins.  
I was looking for a feature similar to **xbins size** in go.Histogram but in Plotly Express

```auto
go.Histogram(
    x=x0,
    histnorm='percent',
    name='control', # name used in legend and hover labels
    xbins=dict( # bins used for histogram
        start=-4.0,
        end=3.0,
        size=0.5
    ),
    marker_color='#EB89B5',
    opacity=0.75
)

```

---

<div class="post-metadata">

**Author:** ![joseph.damiba](https://avatars.discourse-cdn.com/v4/letter/j/2acd7d/32.png) [@joseph.damiba](https://community.plotly.com/u/joseph.damiba)\
**Post date:** [May 5, 2020, 2:19pm UTC](https://community.plotly.com/t/histogram-bin-size-with-plotly-express/38927/5 "2020-05-05T14:19:40Z")

</div>

Thanks for taking the time to further explain your use case.

You are right that you cannot pass an `xbins` attribute directly to the `px.histogram` constructor.

This is because Plotly Express was designed as a terse, high-level wrapper for the `graph_objects` library, so only a subset of the attributes that you can set using a `graph_objects` constructor are available in the Plotly Express constructors for figures.

However, it is important to note that the Plotly Express constructor always returns a `graph_objects` figure object.

This means you can use the `update_traces` method to set any attributes which are available on the figure after using the Plotly Express constructor.

```python
import plotly.express as px
df = px.data.tips()
fig = px.histogram(df, x="total_bill")

fig.update_traces(xbins=dict( # bins used for histogram
        start=0.0,
        end=60.0,
        size=2
    ))

fig.show()

```

 ![Screen Shot 2020-05-05 at 10.17.44 AM](https://us1.discourse-cdn.com/flex024/uploads/plot/original/2X/6/6fafad8a7030bd710daa5be4b0ccd8bae4c05e77.png)

I hope that answers your question.

---

<div class="post-metadata">

**Author:** ![joseph.damiba](https://avatars.discourse-cdn.com/v4/letter/j/2acd7d/32.png) [@joseph.damiba](https://community.plotly.com/u/joseph.damiba)\
**Post date:** [May 5, 2020, 7:29pm UTC](https://community.plotly.com/t/histogram-bin-size-with-plotly-express/38927/6 "2020-05-05T19:29:59Z")

</div>

For future reference, I’d like to point out that `start` and `end` are optional attributes of the `xbins_size` parameter.

`fig.update_traces(xbins_size=2)` will also work, taking advantage of the magic underscore notation documented at [https://plotly.com/python/creating-and-updating-figures/#magic-underscore-notation](https://plotly.com/python/creating-and-updating-figures/#magic-underscore-notation).

---

<div class="post-metadata">

**Author:** ![vcmorini](https://avatars.discourse-cdn.com/v4/letter/v/5f9b8f/32.png) [@vcmorini](https://community.plotly.com/u/vcmorini)\
**Post date:** [May 6, 2020, 7:57am UTC](https://community.plotly.com/t/histogram-bin-size-with-plotly-express/38927/7 "2020-05-06T07:57:26Z")

</div>

@joseph.damiba, very helpful info, many thanks!

---

<div class="post-metadata">

**Author:** ![maulberto3](https://avatars.discourse-cdn.com/v4/letter/m/b782af/32.png) [@maulberto3](https://community.plotly.com/u/maulberto3)\
**Post date:** [April 23, 2021, 7:47pm UTC](https://community.plotly.com/t/histogram-bin-size-with-plotly-express/38927/8 "2021-04-23T19:47:27Z")

</div>

Hi @joseph.damiba Why there are less columns in the graph, considering that you specified 20 bins?

---

<div class="post-metadata">

**Author:** ![Sash7](https://avatars.discourse-cdn.com/v4/letter/s/a8b319/32.png) [@Sash7](https://community.plotly.com/u/Sash7)\
**Post date:** [April 22, 2022, 3:56pm UTC](https://community.plotly.com/t/histogram-bin-size-with-plotly-express/38927/9 "2022-04-22T15:56:36Z")

</div>

This is explained crisply. Saved a lot of time.
