Skip to content

⚠ Early release: v0.3.2.sophistry is not ready for production use. Until 1.0, any minor release may break the API; expect missing features and bugs too.Stability and versioning

Heatmaps and contours

Values on a grid, or at scattered points, shown as colour and iso-lines. Start with HeatmapAnnotated; ColorNorms shows log and diverging norms with their colour bars. Imshow and the other grid marks paint as one image; see Batched rendering.

Annotated heatmap

Categorical rows and columns, a colormap, and each value written in a colour that reads on its cell.

HeatmapAnnotated.tsx
2 collapsed lines
import { Stage } from "@sophistry/solidjs";
import { Heatmap } from "@sophistry/solidjs/charts";
const days = ["Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"];
const hours = ["8am", "12pm", "4pm", "8pm"];
const visits = days.flatMap((day, i) =>
hours.map((hour, j) => ({
day,
hour,
n: Math.round(
20 + 60 * Math.sin((i + 1) * 0.7 + j) ** 2 + (i > 4 ? 25 : 0),
),
})),
);
export default function HeatmapAnnotated() {
return (
<Stage units={12}>
<Heatmap
data={visits}
x="day"
y="hour"
value="n"
ramp="viridis"
width={9}
height={4}
title="Visits per hour"
tooltip
/>
</Stage>
);
}

Heatmap with contours

A grid of values colored as a heatmap, with iso-lines of the same field drawn over it.

HeatmapContours.tsx
4 collapsed lines
import { draw, track } from "@sophistry/core";
import { Axes, Stage, usePlayer } from "@sophistry/solidjs";
import { Contour, Imshow } from "@sophistry/solidjs/charts";
import { onSettled } from "solid-js";
const x = Array.from({ length: 31 }, (_, i) => -3 + i * 0.2);
const y = Array.from({ length: 21 }, (_, i) => -2 + i * 0.2);
const z = y.map((yy) => x.map((xx) => Math.sin(xx) * Math.cos(yy * 1.5)));
export default function HeatmapContours() {
const { play } = usePlayer();
const lines = track(0);
onSettled(() => {
void play(draw(lines, { duration: 2 }));
});
return (
<Stage units={12}>
<Axes
xRange={[-3.1, 3.1, 1]}
yRange={[-2.1, 2.1, 1]}
width={9}
height={5.5}
zIndex={5}
// A halo keeps the numbers clear of the white contours running under them.
axis={{ stroke: "white", numberHalo: true }}
>
<Imshow
values={z}
xRange={[-3.1, 3.1]}
yRange={[-2.1, 2.1]}
rowOrigin="lower"
ramp="viridis"
/>
<Contour xs={x} ys={y} values={z} stroke="white" progress={lines} />
</Axes>
</Stage>
);
}

Clustered heatmap

Hours of use per app and weekday, clustered both ways by average linkage: similar apps and similar days move next to each other, and each dendrogram replays its merges, closest first, as the cells fade in.

ClusteredHeatmap.tsx
4 collapsed lines
import { draw, track } from "@sophistry/core";
import { Stage, usePlayer } from "@sophistry/solidjs";
import { Heatmap } from "@sophistry/solidjs/charts";
import { onSettled } from "solid-js";
const days = ["Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"];
// Three kinds of app — work, social, games — listed out of order.
const apps = [
{ app: "Mail", kind: 0 },
{ app: "Chat", kind: 1 },
{ app: "Chess", kind: 2 },
{ app: "Docs", kind: 0 },
{ app: "Photos", kind: 1 },
{ app: "Puzzles", kind: 2 },
{ app: "Calendar", kind: 0 },
{ app: "Forum", kind: 1 },
];
const weekday = [3, 1.2, 0.6];
const weekend = [0.5, 2.5, 2.8];
const usage = apps.flatMap(({ app, kind }, i) =>
days.map((day, j) => {
const base = j >= 5 ? weekend[kind] : weekday[kind];
const wobble = 0.4 * Math.sin((i + 1) * (j + 2) * 2.3);
return { app, day, hours: Math.max(0, base + wobble) };
}),
);
export default function ClusteredHeatmap() {
const { play } = usePlayer();
const progress = track(0);
onSettled(() => {
void play(draw(progress, { duration: 3 }));
});
return (
<Stage units={12}>
<Heatmap
data={usage}
x="day"
y="app"
value="hours"
cluster
labels={false}
ramp="viridis"
colorbar
colorbarLabel="hours"
width={6.4}
height={3.8}
center={{ x: 0.4, y: -0.3 }}
dendrogramSize={0.9}
progress={progress}
tooltip
/>
</Stage>
);
}

