Python Central HubSign in

Data Analytics with Python / Phase 7: Interactive Visualization with Plotly

Interactive Histograms and Distributions

Build interactive histograms and distribution visualizations with Plotly Express.

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

Python
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

Python
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.

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)

Python
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.

Check your understanding

quizCheck yourself
  1. What does nbins=30 control in px.histogram?

    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.

  2. What does barmode="overlay" with opacity=0.6 do in the by-sex histogram?

    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.

  3. Why try several bin counts before concluding anything about the shape?

    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.

  4. When is px.box preferable to one histogram 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

exerciseYour turn

Tab indents. Press Escape, then Tab, to leave the editor. Control plus Enter runs the code; Control plus Shift plus Enter submits it.

Output
Press Run to see what your code prints.

Exercise 2 – Overlay Two Groups

exerciseYour turn

Tab indents. Press Escape, then Tab, to leave the editor. Control plus Enter runs the code; Control plus Shift plus Enter submits it.

Output
Press Run to see what your code prints.

Exercise 3 – Summarize with a Box Plot

exerciseYour turn

Tab indents. Press Escape, then Tab, to leave the editor. Control plus Enter runs the code; Control plus Shift plus Enter submits it.

Output
Press Run to see what your code prints.

Was this page helpful?

Let us know how we did