svgshepard-diagram
Shepard diagram
How faithfully does an MDS map keep the original distances?
How to read it
- Read x as the original dissimilarity, y as the map distance.
- A tight rising band: the map keeps the order of distances.
- Read stress in the legend: lower is better.
- Callout 1: Monotone fit: map distances follow the original.
- Callout 2: A wide spread here: short distances are less faithful.
- 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
# Shepard diagram — Codesplash Chart Lab (recipe "shepard-diagram")
# controls: defaults
import numpy as np
import pandas as pd
import plotly.express as px
import plotly.graph_objects as go
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 px.colors.qualitative.Plotly
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 = go.Figure()
fig.add_scatter(x=d[X], y=d[Y], mode="markers", marker=dict(color=PALETTE[0], size=4, opacity=0.5), name="pairs of rows")
if F:
fig.add_scatter(x=d[X], y=d[F], mode="lines", line=dict(color=PALETTE[1], shape="hv"), name="monotone fit")
fig.update_layout(xaxis_title=X, yaxis_title=Y)
fig.show()Needs pandas, numpy, plotly · reads shepard-diagram-demo.csv · app theme from the optional codesplash helper
# Shepard diagram — Codesplash Chart Lab (recipe "shepard-diagram")
# controls: defaults
library(ggplot2)
has_cs <- isTRUE(requireNamespace("codesplashr", quietly = TRUE)) # optional: the app's chart theme
theme_cs <- if (has_cs) codesplashr::theme_codesplash(dark = FALSE) else theme_minimal()
cs_cat <- if (has_cs) codesplashr::cs_palette(dark = FALSE)$categorical else grDevices::palette.colors(8, "Okabe-Ito")
if (has_cs) update_geom_defaults("text", list(colour = codesplashr::cs_palette(dark = FALSE)$foreground))
df <- read.csv("shepard-diagram-demo.csv", check.names = FALSE, stringsAsFactors = FALSE, na.strings = "") # empty cells are missing; Chart Lab → Code → Download demo CSV
X <- "dissimilarity"
Y <- "map distance"
F <- "monotone fit"
d <- data.frame(x = df[[X]], y = df[[Y]], f = if (is.null(F)) NA_real_ else df[[F]])
d <- d[is.finite(d$x) & is.finite(d$y), ]
d <- d[order(d$x), ]
p <- ggplot(d, aes(x, y)) +
geom_point(colour = cs_cat[[1]], size = 0.8, alpha = 0.5) +
(if (!is.null(F)) geom_step(aes(y = f), colour = cs_cat[[2]], linewidth = 0.8)) +
labs(x = X, y = Y) + theme_cs
print(p)Needs ggplot2 · reads shepard-diagram-demo.csv · app theme from the optional codesplashr helper