svgvat-heatmap
VAT image (cluster tendency)
Is there cluster structure before any clustering is run?
How to read it
- Read the diagonal: dark blocks along it are candidate clusters.
- Count the blocks and their sizes.
- No blocks suggests no clear structure.
- Callout 1: A dark block: a candidate cluster.
- Callout 2: Another block.
- Callout 3: A smaller block.
Watch out
- It's a pre-check: an empty display doesn't rule out non-round clusters.
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.
- Order: start at the row holding the largest distance, then repeatedly add the unvisited row nearest to the visited set (Bezdek & Hathaway's VAT).
- Large tables are evenly sampled (≤ 500 rows) and averaged into at most 80 × 80 blocks; darker = closer.
# VAT image (cluster tendency) — Codesplash Chart Lab (recipe "vat-heatmap")
# 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("vat-heatmap-demo.csv", keep_default_na=False, na_values=[""]) # demo data (only empty cells are missing): Chart Lab → Code → Download demo CSV
R = "row block"
C = "column block"
V = "mean distance"
m = df.pivot_table(index=R, columns=C, values=V, aggfunc="mean").sort_index().sort_index(axis=1)
fig, ax = plt.subplots(figsize=(6, 5))
im = ax.imshow(m.to_numpy(), cmap="Blues_r", aspect="equal", interpolation="nearest", extent=[m.columns.min() - 0.5, m.columns.max() + 0.5, m.index.max() + 0.5, m.index.min() - 0.5])
fig.colorbar(im, label=V, shrink=0.8)
ax.set_xlabel("block (VAT order)")
ax.set_ylabel("block (VAT order)")
plt.show()Needs pandas, numpy, matplotlib · reads vat-heatmap-demo.csv · app theme from the optional codesplash helper
# VAT image (cluster tendency) — Codesplash Chart Lab (recipe "vat-heatmap")
# 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("vat-heatmap-demo.csv", keep_default_na=False, na_values=[""]) # demo data (only empty cells are missing): Chart Lab → Code → Download demo CSV
R = "row block"
C = "column block"
V = "mean distance"
m = df.pivot_table(index=R, columns=C, values=V, aggfunc="mean").sort_index().sort_index(axis=1)
fig = go.Figure(go.Heatmap(z=m.to_numpy(), x=m.columns, y=m.index, colorscale="Blues", reversescale=True, colorbar=dict(title=V)))
fig.update_layout(xaxis_title="block (VAT order)", yaxis_title="block (VAT order)", yaxis_autorange="reversed", yaxis_scaleanchor="x")
fig.show()Needs pandas, numpy, plotly · reads vat-heatmap-demo.csv · app theme from the optional codesplash helper
# VAT image (cluster tendency) — Codesplash Chart Lab (recipe "vat-heatmap")
# 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("vat-heatmap-demo.csv", check.names = FALSE, stringsAsFactors = FALSE, na.strings = "") # empty cells are missing; Chart Lab → Code → Download demo CSV
tok <- if (has_cs) codesplashr::cs_palette(dark = FALSE) else list()
seq_ramp <- if (is.null(tok$sequential)) c("#eff6fd", "#1a469d") else tok$sequential # low → high
div_ramp <- if (is.null(tok$diverging)) c("#c13e2e", "#f3f5f8", "#0859ac") else tok$diverging # −, 0, +
ref_col <- if (is.null(tok$reference)) "#4e5661" else tok$reference
grey_col <- if (is.null(tok$dim)) "#ced1d6" else tok$dim
R <- "row block"
C <- "column block"
V <- "mean distance"
d <- data.frame(r = df[[R]], c = df[[C]], v = df[[V]])
p <- ggplot(d, aes(c, r, fill = v)) +
geom_raster() +
scale_fill_gradient(low = seq_ramp[[length(seq_ramp)]], high = seq_ramp[[1]], name = V) +
scale_y_reverse() + coord_equal() +
labs(x = "block (VAT order)", y = "block (VAT order)") + theme_cs
print(p)Needs ggplot2 · reads vat-heatmap-demo.csv · app theme from the optional codesplashr helper