Skip to content
svgripley-envelope

Ripley's L with envelopes

At what distances are points more clustered or more regular than random?

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

How to read it

  1. Read x as distance and y as how far the points are from random at that distance.
  2. Above the envelope, points are closer together than random; below it, more evenly spaced.
  3. Use the dashed global envelope to judge the whole curve, and the shaded band for one chosen distance.
  1. Callout 1: Above the global envelope: clustered at these distances.
  2. Callout 2: Pointwise envelope from random patterns.
  3. Callout 3: Global envelope: the test of the whole curve.
  4. Callout 4: A random pattern sits on this line.

Watch out

  • Reading the shaded band at every distance finds false signals: a pointwise envelope is left somewhere far more often than its level says.
  • Density that varies across the window also looks like clustering, so this alone doesn't show that points attract each other.

Defaults

Subtract r
On
Draw L(r) − r so a random pattern is the flat line at 0; off draws L(r) against the line L = r.
Significance level
0.05
The level the envelopes were simulated at; it labels the legend.
Range 0.001 to 0.2, step 0.005

Pinned in the code

  • L(r) = √(K(r)/π) stabilizes the variance of Ripley's K, and L(r) = r under complete spatial randomness.
  • Use the global envelope to test the whole curve. A pointwise envelope is exceeded somewhere far more often than its level suggests, so read it distance by distance only.
  • Results depend on the observation window; a window much larger than the points' range makes any pattern look clustered.

Use with care

Clustering at a distance can also come from density that varies across the window. It does not by itself mean points attract one another.

# Ripley's L with envelopes — Codesplash Chart Lab (recipe "ripley-envelope")
# controls: centred=true, alpha=0.05
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("ripley-envelope-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")
R = "distance r"
L = "L(r)"
LO = "pointwise low"
HI = "pointwise high"
LO_G = "global low"
HI_G = "global high"
CENTRED = True  # L(r) − r: a random pattern is the flat line at 0
d = df.dropna(subset=[R, L]).sort_values(R)
r = d[R].to_numpy(dtype=float)
shift = r if CENTRED else 0.0
fig, ax = plt.subplots(figsize=(8, 4.5))
if LO and HI:
    ax.fill_between(r, d[LO] - shift, d[HI] - shift, color=PALETTE[0], alpha=0.16, linewidth=0, label="pointwise envelope")
if LO_G and HI_G:
    ax.plot(r, d[HI_G] - shift, color=GREY, linestyle="--", linewidth=1, label="global envelope")
    ax.plot(r, d[LO_G] - shift, color=GREY, linestyle="--", linewidth=1)
ax.plot(r, np.zeros_like(r) if CENTRED else r, color=REF, linestyle=":", label="random pattern")
ax.plot(r, d[L] - shift, color=PALETTE[0], linewidth=1.8, label="observed")
ax.set(xlabel=R, ylabel="L(r) − r" if CENTRED else "L(r)")
ax.legend(frameon=False, fontsize=8)
plt.show()

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