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

Coordinate systems

Scene units are where things are; data coordinates are what a graph is about. An axes node owns a plotting area of a known size in scene units and publishes a coordinate system that translates between the two. Everything else (graphs, annotations, charts, fields) plots in data space by asking that coordinate system where to draw.

The definitions live in @sophistry/core (axes, numberLine, numberPlane, polarPlane, complexPlane, functionGraph, …); the Solid components (<Axes>, <FunctionGraph>, …) come from @sophistry/solidjs.

<Axes> takes a data range for each axis ([min, max, step?]) and a size in scene units. The graphs and annotations you put inside it read its coordinate system from the tree, so a <FunctionGraph> plots in data coordinates with no extra wiring.

import { Axes, FunctionGraph, Stage } from "@sophistry/solidjs";
export const Scene = () => (
<Stage units={10}>
<Axes
xRange={[-3, 3, 1]}
yRange={[-2, 2, 1]}
width={8}
height={4.6}
tip
xLabel="x"
yLabel="y"
>
<FunctionGraph function={Math.sin} />
</Axes>
</Stage>
);

Without a framework it is the same tree, built with create:

import { axes, createScene, functionGraph } from "@sophistry/core";
const scene = createScene({ host: document.getElementById("figure")!, units: 10 });
scene.add(
axes.create({
xRange: [-3, 3, 1],
yRange: [-2, 2, 1],
width: 8,
height: 4.6,
children: [functionGraph.create({ function: Math.sin })],
}),
);

Three details decide whether axes look right:

  • width and height are the plotting area; center is its middle. It is not the data origin. [3, 8] × [120, 160] occupies exactly the same box as [-1, 1]².
  • Neither axis assumes zero is in range. Each axis pins to the nearest edge of the other’s range when zero is outside it, the way Manim does. coords.crossing() tells you where the axes cross, in data units.
  • Ticks are anchored on multiples of the step, not on the range minimum, so [-3.5, 3.5, 1] still marks the origin.

Only nodes that ask for a coordinate system plot in data space: graphs, annotations, fields and chart layers. A plain shape such as a <Dot> inside <Axes> is still placed in scene units; map its position yourself (below).

The object an axes node publishes is a CoordinateSystem:

Member Meaning
coordsToPoint(x, y?) data coordinates to scene units (a Point)
pointToCoords(point) scene units back to { x, y } in data units
xRange(), yRange() the ranges as given
area() the plotting area’s bounds in scene units
crossing() the data point where the axes cross

A coordinate system is immutable: its methods are pure functions of the ranges and size it was made from. When a range or the size changes, the axes publish a new object, and keep the same one while nothing changed. So never cache one across changes; read it again. Values outside the range are not clamped; they land outside area().

There are two ways to get one.

From the node. The node is the handle: ref receives the axes node, and coordsOf(node) returns the system it publishes (or null for a node that publishes none). It brings the node up to date first, so it works as soon as the node exists.

2 collapsed lines
import { coordsOf, track } from "@sophistry/core";
import { Axes, Dot, Stage } from "@sophistry/solidjs";
export function Marked() {
const marker = track({ x: 0, y: 0 });
return (
<Stage units={10}>
<Axes
xRange={[0, 5]}
yRange={[0, 3]}
width={8}
height={5}
ref={(plot) => marker.set(coordsOf(plot)?.coordsToPoint(2, 1) ?? marker.peek())}
/>
<Dot center={marker} />
</Stage>
);
}

That reads the system once. When the ranges move (an animated range, a zoom), follow it instead: coordsReadable(node) is a Readable of the node’s current system, so derive from it, or watch it to write a track:

5 collapsed lines
import { coordsReadable, track, watch } from "@sophistry/core";
import type { Range, RangeTransition } from "@sophistry/core/geometry";
import type { Readable } from "@sophistry/core";
import { Axes, Dot } from "@sophistry/solidjs";
import { onCleanup } from "solid-js";
// Keep the dot on (2, 1) whatever the x range does.
export function Pinned(props: { xRange: Readable<Range | RangeTransition> }) {
const marker = track({ x: 0, y: 0 });
return (
<>
<Axes
xRange={props.xRange}
yRange={[0, 3]}
width={8}
height={5}
ref={(plot) =>
onCleanup(
watch(coordsReadable(plot), (coords) => {
if (coords) marker.set(coords.coordsToPoint(2, 1));
}),
)
}
/>
<Dot center={marker} />
</>
);
}

