Skip to content
Tutorials Center
My learning

Topics

Lesson 10 of 38 ยท Distributions

Density Curves with kdeplot

Draw smooth density curves with seaborn kdeplot: how a KDE works, bandwidth with bw_adjust, fill, hue, cut and clip, cumulative curves and common pitfalls.

A histogram is blocky: its look depends on where bins start. A kernel density estimate (KDE) replaces the blocks with a smooth curve. sns.kdeplot draws it, and it is often the nicest way to compare the shape of several groups in one picture. This lesson explains how a KDE is built and how to tune and read it.

How a KDE works

Place a small bell-shaped curve (a Gaussian "kernel") on top of every data point, then add all the curves and rescale so the area under the result is 1. Where points cluster, the bells pile up into a tall peak; where points are sparse, the curve is low.

Building a KDE

  1. Data points
Each observation is a mark on the axis.
  1. Kernels
Put a small bell curve on each point.
  1. Add them up
Overlapping bells form a smooth curve.
  1. Normalize
Scale so the total area is 1.

The bell's width is the bandwidth, and it controls smoothness.

1 / 2

The width of each bell is the bandwidth. A narrow bandwidth follows every wiggle of the data (noisy); a wide one smooths away real structure. Seaborn chooses a default using Scott's rule, which assumes data that are roughly bell-shaped and works well for many cases.

The basic call and bandwidth

bw_adjust multiplies the default bandwidth: below 1 gives a wigglier curve, above 1 a smoother one.

Python Bandwidth changes the curve

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.

With bw_adjust=0.2 the curve chases individual points and shows false small peaks. With 3 it blends the two real groups into one lump. The middle value, the default, shows the two humps.

Comparing groups

hue draws one curve per group. Useful arguments:

  • fill=True shades under each curve; alpha=0.4 keeps overlaps visible.
  • common_norm=False normalizes each group separately (the default True makes the curves share one total area, so bigger groups look taller).
  • multiple="stack" or "fill" stacks the densities.
  • palette="Set2" picks colors; see the styling chapter.
Python Groups as filled densities

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.

On the left, the smaller treatment group has a lower curve simply because it has fewer observations. On the right, each group has area 1, which compares the shapes. Choose based on the question: shapes (False) or relative sizes (True).

Edges: cut, clip and bounded data

A KDE extends past the minimum and maximum by cut bandwidths (default 3), so the tails taper to zero. This is a problem for variables with natural limits: ages cannot be below 0 and percentages cannot exceed 100.

  • cut=0 stops the curve at the data limits.
  • clip=(0, None) truncates the evaluated range to be at least 0.

Even then, a KDE blurs sharp edges. For strictly bounded data or integers, a histogram (with discrete=True) is more honest.

Other useful options

  • cumulative=True with common_grid=True: the smoothed CDF.
  • log_scale=True: a KDE on a log axis for skewed positive data.
  • levels= and thresh=: used for two-variable densities (next lessons).
  • vertical orientation: pass y="score" instead of x.
  • warn_singular=False: silences the warning when a group has no spread.

When to prefer a histogram

A KDE needs enough points (roughly 30 or more) to be meaningful. Ten values produce a plausible-looking curve that is mostly the bandwidth. Seaborn's displot(kind="kde") is the figure-level version if you need col= or row= faceting.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. In a KDE, what does the bandwidth control?

    Bandwidth is the width of each kernel. Small values follow the data closely; large values smooth strongly.
  2. 2. You set bw_adjust=3 and two humps merge into one. What does that tell you?

    Oversmoothing hides real features. Try smaller values.
  3. 3. Which setting makes each hue group's curve have area 1 regardless of its size?

    common_norm=False normalizes the groups independently so you compare shapes.
  4. 4. Your variable is age in years, never below 0. Which argument stops the KDE at the data limits?

    cut=0 truncates the curve at the smallest and largest observation.

Summary

  • A KDE sums small kernels placed on every data point to make a smooth density curve; the area is 1.
  • bw_adjust tunes smoothness: lower is wigglier, higher smoother.
  • hue plus fill=True compares groups; common_norm=False compares shapes.
  • Use cut=0 or clip= for bounded data, and prefer histograms for small samples or integers.
  • displot(kind="kde") is the faceting-friendly version.

Next: ECDF and Rug Plots