Log and diverging colour norms

A field spanning four decades on a log norm, and temperature anomalies on a diverging norm centred on zero, each with a colour bar that follows its norm.

ColorNorms.tsx
4 collapsed lines
import { draw, track } from "@sophistry/core";
import { Stage, usePlayer } from "@sophistry/solidjs";
import { Chart, Heatmap, Imshow } from "@sophistry/solidjs/charts";
import { onSettled } from "solid-js";
// A peak over a floor: 1 at the edges to 10,000 at the centre.
const n = 24;
const field = Array.from({ length: n }, (_, row) =>
Array.from({ length: n }, (_, col) => {
const dx = (col - n / 2) / (n / 4);
const dy = (row - n / 2) / (n / 4);
return 10 ** (4 * Math.exp(-(dx * dx + dy * dy)));
}),
);
// Monthly anomalies (°C) that run warmer than they run cold.
const months = ["Jan", "Mar", "May", "Jul", "Sep", "Nov"];
const years = ["2020", "2021", "2022", "2023", "2024"];
const anomalies = years.flatMap((year, i) =>
months.map((month, j) => ({
year,
month,
anomaly:
Math.round((0.6 * i - 0.6 + 1.1 * Math.sin(i * 1.7 + j * 1.8)) * 10) / 10,
})),
);
export default function ColorNorms() {
const { play } = usePlayer();
const progress = track(0);
onSettled(() => {
void play(draw(progress, { duration: 2 }));
});
return (
<Stage units={14}>
<Chart
width={3.6}
height={3.6}
center={{ x: -3.6, y: 0 }}
xRange={[0, n]}
yRange={[0, n]}
title={'rampScale="log"'}
colorbar
progress={progress}
>
<Imshow
values={field}
xRange={[0, n]}
yRange={[0, n]}
ramp="magma"
rampScale="log"
/>
</Chart>
<Heatmap
data={anomalies}
x="month"
y="year"
value="anomaly"
labels={false}
ramp="coolwarm"
rampMidpoint={0}
colorbar
colorbarLabel="°C"
title="rampMidpoint={0}"
width={4.4}
height={2.4}
center={{ x: 3, y: -0.4 }}
progress={progress}
/>
</Stage>
);
}

Contours from scattered samples

Readings at scattered stations, triangulated: after the frame, the faces fade in as a wave from the top-left while the contour lines draw level by level, lowest first.

TriangulatedField.tsx
4 collapsed lines
import { draw, track } from "@sophistry/core";
import { Stage, usePlayer } from "@sophistry/solidjs";
import { Chart, TriContour, Tripcolor } from "@sophistry/solidjs/charts";
import { onSettled } from "solid-js";
// 90 stations on a sunflower spiral (no grid), each reading a smooth field.
const stations = Array.from({ length: 90 }, (_, i) => {
const r = Math.sqrt((i + 0.5) / 90);
const x = 3 * r * Math.cos(i * 2.4);
const y = 2 * r * Math.sin(i * 2.4);
return { x, y, temp: Math.sin(x * 1.2) * Math.cos(y) + 0.3 * x };
});
export default function TriangulatedField() {
const { play } = usePlayer();
const progress = track(0);
onSettled(() => {
void play(draw(progress, { duration: 3 }));
});
return (
<Stage units={12}>
<Chart
width={7.5}
height={5}
progress={progress}
axis={{ position: "edge" }}
>
<Tripcolor
data={stations}
x="x"
y="y"
value="temp"
ramp="viridis"
stroke="white"
strokeWidth={0.008}
/>
<TriContour
data={stations}
x="x"
y="y"
value="temp"
levels={6}
stroke="white"
/>
</Chart>
</Stage>
);
}