The richer systems have typed helpers: polarCoordsOf(node) and complexCoordsOf(node).

Down the tree. A composite you write reads the enclosing system with ctx.context(coordsContext) (or requireCoords(ctx, "MyComponent"), which throws a helpful error outside one) in its setup and update. Because the axes publish a new object on every change, reading it that way reruns your update exactly when the mapping changes. That is all the built-in graphs and charts do; see Extending for writing a composite.

A <NumberLine> is one axis: a line, ticks and numbers, with a range, a length in scene units, and center for its centre. <UnitInterval> is the same thing preset to [0, 1, 0.25]. <Axes> is two number lines plus the coordinate system they imply, and <NumberPlane> is <Axes> over a grid.

import { NumberLine, Stage, UnitInterval } from "@sophistry/solidjs";
export const Lines = () => (
<Stage units={10}>
<NumberLine range={[-4, 4, 1]} length={8} center={{ x: 0, y: 1 }} tip />
<UnitInterval length={6} center={{ x: 0, y: -1 }} minorTickCount={4} />
</Stage>
);

Style and tick options for a single axis live in an options object: axis applies to both, and xAxis / yAxis override it per axis. They take everything a numberLine takes except its geometry: step, minorTickCount, numbers, numbersToInclude and numbersToExclude, decimalPlaces, numberDirection, tip, scale, stroke and so on, plus position to move the axis line ("zero", "edge", a value, or "top"/"right").

<Axes
xRange={[0, 10, 2]}
yRange={[0, 1, 0.25]}
width={8}
height={4}
axis={{ stroke: "tomato" }}
xAxis={{ minorTickCount: 1 }}
yAxis={{ decimalPlaces: 2 }}
/>

progress on any of these is spent in phases: the axis line, then its ticks, then its numbers. So draw(progress) draws an axis the way a person would, with one track. <NumberPlane> adds a grid on the major ticks behind everything else (gridZIndex, −1 by default), with minorGridLineCount for sub-lines, and caps the line count and thins it by stride rather than drawing thousands of lines.

Coordinate plane

Axes map data coordinates to scene units: plot a function inside them, and use coordsToPoint() to place a point at (2, 4).

CoordinatePlane.tsx
9 collapsed lines
import type { SceneNode } from "@sophistry/core";
import { coordsOf, ORIGIN } from "@sophistry/core";
import {
Axes,
DashedLine,
FunctionGraph,
LabeledDot,
Stage,
} from "@sophistry/solidjs";
export default function CoordinatePlane() {
// The node is the handle; coordsOf reads the system it publishes.
let axes!: SceneNode;
const toScene = (x: number, y: number) =>
coordsOf(axes)?.coordsToPoint(x, y) ?? ORIGIN;
return (
<Stage units={10}>
<Axes
ref={(node) => (axes = node)}
xRange={[-3, 3, 1]}
yRange={[0, 9, 3]}
width={8}
height={4.5}
axis={{ numbers: true }}
xLabel="x"
yLabel="y"
>
<FunctionGraph function={(x) => x * x} />
</Axes>
<DashedLine from={toScene(2, 0)} to={toScene(2, 4)} />
<LabeledDot center={toScene(2, 4)} minRadius={0.22} text="P" />
</Stage>
);
}

Axes are linear by default. Set scale: "log" on an axis for a logarithmic mapping (the range must be wholly positive; ticks land on decades, or on the 2, 3 and 5 multiples when the axis is zoomed inside one, and base sets the log base). "sqrt", "symlog", "utc" and "time" are the other names. A custom scale is a value, not a registered name: any object with toUnit(value) (data to a 0..1 fraction of the axis) and fromUnit(fraction) works, and linearScale / logScale from @sophistry/core/geometry are the built-ins. The axis, the grid and coordsToPoint all share one scale, so a graph plots exactly where the ticks say it does.

Log-scale axes

