Skip to content
Tutorials Center
My learning

Topics

Lesson 9 of 38 ยท Distributions

Histograms with histplot

Master seaborn histplot: choose bins and binwidth, switch stat to density or percent, add a KDE curve, compare groups with hue, multiple and element, and use log scales.

sns.histplot is the workhorse of distribution plots. It cuts a variable into bins and draws the count (or another statistic) per bin. It replaces the old distplot, which was removed from seaborn. This lesson walks through its most useful arguments: bins, stat, kde, hue with multiple, element, cumulative and log_scale.

Bins decide the story

The bins set how coarse the picture is. Too few bins hide structure; too many make noise. Control them with:

  • bins=20: number of bins (or an explicit list of edges).
  • binwidth=2.5: width of each bin in data units, often easier to reason about.
  • binrange=(0, 60): limit the range that gets binned.
  • Without any of these, seaborn picks a rule-based default (the Freedman-Diaconis rule).
Python Same data, different bin widths

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 first panel (very wide bins) shows one lump. The middle one reveals two humps. The last has so many narrow bins that random noise dominates. Always try a few widths. A useful check: if the shape changes a lot when the width changes a little, the structure is fragile.

What the bars measure: stat

stat changes the y axis:

stat Bar height Use for
"count" (default) Number of rows in the bin Raw numbers
"percent" Percent of all rows; bars sum to 100 Easy reading
"probability" / "proportion" Fraction of all rows; sum to 1 Comparing different sample sizes
"density" Count divided by total and bin width; area is 1 Matching a density curve
"frequency" Count divided by bin width Uneven bins

When comparing two groups of different size, a count histogram misleads, because the bigger group looks taller everywhere. Use stat="density" with common_norm=False so each group is normalized on its own.

Adding a KDE curve

kde=True draws a smooth line on top of the histogram, scaled to the same units as the bars.

Python
sns.histplot(data=df, x="order_value", binwidth=4, kde=True)

Style the line with line_kws={"linewidth": 2}. For a standalone curve, see kdeplot.

Comparing groups: hue, multiple and element

Python layer, stack, dodge and fill

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.

  • multiple="layer" (default): groups drawn on top of each other with transparency.
  • "stack": bars stacked; good for composition, hard for comparing the upper group.
  • "dodge": bars side by side; works best with few bins.
  • "fill": every bin scaled to 100%, showing the share of each group.
  • element="step" draws outlines instead of bars (also "poly"), which is much cleaner for overlapping groups.

Cumulative histograms and log scales

cumulative=True accumulates the bars, giving the number or share of values at or below each bin. With stat="percent" it reads like percentiles.

For skewed data, such as prices or populations, log_scale=True bins on a logarithmic axis so the long tail becomes visible:

Python
sns.histplot(data=df, x="wait", log_scale=True)
sns.histplot(data=df, x="wait", cumulative=True, stat="percent", element="step", fill=False)

A log scale requires positive values. It changes bin edges to constant ratios, not constant widths.

Discrete data

Counts of whole numbers (dice rolls, party sizes) get bins that straddle values by default. Pass discrete=True so each integer gets its own bar, centered on the value, with shrink=0.8 to leave small gaps:

Python discrete=True for whole numbers

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.

Common mistakes

  • Comparing groups of different sizes by raw counts.
  • Judging shape from one bin width.
  • Using kde=True on tiny samples: a smooth curve suggests more certainty than ten points give.
  • Using multiple="stack" when you want to compare the groups with each other.
  • Passing a column of strings: you get a bar per category (use countplot for that).

Check your understanding

Quick check ยท 5 questions

of 5 answered You scored / 5 ยท perfect!

  1. 1. Which arguments let you compare two groups of very different sizes fairly?

    Density with separate normalization scales each group to area 1, so sample size no longer dominates.
  2. 2. Which setting draws outlines instead of filled bars?

    element='step' (or 'poly') draws outlines, which keeps overlapping groups readable.
  3. 3. What does multiple='fill' show?

    Each bin is scaled to the full height, so the colors show proportions.
  4. 4. Your data are party sizes 1 to 6. What does discrete=True do?

    discrete=True gives each distinct value its own bar.
  5. 5. Why try several binwidth values?

    Too wide hides structure; too narrow adds noise.

Summary

  • histplot counts values into bins; set bins, binwidth or binrange, and test several.
  • stat selects count, percent, probability, density or frequency.
  • kde=True overlays a smooth curve; cumulative=True accumulates; log_scale=True helps skewed data.
  • For groups use hue with multiple (layer, stack, dodge, fill) and element="step".
  • Use common_norm=False and stat="density" to compare unequal groups.
  • discrete=True suits whole-number data.

Next: Density Curves with kdeplot