Skip to content
Tutorials Center
My learning

Topics

Lesson 28 of 38 ยท Multi-plot Grids

Ridgeline Plots in Seaborn

Build a ridgeline (joy) plot with FacetGrid: stacked, overlapping density curves for many groups, with sorting, colours, labels and common pitfalls.

A ridgeline plot (also called a joyplot) stacks one density curve per group in a single column, with the curves overlapping slightly so they look like mountain ridges. It is a compact way to compare the shape of many distributions at once, for example temperatures in each month, scores in each class, or response times for each server.

Seaborn has no ridgeplot() function. You build one with a FacetGrid: one row per group, no space between the rows, and a trick to let the curves overlap. It is a good exercise in how the grid works, and the result is worth it.

The idea

Ridgeline recipe

One row per group FacetGrid(row=group, hue=group)
Draw a density map_dataframe(kdeplot, fill=True)
Overlap rows Negative hspace, transparent panels
Label inside Name each ridge, hide row titles

Remove the y axis and spines for a clean look.

1 / 2

A first ridgeline

The data: monthly temperatures with a seasonal shape. Each month is a ridge.

Python A ridgeline plot of monthly temperatures

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.

What each step does:

  1. rc={"axes.facecolor": (0, 0, 0, 0)} makes panel backgrounds transparent. Without it, each lower panel would hide the ridge above.
  2. aspect=9, height=0.7 makes each panel very wide and short.
  3. Two kdeplot calls: one fills the area, the second draws a white outline so overlapping ridges stay distinct. clip_on=False lets curves spill outside their panel into the next one.
  4. hspace=-0.5 pulls the panels together so they overlap by half.
  5. label() writes the group name inside each panel using axes coordinates (transform=ax.transAxes).
  6. despine and set(yticks=[]) remove the clutter, since density values are not the point.

Adjusting the overlap

The overlap is a tradeoff. More overlap gives the mountain look; too much hides the lower parts of curves. Start at hspace=-0.3 and go lower if the curves are narrow. Try it with sorted groups:

Python Sorted ridges with less overlap

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.

Sorting the ridges by median turns the picture into a ranking: your eye follows the slope of the peaks. Ordering alphabetically (or by data appearance) hides this.

Variations

  • Histograms instead of densities: replace kdeplot with histplot(element="step") when the sample is small and a smooth density would suggest more certainty than you have.
  • Two groups per ridge: use hue for a second category, with fill alpha around 0.5.
  • Shared scale: panels share the x axis by default, which is what makes the ridges comparable. Keep it.
  • Density scale: with default common_norm=False, each ridge has its own height. Pass common_norm=True in the kdeplot calls if sample sizes matter, so that a ridge with few observations is lower.

Pitfalls

  • Densities extend beyond your data range. Use cut=0 or clip=(lo, hi) for bounded data such as scores or percentages.
  • Strong overlap makes bimodal distributions hard to see; reduce overlap for those.
  • Tick labels are on the bottom panel only. Add g.set(xlim=...) to keep the range sensible.
  • With 30+ groups the plot is tall and the bandwidth choice matters a lot; test bw_adjust.

The KDE itself, with bandwidth and cumulative options, is covered in the distribution chapter; the grid mechanics are in FacetGrid.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. Which seaborn object do you use to build a ridgeline plot?

    Seaborn has no ridgeplot function; a FacetGrid with row, negative hspace and transparent panels does the job.
  2. 2. Why set axes.facecolor to a fully transparent colour?

    Overlapping panels would otherwise paint their white backgrounds over the curves of the panel above.
  3. 3. What does hspace=-0.5 do?

    A negative vertical spacing pulls panels into each other.
  4. 4. What is a good way to order the ridges?

    Sorting by a summary makes the ranking visible.

Summary

  • A ridgeline plot is a FacetGrid with row and hue set to the same column, KDEs in each panel and negative hspace.
  • Use transparent panel backgrounds, clip_on=False and a white outline curve.
  • Label ridges inside the panels and remove y ticks and spines.
  • Sort groups by a summary statistic and use cut=0 for bounded data.

Next: Axis Labels, Titles, Sizes and Legends