A log y axis turns exponential growth into a straight line, with ticks on each decade. The graph plots through the same scale as the axis.

LogScaleAxes.tsx
3 collapsed lines
import { draw, sequence, track } from "@sophistry/core";
import { Axes, FunctionGraph, Stage, usePlayer } from "@sophistry/solidjs";
import { onSettled } from "solid-js";
export default function LogScaleAxes() {
const { play } = usePlayer();
const axes = track(0);
const graphs = track(0);
// The axes first, then the curves over them.
onSettled(() => {
void play(
sequence(draw(axes, { duration: 1 }), draw(graphs, { duration: 1.2 })),
);
});
return (
<Stage units={12}>
<Axes
xRange={[0, 6, 1]}
yRange={[1, 10000]}
width={8}
height={5}
yAxis={{ scale: "log" }}
xLabel="t"
yLabel="N"
progress={axes}
>
<FunctionGraph function={(t) => 10 ** (0.65 * t)} progress={graphs} />
<FunctionGraph
function={(t) => 10 ** (0.4 * t + 1)}
progress={graphs}
/>
</Axes>
</Stage>
);
}

To change how numbers are written, use tickFormat on an axis (or a <NumberLine>). It is a built-in name ("plain", "si", "scientific", "percent", "piMultiple") or your own (value, decimalPlaces?) => string:

2 collapsed lines
import { formatTick } from "@sophistry/core/geometry";
import { Axes, Stage } from "@sophistry/solidjs";
export const Scene = () => (
<Stage units={10}>
<Axes
xRange={[0, 2 * Math.PI, Math.PI / 2]}
yRange={[-1, 1, 1]}
width={8}
height={4}
xAxis={{ tickFormat: "piMultiple" }}
yAxis={{ tickFormat: (value) => `${formatTick(value)} u` }}
/>
</Stage>
);

tickFormatters in @sophistry/core/geometry ships plain, si (1.5k, 2.5m), scientific, percent and piMultiple (π/2, 3π/2). They are plain (value) => string functions, so you can call them from your own tickFormat function.

<PolarPlane> is the one coordinate system that is not a rectangular lattice in disguise. It draws rings and spokes over a disc whose diameter is size, with maxRadius at the outer edge. Underneath is a square cartesian mapping, so coordsToPoint still means what it means inside <Axes>, and a graph inside it still plots in ordinary data space. On top of that, its PolarCoordinateSystem adds polarToPoint(radius, theta) and its inverse pointToPolar(point), which returns { radius, theta }, with theta in radians and coming back in [0, 2π).

import { PolarPlane, Stage } from "@sophistry/solidjs";
export const Scene = () => (
<Stage units={10}>
<PolarPlane size={6} maxRadius={3} azimuthDivisions={12} azimuthUnits="radians" />
</Stage>
);

To plot r = f(θ), put a <PolarGraph> inside the plane (Manim’s plot_polar_graph). It runs over a full turn unless you pass from and to (radians), and lands each sample where the plane’s polarToPoint puts it. It takes the same sampling props as the other graphs (see Plotting functions) and graphHandle(node).pointAt(theta) gives the curve point. It works inside <Axes> and <NumberPlane> too, as (r cos θ, r sin θ) in their data space. A negative r plots on the far side of the pole, as in Manim.

import { PolarGraph, PolarPlane, Stage } from "@sophistry/solidjs";
export const Scene = () => (
<Stage units={10}>
<PolarPlane size={6} maxRadius={2} azimuthUnits="radians">
<PolarGraph function={(theta) => 1 + Math.cos(theta)} />
<PolarGraph function={(theta) => 2 * Math.cos(3 * theta)} from={0} to={Math.PI} />
</PolarPlane>
</Stage>
);

azimuthUnits is "none" (the default), "degrees" or "radians", and the radian labels are exact multiples of π: piMultipleLabel(2, 12) is π/6, not 2π/12. Rings and spokes go through the same line cap as <NumberPlane>, so radiusStep={0.001} gives a coarse plane rather than ten thousand nodes.

Polar rose

Trace the curve r = 3 cos 5θ on a polar grid by sweeping θ from 0 to π.

