Use shared units and smoothing, disclose whether each ridge has unit area, and make overlaps readable.
Ridgeline plots: compare distributions without hiding their size
First decide what height means
A ridgeline plot stacks several distribution curves along one shared horizontal measure. Each ridge can be a normalized density or a count-scaled curve. Those choices answer different questions. Unit-area curves compare shape but erase group size; count-scaled curves retain volume but can bury small groups. Put the choice beside the chart and list group counts. A ridge with 47 observations should not appear as equally well supported as one with 4,700 just because both curves reach the same height. Box and violin displays offer a less overlapping alternative when the group count grows.
One bandwidth across comparable groups
A density ridge depends on the smoothing bandwidth. A narrow bandwidth can turn individual observations into false-looking peaks; a wide one can erase real mixtures. If the goal is comparison, use one documented bandwidth in the same units for every group, then inspect the underlying counts and a histogram or ECDF. Near a hard lower bound such as zero minutes, a symmetric kernel can place visual density below zero. Clip display only with an explicit boundary rule, or use a boundary-aware estimator. ECDFs avoid bandwidth choice entirely and are useful for checking tail claims.
Overlap is a design parameter
The vertical offset between ridges changes what is concealed. With heavy overlap, the front ridge can cover a smaller peak in the ridge behind it. Keep a thin outline, enough transparency and a stable layer order. Do not use color alone for identity; label each ridge on its own baseline. For a screen reader or a small phone, publish a companion table of group size, median and selected percentiles. Accessible chart alternatives help preserve the comparison when curves cannot be seen or hovered.
Keep populations and windows aligned
A weekly ridge may show an apparent shift because the eligible population changed, not because individual values moved. Define the event, unit, missing-value rule and period before comparing curves. If a week has too few valid observations, mark it as insufficient rather than creating a smooth curve from a handful of points. Keep the horizontal domain fixed across a report; otherwise a later ridge can look compressed only because its axis changed. Aggregation grain and period completeness should accompany the exported chart data.
Worked contract
from math import exp, isfinite, pi, sqrt
def gaussian_ridge(samples: list[float], grid: list[float],
bandwidth: float) -> list[float]:
if not samples or not grid or not isfinite(bandwidth) or bandwidth <= 0:
raise ValueError("samples, grid and positive bandwidth required")
if any(not isfinite(value) for value in samples + grid):
raise ValueError("values must be finite")
scale = len(samples) * bandwidth * sqrt(2 * pi)
return [sum(exp(-0.5 * ((position - value) / bandwidth) ** 2)
for value in samples) / scale for position in grid]
weekly_minutes = {"week 47": [27, 31, 35, 41],
"week 48": [31, 37, 43, 49]}
grid_minutes = [30, 40, 50]
ridge_rows = {week: gaussian_ridge(values, grid_minutes, 8)
for week, values in weekly_minutes.items()}
assert len(ridge_rows["week 47"]) == 3
assert all(height > 0 for heights in ridge_rows.values() for height in heights)
assert ridge_rows["week 47"][0] > ridge_rows["week 48"][0]Performance and operating cost
For G groups, M grid positions and N total observations, direct Gaussian evaluation takes O(MN) time and O(GM) output space. The sample lists also occupy O(N) memory. A histogram convolution or a vetted density library is preferable for a very large grid. The code estimates unbounded Gaussian densities; it does not correct a physical zero boundary or choose a statistically justified bandwidth. Save the bandwidth, group counts, grid and raw eligibility rule with every export.
Common Mistakes
- Reading equal-height, unit-area ridges as equal numbers of events.
- Using a different automatic bandwidth for each group while comparing peaks.
- Allowing overlap to hide a smaller group without outlines or a table.
- Treating a four-record smooth curve as stable evidence of several modes.
Validation drill
Compare 59 weekly response-time groups. Mark weeks with fewer than 47 valid records, hold the unit and horizontal range fixed, and verify one apparent tail shift with an ECDF.
