Interactive Histograms and Distributions
Why interactive distributions
A histogram answers “how are these values spread out?” by grouping numbers into bins and counting how many fall in each. Interactive distributions help you:
- zoom into interesting ranges (drag across a busy region of bars)
- inspect outliers (hover a thin bar far from the rest)
- compare groups with hover tooltips (see the exact count per bin, per category)
Histogram
import pandas as pd
import plotly.express as px
df = px.data.tips()
fig = px.histogram(
df,
x="total_bill",
nbins=30,
title="Total bill distribution",
)
fig.show()import pandas as pd
import plotly.express as px
df = px.data.tips()
fig = px.histogram(
df,
x="total_bill",
nbins=30,
title="Total bill distribution",
)
fig.show()nbinsnbins controls how many bins the range is split into — too few bins hide structure (everything looks like one lump), too many bins make the shape noisy (every bar is its own spike).
Histogram by category
import plotly.express as px
df = px.data.tips()
fig = px.histogram(
df,
x="total_bill",
color="sex",
nbins=25,
barmode="overlay",
opacity=0.6,
title="Total bill distribution by sex",
)
fig.show()import plotly.express as px
df = px.data.tips()
fig = px.histogram(
df,
x="total_bill",
color="sex",
nbins=25,
barmode="overlay",
opacity=0.6,
title="Total bill distribution by sex",
)
fig.show()barmode="overlay"barmode="overlay" stacks both distributions on top of each other (instead of side by side); lowering opacityopacity lets you see where the two overlap.
Box plot (interactive)
import plotly.express as px
df = px.data.tips()
fig = px.box(df, x="day", y="total_bill", color="sex", title="Total bill by day")
fig.show()import plotly.express as px
df = px.data.tips()
fig = px.box(df, x="day", y="total_bill", color="sex", title="Total bill by day")
fig.show()A box plot summarizes a distribution’s median, quartiles, and outliers in one compact shape — useful when you’re comparing many groups and a full histogram per group would be too much to look at.
Tip
Start with a histogram, then add:
- category color
- hover fields
- facet columns/rows
flowchart LR A["Raw numeric column"] --> B["Split range into nbins
equal-width buckets"] B --> C["Count values
falling in each bucket"] C --> D["Draw one bar per bucket
height = count"]
Next
Continue to: Plotly Subplots and Facets for comparing many distributions side by side.
🧪 Try It Yourself
Exercise 1 – Build a Histogram
Exercise 2 – Overlay Two Groups
Exercise 3 – Summarize with a Box Plot
If this helped you, consider buying me a coffee ☕
Buy me a coffeeWas this page helpful?
Let us know how we did
