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()nbins 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()barmode="overlay" stacks both distributions on top of each other (instead of side by side); lowering opacity 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()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"]
Check your understanding
What does nbins=30 control in px.histogram?
nbins sets the bin count. Too few hides structure and too many makes the shape noisy.
Show answer
B — How many bins the range is split into — nbins sets the bin count. Too few hides structure and too many makes the shape noisy.
What does barmode="overlay" with opacity=0.6 do in the by-sex histogram?
overlay draws both distributions over the same bins and lower opacity shows the overlap.
Show answer
A — Stacks the groups on top of each other so you can see where they overlap — overlay draws both distributions over the same bins and lower opacity shows the overlap.
Why try several bin counts before concluding anything about the shape?
Bin count is not a neutral choice, so test a few before claiming the data has one peak or two.
Show answer
B — The same data can look unimodal or bimodal depending on nbins — Bin count is not a neutral choice, so test a few before claiming the data has one peak or two.
When is px.box preferable to one histogram per group?
A box plot summarizes median, quartiles and outliers in one compact shape per group.
Show answer
B — When comparing many groups compactly — A box plot summarizes median, quartiles and outliers in one compact shape per group.
Next
Continue to: Plotly Subplots and Facets for comparing many distributions side by side.
🧪 Try It Yourself
Exercise 1 – Build a Histogram
Tab indents. Press Escape, then Tab, to leave the editor. Control plus Enter runs the code; Control plus Shift plus Enter submits it.
Press Run to see what your code prints.Exercise 2 – Overlay Two Groups
Tab indents. Press Escape, then Tab, to leave the editor. Control plus Enter runs the code; Control plus Shift plus Enter submits it.
Press Run to see what your code prints.Exercise 3 – Summarize with a Box Plot
Tab indents. Press Escape, then Tab, to leave the editor. Control plus Enter runs the code; Control plus Shift plus Enter submits it.
Press Run to see what your code prints.