Issue with px.histogram auto bin choice

I have a uniform distribution that I would like to plot. I use px.histogram and expect to see a uniform distribution.

However, for some reason the distribution appears tapered at each end. It looks like while the values range from 0.00 - 1.00, there are bins at -0.05 - +0.05 and 0.95 - 1.05.

This behavior persists when specifying a range in update_layout, or specifying range_x in px.histogram. It also persists regardless of if you choose an even or odd number of bins. Here is an MRE:

import plotly.express as px
import scipy.stats as st

r = st.uniform.rvs(0, 1, size=1000)
px.histogram(r, nbins=12, range_x=[0, 1])

Does anyone have a way of ensuring the bins do not go past the min and max values of the dataset?

:raised_hand: Hi! Try this fig.update_xaxes(range=[-0.08,1.08]). Sorry if I didn’t understand you.

r = st.uniform.rvs(0, 1, size=1000)
fig = px.histogram(r, nbins=12, range_x=[0, 1])
fig.update_xaxes(range=[-0.08,1.08])
fig

Hi Juan, this doesn’t answer the question - see how it’s still only half frequency at each end? They are around 50 vs around 100. it should be around 100 for each bar

take a look a this plot, ‘out of the box’…

I think, that I’ve found what you’re talking about. Looking this hover data, I think it’s the issue related with the bin selection:

Googling it…

2 Likes

Great find!

Copy pasted again here for ease of implementation:

fig.update_traces(xbins=dict( 
        start=0.0,
        end=1.0
))