PolarRose.tsx
9 collapsed lines
import { draw, track } from "@sophistry/core";
import {
ParametricGraph,
PolarPlane,
Stage,
Text,
usePlayer,
} from "@sophistry/solidjs";
import { onSettled } from "solid-js";
const rose = (theta: number) => {
const r = 3 * Math.cos(5 * theta);
return { x: r * Math.cos(theta), y: r * Math.sin(theta) };
};
export default function PolarRose() {
const { play } = usePlayer();
const drawn = track(0);
onSettled(() => {
void play(draw(drawn, { duration: 4 }));
});
return (
<Stage units={14}>
<PolarPlane size={6} maxRadius={3} radiusStep={1}>
<ParametricGraph
function={rose}
from={0}
to={Math.PI}
progress={drawn}
/>
</PolarPlane>
<Text text="r = 3 cos 5θ" x={5} y={0} />
</Stage>
);
}

<ComplexPlane> is a <NumberPlane> with the imaginary axis labelled i, 2i, -i. Its ComplexCoordinateSystem adds numberToPoint({ re, im }) (Manim’s number_to_point) and pointToNumber(point).

2 collapsed lines
import { complexCoordsOf, track } from "@sophistry/core";
import { ComplexPlane, Dot, Stage } from "@sophistry/solidjs";
export function PointOnPlane() {
const marker = track({ x: 0, y: 0 });
return (
<Stage units={10}>
<ComplexPlane
xRange={[-3, 3, 1]}
yRange={[-2, 2, 1]}
width={8}
height={5.3}
ref={(plane) => {
const coords = complexCoordsOf(plane);
if (coords) marker.set(coords.numberToPoint({ re: 1.5, im: 1 }));
}}
/>
<Dot center={marker} />
</Stage>
);
}

Complex multiplication

Multiplying z by 1 + i turns it by 45 degrees and stretches it by the square root of 2, which is the argument and modulus of 1 + i.

ComplexMultiplication.tsx
10 collapsed lines
import { derive, hold, track, valueTo } from "@sophistry/core";
import {
ComplexPlane,
LabeledDot,
Stage,
Text,
usePlayer,
Vector,
} from "@sophistry/solidjs";
import { onSettled } from "solid-js";
// z = 1 + 0.5i is multiplied by w = 1 + i, whose modulus is sqrt(2) and
// argument is 45 degrees. t = 0..1 applies that rotation and scaling gradually.
const Z = { x: 1, y: 0.5 };
const ARG_W = Math.PI / 4;
export default function ComplexMultiplication() {
const { play, signal } = usePlayer();
const t = track(0);
const zw = derive(t, (t) => {
const scale = Math.SQRT2 ** t;
const [cos, sin] = [Math.cos(t * ARG_W), Math.sin(t * ARG_W)];
return {
x: scale * (Z.x * cos - Z.y * sin),
y: scale * (Z.x * sin + Z.y * cos),
};
});
const moved = derive(t, (t) => t > 0.02);
onSettled(() => {
void (async () => {
while (!signal.aborted) {
await play(hold(1));
await play(valueTo(t, 1, { duration: 2.5 }));
await play(hold(1.5));
await play(valueTo(t, 0, { duration: 1.5 }));
}
})();
});
return (
<Stage units={12}>
<ComplexPlane xRange={[-3, 3]} yRange={[-3, 3]} width={6} height={6} />
<Vector to={zw} visible={moved} />
<LabeledDot center={zw} text="z·w" visible={moved} />
<Vector to={Z} />
<LabeledDot center={Z} text="z" />
<Text text="w = 1 + i" x={-4.3} y={2} />
</Stage>
);
}

<FunctionGraph function={...} /> inside an <Axes> plots y = f(x) in data coordinates, over the axes’ own xRange unless you pass from and to. <ParametricGraph function={(t) => ({ x, y })} from to /> does the same for t ↦ (x, y). graphHandle(node) turns any of the three graph nodes into a FunctionGraphHandle, whose pointAt(x) returns the curve point in scene units, so an annotation can attach to the curve without re-sampling it.

