seaborn and interactive charts · lesson 2 of 10
Parts in this lesson
Asns.histplot(data=df, x="a", binwidth=200)counts in bins
Bsns.kdeplot(data=df, x="a", bw_adjust=0.5)a smooth density curve
Csns.ecdfplot(data=df, x="a")the share of values at or below each value
Dsns.displot(..., col="g", row="h")7.1: one panel per group

Distributions

Last updated

Before you compare averages, look at how a variable is spread out: where most values are, how far they reach, whether there's one hump or two. seaborn has four ways to show it. In this lesson you'll draw histograms and see how the bin width changes the story, smooth them into density curves, read a cumulative curve, split a distribution into panels, and catch a density curve claiming prices below zero.

By the end of this lesson you can

  • Draw a histogram with histplot and choose a sensible bin width
  • Draw a KDE curve, and explain what bandwidth does
  • Read an ECDF to answer “what share is below…?”
  • Keep a KDE from smoothing past the data’s real limits

Histograms and bin width

A histogram cuts the range of a variable into equal bins and draws a bar for how many values fall in each. Run the cells: the body mass of the penguins.

mass.ipynb
  • sns.histplot(data=penguins, x="body_mass_g") picks a bin width by itself, with a rule that looks at how many values there are and how spread out they are.
  • The second cell draws the same data with three widths you choose with binwidth=. At 25 g it's spiky: many bins hold one or two penguins, and the gaps between them mean nothing. At 1,000 g there are four bars and the shape is gone. At 250 g you can see what's there: a big group of lighter penguins, and a second, heavier hump.
  • The third cell splits the bars by species with hue=. element="step" draws each species as an outline so they don't hide each other. The heavier hump is the Gentoos.

There is no single right bin width: try a few, and pick the one where the shape stops changing much. Try it here, with a KDE curve (step 2) on top:

Bin and bandwidth slidersbody mass of 342 penguins
lightest, 2700 gheaviest, 6,300 g2,0003,0004,0005,0006,0007,000body_mass_g
sns.histplot(data=penguins, x="body_mass_g", binwidth=250, kde=True, kde_kws={"bw_adjust": 1.00})

In between, you can see two humps: lighter Adelie and Chinstrap penguins, and heavier Gentoos. The curve has 1 peak, and it starts at 1,951 g, 749 g lighter than any real penguin.

Density curves and the ECDF

A kernel density estimate (KDE) draws a smooth curve instead of bars. It puts a small bell curve on every value and adds them up; where values crowd together, the curve is high. How wide each little bell is, the bandwidth, does what the bin width does for a histogram. Run the cells.

density.ipynb
  • sns.kdeplot(..., hue="species", fill=True) draws one filled curve per species. By default each curve's area is its species' share of all the penguins, so the 68 Chinstraps get a smaller curve than the 151 Adelies. Add common_norm=False to give every curve the same area, when you care about shapes rather than numbers.
  • bw_adjust= multiplies seaborn's chosen bandwidth: 0.25 makes a bumpy curve that follows every penguin, 3 a smooth one that blurs the two humps into one.
  • sns.ecdfplot draws the empirical cumulative distribution function: for each weight on the x-axis, the share of penguins at or below it. It needs no bins and no bandwidth: every penguin is a step up.
  • The last cell checks one point off it: 0.494, so about half of all penguins weigh 4,000 g or less. On the ECDF, read up from 4,000 g on the x-axis. The Gentoo curve stays near zero there: almost no Gentoo is that light.

Histograms are easiest to explain to anyone; KDE curves are good for comparing a few groups; ECDFs answer “what share is below…?” exactly, and hide nothing.

Distributions in panels

sns.displot is the figure-level function for all three (7.1). With col= and row= it makes a grid of panels. Run the cell.

panels.ipynb

One panel for each species and sex, six in all, with titles like sex = male | species = Adelie. The axes are shared, so you can compare panels at a glance: in every species the males' flippers are longer than the females', and Gentoo flippers are the longest of all. The 11 penguins whose sex wasn't recorded are left out, as their sex is missing.

displot draws histograms by default; kind="kde" or kind="ecdf" switches.

Wrong way: prices below zero

A KDE curve is smooth because it spreads every value out a little to each side. Near a hard limit, that spreading goes past it. Run the cells: what 20 customers paid for a sachet of coffee.

sachets.ipynb

Most customers paid ₱5 or ₱6, and nobody paid less than ₱5. But the curve starts at −4.1 pesos: it says some customers paid nothing, and some were paid to take a sachet. seaborn draws the curve on past the smallest and largest values (by default, three bandwidths), and the little bells on the many ₱5 values spill over zero.

The second cell shows two fixes:

  • cut=0 stops the curve at the smallest and largest values. (clip=(0, None) is another way, which keeps it from going below 0.)
  • Or don't smooth at all: a histogram with binwidth=1 shows exactly what people paid, including the big pile at ₱5.

The check: before trusting a density curve, ask whether the variable has a limit (prices, weights and counts can't be negative; percentages stop at 100) and whether the curve runs past it.

Check what you learned

A histogram of daily sales looks like a comb: tall and short bars side by side, with gaps. What’s the first thing to try?
On an ECDF of delivery times, the curve is at 0.9 above 30 minutes. What does that tell you?
A KDE of how many children each household has shows a little curve below 0. What’s the best fix?

Sources

Every example on this page was run with Python 3.14, and its output and error messages are copied from it. These pages cover the same ground in more depth.

All sources for Simple Data Analysis