Skip to content
svgcd-diagram

Critical-difference diagram

Across folds, which models are reliably better than which?

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

How to read it

  1. Read the top axis as mean rank across folds: lower is better.
  2. A thick bar joins models the test could not separate.
  3. Compare distances with the critical difference at the top.
  1. Callout 1: The critical difference.
  2. Callout 2: A thick bar: these models can't be separated.
  3. Callout 3: A model and its mean rank.

Watch out

  • Folds aren't fully independent, so the test can be optimistic. Use repeated folds if it matters.

Defaults

Critical difference
1.93
Nemenyi critical difference of mean ranks (the comparison sets it); 0 hides the bars.
Range 0 to 20, step 0.01

Pinned in the code

  • Draws the run's numbers as given (computed by the host's cs-ml evaluation or the Python/R engine); the chart computes nothing statistical.
  • Ranks are per fold (1 = best model in that fold, ties averaged) and averaged across the folds every model shared.
  • Bars join maximal runs of models within the critical difference (Demšar's layout); nested runs are drawn once.
  • Folds share training rows, so the Friedman/Nemenyi tests are optimistic — read the diagram as an ordering, and use the paired corrected t for claims about one pair.
# Critical-difference diagram — Codesplash Chart Lab (recipe "cd-diagram")
# controls: cd=1.93
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("cd-diagram-demo.csv", keep_default_na=False, na_values=[""])  # demo data (only empty cells are missing): Chart Lab → Code → Download demo CSV

L = "model"
R = "mean rank"
CD = 1.93  # critical difference
d = df[[L, R]].dropna().sort_values([R, L]).reset_index(drop=True)
ranks, names = d[R].to_numpy(float), d[L].astype(str).tolist()
cliques, last = [], -1  # Demšar: maximal runs of models within CD of each other
for i in range(len(ranks)):
    j = i
    while CD and j + 1 < len(ranks) and ranks[j + 1] - ranks[i] <= CD + 1e-12:
        j += 1
    if j > i and j > last:
        cliques.append((i, j))
        last = j
fig, ax = plt.subplots(figsize=(7, 3))
ax.plot(ranks, [0] * len(ranks), "o", color=PALETTE[0])
for k, (i, j) in enumerate(cliques):
    ax.plot([ranks[i], ranks[j]], [-0.12 - 0.06 * k] * 2, color="black", linewidth=3)
for i, (r, n) in enumerate(zip(ranks, names)):
    ax.annotate(f"{n} ({r:.2f})", (r, 0), xytext=(r, 0.15 + 0.1 * i), fontsize=8, ha="center", arrowprops=dict(arrowstyle="-", color="grey"))
ax.set_xlim(max(0.5, ranks.min() - 0.5), ranks.max() + 0.5)
ax.set_ylim(-0.3 - 0.06 * len(cliques), 0.3 + 0.1 * len(ranks))
ax.set_yticks([])
ax.set_xlabel("Mean rank across folds (1 = best)")
plt.show()

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