Interactive Histograms and Distributions
Why interactive distributions
Section titled “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
Section titled “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
Section titled “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)
Section titled “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.
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"]
Continue to: Plotly Subplots and Facets for comparing many distributions side by side.
🧪 Try It Yourself
Section titled “🧪 Try It Yourself”Exercise 1 – Build a Histogram
Section titled “Exercise 1 – Build a Histogram”Exercise 2 – Overlay Two Groups
Section titled “Exercise 2 – Overlay Two Groups”Exercise 3 – Summarize with a Box Plot
Section titled “Exercise 3 – Summarize with a Box Plot”pch.coffeeTagline
pch.coffeeCtapch.feedbackHeading
pch.feedbackSubheading