Skip to content
svgshepard-diagram

Shepard diagram

How faithfully does an MDS map keep the original distances?

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

How to read it

  1. Read x as the original dissimilarity, y as the map distance.
  2. A tight rising band: the map keeps the order of distances.
  3. Read stress in the legend: lower is better.
  1. Callout 1: Monotone fit: map distances follow the original.
  2. Callout 2: A wide spread here: short distances are less faithful.
  3. Callout 3: Stress.

Watch out

  • Low stress in two dimensions doesn't make clusters real: pairs can still be misplaced.

Defaults

No settings: the chart draws one way.

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.
  • Kruskal stress-1 = √(Σ (distance − fit)² / Σ distance²); below 0.05 excellent, 0.1 fair, above 0.2 poor (Kruskal 1964).
  • Large maps show an evenly spaced sample of pairs; the stress in the legend is recomputed from the pairs shown.
# Shepard diagram — Codesplash Chart Lab (recipe "shepard-diagram")
# 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("shepard-diagram-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")
X = "dissimilarity"
Y = "map distance"
F = "monotone fit"
d = df.dropna(subset=[X, Y]).sort_values(X)
fig, ax = plt.subplots(figsize=(6, 4.5))
ax.scatter(d[X], d[Y], s=8, color=PALETTE[0], alpha=0.5, label="pairs of rows")
if F:
    ax.step(d[X], d[F], where="post", color=PALETTE[1], linewidth=1.6, label="monotone fit")
ax.set_xlabel(X)
ax.set_ylabel(Y)
ax.legend(fontsize=8)
plt.show()

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