Skip to content
Tutorials Center
My learning

Topics

Lesson 9 of 42 ยท Basic Charts

Histograms in Plotly

Build histograms in Plotly with px.histogram and go.Histogram: choose bins, normalize to percent or density, aggregate with histfunc, overlay groups, add marginal plots and cumulative views.

A histogram shows the distribution of one numeric variable. The values are cut into intervals (bins), and each bar's height says how many values fall in that bin. Histograms reveal the shape of data: centered or skewed, one peak or several, outliers or not.

Plotly's histogram has a surprising property: the browser does the binning. Python sends the raw values, and plotly.js computes the bins when it draws the chart. This makes histograms very flexible (change the bins and it re-bins), but it also means the Python figure contains the raw data, not the bar heights.

Your first histogram

Python px.histogram and what it stores

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

The figure contains all 244 bills, and nbinsx=20 is only a suggestion: plotly.js picks "nice" bin edges close to that number. NumPy shows the same principle. If you want exact bins, set them yourself (next section).

Draw it

This example creates several histograms in plotly.js from raw samples, using xbins for exact bin edges:

Histogram with exact bins and overlay

With barmode="overlay" and an opacity below 1, two groups can be compared in one chart. Using the same xbins for both traces is essential. Different bin widths make the bars incomparable.

Controlling the bins

Property Meaning
nbinsx / nbinsy Approximate number of bins (Express: nbins)
xbins.start, xbins.end, xbins.size Exact first edge, last edge and bin width
autobinx False disables automatic binning
bingroup Traces with the same bingroup share bins automatically

In Express, px.histogram(df, x="score", color="group", barmode="overlay") already makes all traces share bins. A fixed bin width beats a bin count for communicating results, because "each bar is a 5-point range" is easy to understand. Try several widths: too few bins hide the shape, too many make noise.

What the bars show: histnorm and histfunc

By default each bar is a count. histnorm changes the scale:

histnorm Bar height
"" (default) Count of values in the bin
"percent" Percent of all values
"probability" Fraction of all values (sums to 1)
"density" Count divided by bin width
"probability density" Area of all bars sums to 1

Normalizing is needed when groups have different sizes: with histnorm="probability density", two groups of 50 and 5,000 values can be compared fairly.

histfunc changes what is aggregated per bin. With y given, the bar height can be the sum, avg, min, max or count of y. That turns a histogram into a quick group-by chart:

Python histnorm, histfunc and cumulative

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

The output confirms the settings that plotly.js reads. The printed group means (tips.groupby) are what the avg histogram will display as bar heights. cumulative=True turns bars into a running total, which is the visual form of a cumulative distribution function.

Marginals and facets

marginal="rug", "box" or "violin" adds a small chart above the histogram that shows the same data differently: individual points, or the quartile summary. facet_col and facet_row give one histogram per group, which is often better than overlaying more than two groups. text_auto=True writes the counts on the bars.

Histogram versus bar chart

These look alike but are not the same. A bar chart compares categories you already have; a histogram creates its own categories by cutting a number line into bins, so its bars touch (a small bargap) and their order is fixed. If you already have counts per category, use px.bar. If you have raw values, use px.histogram.

Common mistakes

  • Comparing two overlaid histograms with different bins or without normalization.
  • Using too few bins (important shapes vanish) or too many (noise looks like structure).
  • Reading a histogram of sums (histfunc="sum") as a distribution.
  • Forgetting that dates also work: px.histogram(df, x="date", nbins=12) makes a count per period.

Check your understanding

Quick check ยท 5 questions

of 5 answered You scored / 5 ยท perfect!

  1. 1. Where does the binning of a Plotly histogram happen?

    The figure stores the raw values and plotly.js computes the bins when drawing.
  2. 2. Which setting gives bins of exactly width 5, starting at 0?

    nbinsx is only a hint, while xbins fixes the edges and width.
  3. 3. You want to compare groups of very different sizes. Which option helps?

    Normalizing makes each group's bar areas sum to 1, so sizes no longer dominate.
  4. 4. What does histfunc="avg" with a y column draw?

    histfunc chooses the aggregation of y per bin: count, sum, avg, min or max.
  5. 5. When should you use px.bar instead of px.histogram?

    A histogram aggregates raw data; a bar chart displays values you already computed.

Summary

  • A histogram shows the distribution of one numeric variable; plotly.js bins the raw data.
  • Control bins with nbinsx (hint) or xbins (exact) and share them with bingroup.
  • histnorm rescales (percent, probability, density), histfunc aggregates a second column, cumulative accumulates.
  • Overlay groups with barmode="overlay" and opacity; add marginal plots or facets for more context.

Next: Pie and Donut Charts