Skip to content
svgrootogram

Hanging rootogram

Does a count model fit the distribution of counts — zeros, overdispersion, the tail?

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

How to read it

  1. Read the line as the counts the model expects, on a square-root scale.
  2. Each bar hangs from the line: its height is the observed count.
  3. A bar ending above or below zero marks a count the model under- or over-predicts.
  1. Callout 1: A bar past zero: more zeros than the model predicts.
  2. Callout 2: The line: the model's expected counts.
  3. Callout 3: Tail: are large counts well predicted?

Watch out

  • The square-root scale makes small mismatches visible but not proportional: read gaps with care.

Defaults

No settings: the chart draws one way.

Pinned in the code

  • Draws the engine's numbers as given (precomputed by the Python or R analysis); the chart computes nothing statistical.
  • Square-root scale (Tukey 1977): each bar spans √expected − √observed to √expected, so a bar bottom below 0 means under-prediction and above 0 over-prediction.
  • Expected frequencies are Σᵢ P(Y = k | xᵢ) from the fitted model (Poisson, negative binomial, …).
# Hanging rootogram — Codesplash Chart Lab (recipe "rootogram")
# 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("rootogram-demo.csv", keep_default_na=False, na_values=[""])  # demo data (only empty cells are missing): Chart Lab → Code → Download demo CSV

_tokens = cs.palette(dark=False) if cs else {}
SEQ = _tokens.get("sequential", ["#eff6fd", "#1a469d"])  # sequential ramp (low → high)
DIV = _tokens.get("diverging", ["#c13e2e", "#f3f5f8", "#0859ac"])  # negative, midpoint, positive
REF = _tokens.get("reference", "#4e5661")
GREY = _tokens.get("dim", "#ced1d6")
K = "visits"
OBS = "observed"
EXP = "expected (Poisson)"
d = df.dropna(subset=[K, OBS, EXP]).sort_values(K)
top = np.sqrt(d[EXP])  # bars hang from the expected frequency (square-root scale)
fig, ax = plt.subplots(figsize=(8, 4.5))
ax.bar(d[K], np.sqrt(d[OBS]), bottom=top - np.sqrt(d[OBS]), width=0.8, color=GREY, edgecolor=REF, linewidth=0.6)
ax.plot(d[K], top, color=PALETTE[0], linewidth=2, marker="o", ms=4, label=EXP)
ax.axhline(0, color="#ce5342", linewidth=1)
ax.set_xlabel(K)
ax.set_ylabel("√ frequency")
ax.legend(fontsize=8)
plt.show()

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