Skip to content
svgcaterpillar

Caterpillar plot

How much do groups differ in a mixed model, and which deviate clearly from the average?

Demo data · seed 20261101 · 36 rows · default settings · 640 × 360SVG, lightSVG, darkDemo CSV

How to read it

  1. Read each row as one group's estimated deviation from average.
  2. Rows are sorted by size.
  3. Intervals that stay away from zero mark groups that differ from average.
  1. Callout 1: A group above average.
  2. Callout 2: A group below average.
  3. Callout 3: Reference: the average group.

Watch out

  • These are shrunken estimates: small groups get pulled towards the average.

Defaults

No settings: the chart draws one way.

Pinned in the code

  • Draws the run's numbers as given (computed by the Python/R engine); the chart computes nothing statistical.
  • Groups are sorted by estimate within each term (largest at the top); with many groups, labels are hidden and marks shrink. Intervals are the engine's conditional (posterior) intervals.
# Caterpillar plot — Codesplash Chart Lab (recipe "caterpillar")
# controls: defaults
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt

try:
    import codesplash as cs  # optional: the app's chart theme
except ImportError:
    cs = None
if cs:
    cs.use_codesplash(dark=False)
PALETTE = cs.palette(dark=False)["categorical"] if cs else plt.rcParams["axes.prop_cycle"].by_key()["color"]

df = pd.read_csv("caterpillar-demo.csv", keep_default_na=False, na_values=[""])  # demo data (only empty cells are missing): Chart Lab → Code → Download demo CSV
df["term"] = pd.Categorical(df["term"].astype("string"), categories=["(Intercept)", "Days"])

def levels_of(s):
    """Declared level order (categoricals), else order of first appearance."""
    if isinstance(s.dtype, pd.CategoricalDtype):
        return [v for v in s.cat.categories if (s == v).any()]
    return list(dict.fromkeys(s.dropna().astype(str)))
G = "subject"
E = "random effect (ms)"
LO = "ci_low"
HI = "ci_high"
TERM = "term"
terms = levels_of(df[TERM]) if TERM else [None]
fig, axes = plt.subplots(1, len(terms), figsize=(4 * len(terms) + 1, 5), squeeze=False)
for ax, term in zip(axes[0], terms):
    d = (df if term is None else df[df[TERM].astype(str) == str(term)]).sort_values(E)
    y = np.arange(len(d))
    out = (d[LO] > 0) | (d[HI] < 0) if LO and HI else pd.Series(False, index=d.index)
    col = np.where(out, PALETTE[0], "grey")
    if LO and HI: ax.hlines(y, d[LO], d[HI], colors=col)
    ax.scatter(d[E], y, c=col, s=14, zorder=3)
    ax.axvline(0, color="grey", linewidth=0.8)
    ax.set_yticks(y, d[G].astype(str), fontsize=7)
    ax.set_title(str(term) if term is not None else "", fontsize=10, loc="left")
    ax.set_xlabel(E)
plt.show()

Needs pandas, numpy, matplotlib · reads caterpillar-demo.csv · app theme from the optional codesplash helper