Skip to content
svgdistance-distance

Dissimilarity–dissimilarity plot

Do pairs of rows that differ more in one set of variables also differ more in another (the Mantel test's question)?

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

How to read it

  1. Read each point as a pair of rows: its distance in one matrix against the other.
  2. A rising cloud means pairs that differ in one set also differ in the other.
  3. Read r in the legend, then the Mantel test's p.
  1. Callout 1: A rising line: distances agree.
  2. Callout 2: Each point: one pair of rows.

Watch out

  • Pairs aren't independent. A usual correlation test is wrong: use the permutation p-value.

Defaults

Least-squares line
On
Add the fitted straight line.

Pinned in the code

  • One point per pair of rows (n(n − 1)/2 points); the engines send at most 5000 evenly spaced pairs.
  • The line is the least-squares fit of the first matrix's dissimilarity on the second's; its correlation is the (Pearson) Mantel statistic.
  • The pairs are not independent, so the line's ordinary standard errors are wrong — the Mantel test's permutation p-value is the valid test.
# Dissimilarity–dissimilarity plot — Codesplash Chart Lab (recipe "distance-distance")
# controls: line=true
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("distance-distance-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 = "geographic distance (km)"
Y = "Bray–Curtis dissimilarity"
LINE = True
d = df.dropna(subset=[X, Y])
x, y = d[X].to_numpy(float), d[Y].to_numpy(float)
fig, ax = plt.subplots(figsize=(7, 5))
ax.scatter(x, y, s=8 if len(x) > 2000 else 14, alpha=0.25 if len(x) > 2000 else 0.45, color=PALETTE[0])
if LINE and len(x) > 1:
    slope, intercept = np.polyfit(x, y, 1)  # least squares; its r is the (Pearson) Mantel statistic
    r = np.corrcoef(x, y)[0, 1]
    xs = np.array([x.min(), x.max()])
    ax.plot(xs, intercept + slope * xs, color=REF, linewidth=2, label=f"least squares, r = {r:.3f}")
    ax.legend(fontsize=8)
ax.set_xlabel(X)
ax.set_ylabel(Y)
ax.set_title("Every pair of rows")
plt.show()

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