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
- BinEdges
- Color
- Decimals
- Escape
- Fmt
- FormatNumber
- FormatTick
- Hex2
- IsFinite
- MaxOf
- MinOf
- NiceRange
- NiceStep
- Quantile
- StripControl
- TextWidth
- Ticks
- Viridis
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
| Name | Type | Description |
|---|---|---|
| values | Float | the data |
| edges | Float | bin edges, ascending |
Return
| Type | Description |
|---|---|
| Int | one count per bin |
BinEdges # function
Equal-width bin edges.
function : BinEdges(lo:Float, hi:Float, bins:Int) ~ Float[]Parameters
| Name | Type | Description |
|---|---|---|
| lo | Float | lowest edge |
| hi | Float | highest edge, above lo |
| bins | Int | number of bins |
Return
| Type | Description |
|---|---|
| Float | bins + 1 edges |
Color # function
The categorical color of the i-th series: the matplotlib tab10 start, 8 colors.
function : Color(i:Int) ~ StringParameters
| Name | Type | Description |
|---|---|---|
| i | Int | series index |
Return
| Type | Description |
|---|---|
| String | a "#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) ~ StringParameters
| Name | Type | Description |
|---|---|---|
| v | Float | the value |
| decimals | Int | digits after the point |
Return
| Type | Description |
|---|---|
| String | the text |
Escape # function
XML-escapes text for an SVG element or attribute.
function : Escape(text:String) ~ StringParameters
| Name | Type | Description |
|---|---|---|
| text | String | raw text |
Return
| Type | Description |
|---|---|
| String | text 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) ~ StringParameters
| Name | Type | Description |
|---|---|---|
| v | Float | the value |
Return
| Type | Description |
|---|---|
| String | the text, never "-0.0" |
FormatNumber # function
A number as short text: no trailing zeros, no "-0".
function : FormatNumber(value:Float) ~ StringParameters
| Name | Type | Description |
|---|---|---|
| value | Float | the number |
Return
| Type | Description |
|---|---|
| String | its 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) ~ StringParameters
| Name | Type | Description |
|---|---|---|
| value | Float | the tick |
| step | Float | the distance between ticks, or the value itself for a log axis |
Return
| Type | Description |
|---|---|
| String | the text |
Hex2 # function
Two lower-case hex digits.
function : Hex2(v:Int) ~ StringParameters
| Name | Type | Description |
|---|---|---|
| v | Int | 0 to 255 |
Return
| Type | Description |
|---|---|
| String | the digits |
IsFinite # function
Whether a number is finite: not NaN and not infinite.
function : IsFinite(x:Float) ~ BoolParameters
| Name | Type | Description |
|---|---|---|
| x | Float | the number |
Return
| Type | Description |
|---|---|
| Bool | true when finite |
MaxOf # function
Largest of the values.
function : MaxOf(v:Float[]) ~ FloatParameters
| Name | Type | Description |
|---|---|---|
| v | Float | values, not empty |
Return
| Type | Description |
|---|---|
| Float | the maximum |
MinOf # function
Smallest of the values.
function : MinOf(v:Float[]) ~ FloatParameters
| Name | Type | Description |
|---|---|---|
| v | Float | values, not empty |
Return
| Type | Description |
|---|---|
| Float | the minimum |
NiceRange # function
A 1-2-5 range enclosing lo..hi.
function : NiceRange(lo:Float, hi:Float, target:Int) ~ Float[]Parameters
| Name | Type | Description |
|---|---|---|
| lo | Float | smallest value |
| hi | Float | largest value |
| target | Int | roughly how many steps |
Return
| Type | Description |
|---|---|
| Float | [nice low, nice high, step] |
NiceStep # function
A 1-2-5 tick step for a range.
function : NiceStep(range:Float, target:Int) ~ FloatParameters
| Name | Type | Description |
|---|---|---|
| range | Float | the axis extent |
| target | Int | roughly how many steps |
Return
| Type | Description |
|---|---|
| Float | the step |
Quantile # function
A quantile by linear interpolation between the closest ranks, as numpy does by default.
function : Quantile(sorted:Float[], q:Float) ~ FloatParameters
| Name | Type | Description |
|---|---|---|
| sorted | Float | the data in ascending order, not empty |
| q | Float | the quantile from 0 to 1 |
Return
| Type | Description |
|---|---|
| Float | the 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) ~ StringParameters
| Name | Type | Description |
|---|---|---|
| text | String | raw text |
Return
| Type | Description |
|---|---|
| String | the 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) ~ FloatParameters
| Name | Type | Description |
|---|---|---|
| text | String | the string |
| size | Int | font size in pixels |
Return
| Type | Description |
|---|---|
| Float | pixels |
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
| Name | Type | Description |
|---|---|---|
| lo | Float | low end of the range |
| hi | Float | high end of the range |
| step | Float | distance between ticks |
Return
| Type | Description |
|---|---|
| Float | the ticks (at least 2, at most 50) |