import { Axes, FunctionGraph, ParametricGraph, Stage } from "@sophistry/solidjs";
export const Scene = () => (
<Stage units={10}>
<Axes xRange={[-3, 3, 1]} yRange={[-3, 3, 1]} width={7} height={7}>
<FunctionGraph function={(x) => x ** 3 - 2 * x} />
<ParametricGraph
function={(t) => ({ x: 2 * Math.cos(t), y: 2 * Math.sin(t) })}
from={0}
to={2 * Math.PI}
/>
</Axes>
</Stage>
);

Both sample the function samples times (default 160) and clip to the plotting area (clip={false} turns that off). The curve breaks at non-finite values, at inputs listed in discontinuities, and at jumps bigger than jumpThreshold (default 0.5 of the axis extent). Only the first two are exact, so when you know where a function breaks, say so:

<FunctionGraph function={(x) => 1 / x} discontinuities={[0]} />
<FunctionGraph function={Math.tan} discontinuities={[-Math.PI / 2, Math.PI / 2]} />

A graph is an ordinary shape, so progress draws it along its length and stroke, strokeWidth and dash style it. Pass a track as progress and play draw on it.

Sine and cosine

Plot two functions on one set of axes and draw them in with draw().

SinAndCosFunctionPlot.tsx
9 collapsed lines
import { draw, RED, TEAL, track } from "@sophistry/core";
import {
Axes,
FunctionGraph,
Stage,
Text,
usePlayer,
} from "@sophistry/solidjs";
import { onSettled } from "solid-js";
export default function SinAndCosFunctionPlot() {
const { play } = usePlayer();
const drawn = track(0);
onSettled(() => {
void play(draw(drawn, { duration: 2.5 }));
});
return (
<Stage units={14}>
<Axes xRange={[-7, 7, 1]} yRange={[-1.5, 1.5, 0.5]} width={12} height={4}>
<FunctionGraph function={Math.sin} stroke={RED} progress={drawn} />
<FunctionGraph function={Math.cos} stroke={TEAL} progress={drawn} />
</Axes>
<Text text="sin x" x={2} y={2.6} color={RED} />
<Text text="cos x" x={-0.6} y={2.6} color={TEAL} />
</Stage>
);
}

<ImplicitFunction function={(x, y) => ...} /> draws the curve function(x, y) = 0 (Manim’s ImplicitFunction). The curve is searched for over the plotting area (or xRange/yRange) on an adaptive grid, and every connected piece is drawn; progress reveals the pieces one after another.

import { Axes, ImplicitFunction, Stage } from "@sophistry/solidjs";
export const Scene = () => (
<Stage units={10}>
<Axes xRange={[-3, 3, 1]} yRange={[-3, 3, 1]} width={7} height={7}>
<ImplicitFunction function={(x, y) => x ** 2 + y ** 2 - 4} />
<ImplicitFunction function={(x, y) => y ** 2 - x ** 3 + x} />
</Axes>
</Stage>
);

The search finds where function changes sign, so a loop smaller than one grid cell can slip through (raise minDepth), and a curve that touches zero without changing sign, such as (x² + y² − 1)², is never found. The same pure function is implicitCurve(f, xRange, yRange, options) in @sophistry/core/geometry. It returns the pieces as { points, closed }.

The annotations take function and read the same coordinate system as the graph, so the two cannot disagree about where the curve is. Their numeric props (from, to, x, dx, count) are ordinary props: pass a track and the rectangles subdivide or the secant walks into a tangent as it changes, with nothing captured at construction.

Component Draws
AreaUnderGraph the filled region between the graph and the baseline over from..to (bounded for the area between two functions)
RiemannRectangles count rectangles (or width dx) with sample of "left", "right" or "midpoint"
VerticalLineToGraph, HorizontalLineToGraph a (dashed) drop line from the axis to the curve, with optional dot and label
SecantSlopeGroup a secant through x and x + dx, with the slope triangle and dxLabel / dyLabel
TangentToGraph the tangent at x
9 collapsed lines
import {
AreaUnderGraph,
Axes,
FunctionGraph,
RiemannRectangles,
SecantSlopeGroup,
Stage,
VerticalLineToGraph,
} from "@sophistry/solidjs";
const f = (x: number) => 0.4 * x * x - 1;
export const Scene = () => (
<Stage units={10}>
<Axes xRange={[0, 6.5, 1]} yRange={[-2, 4, 1]} width={8.4} height={4.4}>
<FunctionGraph function={f} />
<RiemannRectangles function={f} count={8} sample="midpoint" />
<AreaUnderGraph function={f} from={1} to={4} />
<VerticalLineToGraph function={f} x={2} dot label="x = 2" />
<SecantSlopeGroup function={f} x={1} dx={0.8} dxLabel="dx" dyLabel="dy" />
</Axes>
</Stage>
);

