Skip to content

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

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()
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()

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

Histogram by sex
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()
Histogram by sex
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.

sketch A histogram is a bar chart of counts per bin p5.js
Raw values are grouped into equal-width bins; bar height = how many values landed in that bin.

Box plot (interactive)

Interactive box plot
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()
Interactive box plot
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
diagram From raw values to a histogram mermaid
Values are grouped into equal-width bins, then each bin's count becomes a bar.

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 coffee

Was this page helpful?

Let us know how we did