v2026.11.2
All Bundles
Bundle

A matplotlib-like plotting library: scatter, histogram, line and bar charts, subplots, as standalone SVG. A Figure holds one or more Axes; drawing the figure builds a PlotScene, an ordered list of pixel-space drawing primitives (lines, polylines, polygons, rects, circles, text), and a backend turns the scene into output. The SVG backend writes SVG 1.1 with no script and no external font, one decimal for every coordinate, so the same data gives the same bytes on every platform. Compile with -lib plot,gen_collect; it needs nothing else and draws without a display. Design: docs/PLOT_DESIGN.md. Examples: docs/AI.md, "Plotting".

PlotMath

Numbers, ticks, colors and text helpers shared by the plotting classes.

Operations

BinCounts # function

Counts values into equal-width bins; a value equal to the top edge falls in the last bin and values outside the edges are not counted.

function : BinCounts(values:Float[], edges:Float[]) ~ Int[]

Parameters

NameTypeDescription
valuesFloatthe data
edgesFloatbin edges, ascending

Return

TypeDescription
Intone count per bin

BinEdges # function

Equal-width bin edges.

function : BinEdges(lo:Float, hi:Float, bins:Int) ~ Float[]

Parameters

NameTypeDescription
loFloatlowest edge
hiFloathighest edge, above lo
binsIntnumber of bins

Return

TypeDescription
Floatbins + 1 edges

Color # function

The categorical color of the i-th series: the matplotlib tab10 start, 8 colors.

function : Color(i:Int) ~ String

Parameters

NameTypeDescription
iIntseries index

Return

TypeDescription
Stringa "#rrggbb" color

Decimals # function

A number with a fixed count of decimals, always with a point (Float->ToString follows the C locale, which can write 12,3) and never as a negative zero ("-0.00").

function : Decimals(v:Float, decimals:Int) ~ String

Parameters

NameTypeDescription
vFloatthe value
decimalsIntdigits after the point

Return

TypeDescription
Stringthe text

Escape # function

XML-escapes text for an SVG element or attribute.

function : Escape(text:String) ~ String

Parameters

NameTypeDescription
textStringraw text

Return

TypeDescription
Stringtext with the five XML special characters escaped

Fmt # function

A coordinate with one decimal, so the same data gives the same bytes everywhere.

function : Fmt(v:Float) ~ String

Parameters

NameTypeDescription
vFloatthe value

Return

TypeDescription
Stringthe text, never "-0.0"

FormatNumber # function

A number as short text: no trailing zeros, no "-0".

function : FormatNumber(value:Float) ~ String

Parameters

NameTypeDescription
valueFloatthe number

Return

TypeDescription
Stringits text

FormatTick # function

A tick label: the value with as many decimals as the tick step needs and no more (0.2 steps give 0.2, 0.4; 1700000000.2 stays exact), and in scientific form when the step is below 1e-6, where decimals would print only zeros.

function : FormatTick(value:Float, step:Float) ~ String

Parameters

NameTypeDescription
valueFloatthe tick
stepFloatthe distance between ticks, or the value itself for a log axis

Return

TypeDescription
Stringthe text

Hex2 # function

Two lower-case hex digits.

function : Hex2(v:Int) ~ String

Parameters

NameTypeDescription
vInt0 to 255

Return

TypeDescription
Stringthe digits

IsFinite # function

Whether a number is finite: not NaN and not infinite.

function : IsFinite(x:Float) ~ Bool

Parameters

NameTypeDescription
xFloatthe number

Return

TypeDescription
Booltrue when finite

MaxOf # function

Largest of the values.

function : MaxOf(v:Float[]) ~ Float

Parameters

NameTypeDescription
vFloatvalues, not empty

Return

TypeDescription
Floatthe maximum

MinOf # function

Smallest of the values.

function : MinOf(v:Float[]) ~ Float

Parameters

NameTypeDescription
vFloatvalues, not empty

Return

TypeDescription
Floatthe minimum

NiceRange # function

A 1-2-5 range enclosing lo..hi.

function : NiceRange(lo:Float, hi:Float, target:Int) ~ Float[]

Parameters

NameTypeDescription
loFloatsmallest value
hiFloatlargest value
targetIntroughly how many steps

Return

TypeDescription
Float[nice low, nice high, step]

NiceStep # function

A 1-2-5 tick step for a range.

function : NiceStep(range:Float, target:Int) ~ Float

Parameters

NameTypeDescription
rangeFloatthe axis extent
targetIntroughly how many steps

Return

TypeDescription
Floatthe step

Quantile # function

A quantile by linear interpolation between the closest ranks, as numpy does by default.

function : Quantile(sorted:Float[], q:Float) ~ Float

Parameters

NameTypeDescription
sortedFloatthe data in ascending order, not empty
qFloatthe quantile from 0 to 1

Return

TypeDescription
Floatthe value

StripControl # function

Removes the control characters XML 1.0 cannot hold (everything below space except tab, line feed and carriage return), so a stray character in a title or label cannot make the SVG invalid.

function : StripControl(text:String) ~ String

Parameters

NameTypeDescription
textStringraw text

Return

TypeDescription
Stringthe text without them

TextWidth # function

A rough text width for layout, so legends and labels never depend on a font.

function : TextWidth(text:String, size:Int) ~ Float

Parameters

NameTypeDescription
textStringthe string
sizeIntfont size in pixels

Return

TypeDescription
Floatpixels

Ticks # function

Tick positions: the multiples of the step inside lo..hi. Each is computed as a whole number of steps, so there is no absolute rounding to collapse them on a range of 1e-9 or smear them on a range around 1e9; labels come from FormatTick, which prints the step's decimals.

function : Ticks(lo:Float, hi:Float, step:Float) ~ Float[]

Parameters

NameTypeDescription
loFloatlow end of the range
hiFloathigh end of the range
stepFloatdistance between ticks

Return

TypeDescription
Floatthe ticks (at least 2, at most 50)

Viridis # function

A viridis-like sequential color map: dark purple, blue, teal, green, yellow.

function : Viridis(t:Float) ~ String

Parameters

NameTypeDescription
tFloatposition from 0 to 1 (clamped)

Return

TypeDescription
Stringa "#rrggbb" color