SecantSlopeGroup falls back to the true tangent direction when dx reaches zero, so the line stays put at the end of a dx → 0 animation instead of collapsing to a NaN direction. The slopes themselves are pure functions too: slopeOfSecant, angleOfTangent and inputToGraphPoint.

Area under a curve

Riemann rectangles are stacked ever more thinly until they fill the area under the curve.

AreaUnderCurve.tsx
11 collapsed lines
import { derive, track, valueTo } from "@sophistry/core";
import {
AreaUnderGraph,
Axes,
FunctionGraph,
RiemannRectangles,
Stage,
Text,
usePlayer,
} from "@sophistry/solidjs";
import { onSettled } from "solid-js";
const f = (x: number) => (x * x) / 6;
export default function AreaUnderCurve() {
const { play } = usePlayer();
const count = track(3);
onSettled(() => {
void play(valueTo(count, 60, { duration: 4 }));
});
return (
<Stage units={14}>
<Axes xRange={[0, 6]} yRange={[0, 6]} width={9} height={6}>
<AreaUnderGraph function={f} from={1} to={5} fillOpacity={0.2} />
<RiemannRectangles
function={f}
from={1}
to={5}
count={count}
sample="midpoint"
/>
<FunctionGraph function={f} />
</Axes>
<Text
text={derive(count, (value) => `${Math.round(value)} rectangles`)}
y={3.5}
/>
</Stage>
);
}

Secant to tangent

Shrink dx toward zero and the secant line through two points turns into the tangent.

SecantToTangent.tsx
10 collapsed lines
import { derive, track, valueTo } from "@sophistry/core";
import {
Axes,
FunctionGraph,
SecantSlopeGroup,
Stage,
Text,
usePlayer,
} from "@sophistry/solidjs";
import { onSettled } from "solid-js";
const f = (x: number) => (x * x) / 6;
export default function SecantToTangent() {
const { play } = usePlayer();
const dx = track(2.5);
onSettled(() => {
void play(valueTo(dx, 0.01, { duration: 4 }));
});
return (
<Stage units={14}>
<Axes xRange={[0, 6]} yRange={[0, 6]} width={9} height={6}>
<FunctionGraph function={f} />
<SecantSlopeGroup function={f} x={3} dx={dx} length={4} />
</Axes>
<Text text={derive(dx, (value) => `dx = ${value.toFixed(2)}`)} y={3.5} />
</Stage>
);
}

<VectorField>, <StreamLines> and <NetworkGraph> have their own page: Fields and graphs.

Because pointToCoords is the inverse of coordsToPoint, turning a pointer position into data coordinates is one call. The scene’s pointer is a Readable of where the pointer is, in scene units (null while it is off the canvas), so a readout is a derive of it:

3 collapsed lines
import { coordsOf, derive } from "@sophistry/core";
import type { SceneNode } from "@sophistry/core";
import { Axes, Text, useScene } from "@sophistry/solidjs";
// Inside a <Stage>:
function Readout() {
const scene = useScene();
let plot: SceneNode | undefined;
const text = derive(scene.pointer, (pointer) => {
const coords = plot && coordsOf(plot);
if (!pointer || !coords) return "";
const { x, y } = coords.pointToCoords(pointer.scene);
return `(${x.toFixed(2)}, ${y.toFixed(2)})`;
});
return (
<>
<Axes xRange={[-3, 3, 1]} yRange={[-2, 2, 1]} width={8} height={4.6} ref={(node) => (plot = node)} />
<Text text={text} x={0} y={3} />
</>
);
}

See Interaction for dragging in data space, and the charts guide for data-driven plots, whose frame publishes the same kind of coordinate system.