Make a histogram in Plotly with px.histogram(df, x="column"). Set the bin width exactly with fig.update_traces(xbins=dict(size=5)), compare groups with color= and barmode="overlay", and show percentages with histnorm="percent".
Quick Example
Finish times for a sample half marathon, split by age group:
import numpy as np
import pandas as pd
import plotly.express as px
rng = np.random.default_rng(73)
race = pd.concat([
pd.DataFrame({"age_group": group, "minutes": rng.lognormal(np.log(mid), spread, n).round(1)})
for group, mid, spread, n in [("Under 40", 112, 0.15, 520), ("40 and over", 127, 0.14, 380)]
], ignore_index=True)
colors = {"Under 40": "#0d9488", "40 and over": "#d97706"}
fig = px.histogram(race, x="minutes", color="age_group", barmode="overlay", opacity=0.65,
color_discrete_map=colors, template="plotly_white",
title="Half marathon finish times",
labels={"minutes": "Finish time (minutes)", "age_group": "Age group"})
fig.update_traces(xbins=dict(start=60, end=210, size=5))
fig.update_layout(yaxis_title="Runners", bargap=0.04)
fig.show()
Runners under 40 cluster about 15 minutes ahead of the older group, with a long tail of slower finishers in both. Hover a bar to see its range and count. The code targets Plotly 6.x.
Set the Number of Bins or the Bin Width
nbins is a hint, not an exact count. Plotly picks a round bin width based on it, so nbins=12 here draws 13 bars, each 10 minutes wide. For exact edges, set xbins with start, end and size.
fig = px.histogram(race, x="minutes", nbins=12, title="nbins=12",
template="plotly_white", color_discrete_sequence=["#475569"])
fig.show()
fig = px.histogram(race, x="minutes", title="2-minute bins with xbins",
template="plotly_white", color_discrete_sequence=["#475569"])
fig.update_traces(xbins=dict(start=60, end=210, size=2))
fig.show()


- For dates,
sizetakes milliseconds, or"M1"for one-month bins. endmarks where binning stops, so set it past your largest value.- Wide bins hide detail and narrow bins show noise. Try two or three widths before settling.
Overlay, Stack, or Group by Color
With color=, bars stack by default. That shows the total well but makes the upper group hard to read. barmode="overlay" draws them from a shared baseline, and barmode="group" puts them side by side.
for mode in ["relative", "group"]:
fig = px.histogram(race, x="minutes", color="age_group", barmode=mode,
color_discrete_map=colors, template="plotly_white",
title=f'barmode="{mode}"')
fig.update_traces(xbins=dict(start=60, end=210, size=10))
fig.show()


Show Percentages Instead of Counts
The groups have different sizes (520 and 380 runners), so raw counts favor the larger one. histnorm="percent" normalizes each trace separately.
fig = px.histogram(race, x="minutes", color="age_group", barmode="overlay", opacity=0.65,
histnorm="percent", color_discrete_map=colors, template="plotly_white",
title="Share of each age group")
fig.update_traces(xbins=dict(start=60, end=210, size=5))
fig.update_layout(yaxis_title="Percent of group")
fig.show()

histnorm |
Bar height means |
|---|---|
None (default) |
Count |
"percent" |
Percent of the trace's rows |
"probability" |
Fraction of the trace's rows |
"density" |
Count divided by bin width |
"probability density" |
Bar areas sum to 1, for comparing with a fitted curve |
Add Count Labels
text_auto=True writes each bar's value on it. Use wider bins so the numbers fit.
fig = px.histogram(race, x="minutes", text_auto=True, template="plotly_white",
title="Runners per 15 minutes", color_discrete_sequence=["#1e40af"])
fig.update_traces(xbins=dict(start=60, end=210, size=15), textposition="outside",
cliponaxis=False)
fig.update_layout(bargap=0.05, yaxis_title="Runners")
fig.show()

Add a Marginal Box Plot, Violin, or Rug
marginal= draws a second view of the same data above the histogram. The options are "box", "violin", "rug" and "histogram".
fig = px.histogram(race, x="minutes", color="age_group", marginal="box",
barmode="overlay", opacity=0.65, color_discrete_map=colors,
template="plotly_white", title="Histogram with marginal box plots")
fig.show()
Hovering the boxes gives the median and quartiles, which a histogram alone doesn't show. For those charts on their own, see box and violin plots in Plotly.
Add a Mean or Median Line
add_vline draws a vertical line with a label.
fig = px.histogram(race, x="minutes", template="plotly_white",
title="Mean and median finish time",
color_discrete_sequence=["#94a3b8"])
mean, median = race["minutes"].mean(), race["minutes"].median()
fig.add_vline(x=median, line=dict(color="#047857", width=3),
annotation_text=f"median {median:.0f}", annotation_position="top left")
fig.add_vline(x=mean, line=dict(color="#be123c", width=3, dash="dash"),
annotation_text=f"mean {mean:.0f}", annotation_position="top right")
fig.update_yaxes(range=[0, 125]) # room for the labels
fig.show()

The slow tail pulls the mean to the right of the median.
Cumulative Histogram
cumulative=True adds up the bins from left to right. With histnorm="percent" the y-axis reads as "percent finished within this time".
fig = px.histogram(race, x="minutes", color="age_group", barmode="overlay", opacity=0.6,
cumulative=True, histnorm="percent", color_discrete_map=colors,
template="plotly_white", title="Percent finished by each time")
fig.update_traces(xbins=dict(start=60, end=210, size=5))
fig.add_vline(x=120, line=dict(color="black", dash="dot"), annotation_text="2 hours",
annotation_position="top left")
fig.update_layout(yaxis_title="Percent finished")
fig.show()

By the two-hour mark, 68% of runners under 40 had finished, compared with 33% of the older group.
px.histogram vs px.bar vs NumPy
px.histogram sends every row to the browser and bins there. That's what makes hover and rebinning work, but a few million rows will make the figure heavy. Bin first with NumPy and draw bars:
counts, edges = np.histogram(race["minutes"], bins=np.arange(60, 211, 5))
fig = px.bar(x=edges[:-1] + 2.5, y=counts, template="plotly_white",
title="Pre-binned with np.histogram",
labels={"x": "Finish time (minutes)", "y": "Runners"})
fig.update_traces(width=5, marker_color="#334155")
fig.show()

| Use | When |
|---|---|
px.histogram |
Raw rows, and you want Plotly to bin, normalize or aggregate them |
np.histogram + px.bar |
Very large data, or custom (uneven) bin edges |
px.bar |
Categories that are already summarized |

