Skip to content
svgcalibration

Calibration plot

Do predicted probabilities match observed frequencies?

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

How to read it

  1. Read each point as a group's mean predicted risk against its observed rate.
  2. Points on the diagonal: predictions match reality.
  3. Above the diagonal: the model under-predicts there; below: over-predicts.
  1. Callout 1: A group of rows: its predicted and observed risk.
  2. Callout 2: Diagonal: perfect calibration.
  3. Callout 3: Below the diagonal: predictions are too high here.

Watch out

  • Groups depend on the binning, and a good AUC says nothing about calibration.

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.
  • Groups are the engine's (e.g. Hosmer–Lemeshow deciles of risk, g = 10); intervals are the engine's binomial CIs.
  • Probability axes are fixed to [0, 1] when every value lies in it; marker area ∝ group size.
# Calibration plot — Codesplash Chart Lab (recipe "calibration")
# 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("calibration-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")
PRED = "mean predicted risk"
OBS = "observed readmission rate"
N = "patients"
LO = "lower (Wilson)"
HI = "upper (Wilson)"
d = df.dropna(subset=[PRED, OBS])
fig, ax = plt.subplots(figsize=(6, 6))
ax.plot([0, 1], [0, 1], color=REF, linestyle="--", linewidth=1, label="perfect calibration")
if LO and HI:
    ax.errorbar(d[PRED], d[OBS], yerr=[d[OBS] - d[LO], d[HI] - d[OBS]], fmt="none", ecolor=PALETTE[0], capsize=2, linewidth=1)
size = 20 + 160 * d[N] / d[N].max() if N else 40  # marker area ∝ n per group
ax.plot(d[PRED], d[OBS], color=PALETTE[0], linewidth=1)
ax.scatter(d[PRED], d[OBS], s=size, color=PALETTE[0], zorder=3)
lim = max(1e-9, float(max(d[PRED].max(), (d[HI] if HI else d[OBS]).max())) * 1.05)
ax.set_xlim(0, min(1, lim))
ax.set_ylim(0, min(1, lim))
ax.set_xlabel(PRED)
ax.set_ylabel(OBS)
ax.legend(fontsize=8, loc="upper left")
plt.show()

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