Skip to content
svgreachability-plot

Reachability plot (OPTICS)

Where are the dense regions, at every density at once?

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

How to read it

  1. Read left to right in OPTICS order.
  2. Read each valley as a cluster; deeper valleys are denser.
  3. Read the peaks between valleys as the boundaries.
  1. Callout 1: A valley: one cluster.
  2. Callout 2: A peak: a boundary between clusters.
  3. Callout 3: A shallower valley: a denser or sparser cluster.

Watch out

  • Valleys can look deeper from the display's scale. A cut line gives a concrete clustering.

Defaults

Cut (0 = none)
0
Draw the reachability level that defines clusters; valleys below it become clusters.
Range 0 to 1,000,000,000, 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.
  • Reachability of a row = max(core distance of its predecessor, distance to it); the first row of each region has none and is drawn as a faint full-height bar.
  • Bars are coloured by the DBSCAN-equivalent clusters at the cut; noise is grey.
# Reachability plot (OPTICS) — Codesplash Chart Lab (recipe "reachability-plot")
# controls: cut=0
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("reachability-plot-demo.csv", keep_default_na=False, na_values=[""])  # demo data (only empty cells are missing): Chart Lab → Code → Download demo CSV
df["cluster"] = pd.Categorical(df["cluster"].astype("string"), categories=["1", "2", "3", "-1"])

_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")
def levels_of(s):
    """Declared level order (categoricals), else order of first appearance."""
    if isinstance(s.dtype, pd.CategoricalDtype):
        return [v for v in s.cat.categories if (s == v).any()]
    return list(dict.fromkeys(s.dropna().astype(str)))
X = "OPTICS order"
R = "reachability distance"
K = "cluster"
U = "no reachability"
d = df.sort_values(X)
h = d[R].astype(float)
top = np.nanmax(h) * 1.05
fig, ax = plt.subplots(figsize=(8, 4))
no_reach = h.isna() | (d[U].astype(str).str.lower() == "true" if U else False)
ax.bar(d.loc[no_reach, X], top, width=1, color=GREY, label="no reachability")
clusters = levels_of(d[K]) if K else []
for i, k in enumerate(clusters):
    g = d[(d[K].astype(str) == str(k)) & ~no_reach]
    noise = str(k).lower() in ("noise", "-1")
    ax.bar(g[X], g[R], width=1, color=GREY if noise else PALETTE[i % len(PALETTE)], label=str(k))
if not K:
    g = d[~no_reach]
    ax.bar(g[X], g[R], width=1, color=PALETTE[0])

ax.set_xlabel("OPTICS order")
ax.set_ylabel("reachability distance")
ax.legend(fontsize=8)
plt.show()

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