Skip to content
Jennifer Programming Language

plot - data plotting to SVG

Import with import "plot.j" as plot;. Turns numbers into a self-contained <svg> chart string you can write to a .svg file or drop into HTML. A unified chart renders one or more series (line, points, both, or filled area, solid or dashed, with error bars and selectable marker shapes) on shared axes with a positioned legend; line / scatter / bar / bars / histogram are focused wrappers, and bars groups or stacks multiple series (negative values diverge from a zero baseline). Charts support log scales, a date axis, fonts, configurable margins, reference lines, data labels on bars, and native <title> hover tooltips. save writes the SVG to a file. Pure Jennifer over math and time / fs / strings / lists / convert; both binaries; the visual companion to the stats / ml numeric stack.

jennifer
import "plot.j" as plot;

def opts as plot.Options init plot.defaults();
$opts.title = "measured vs model";
$opts.refLines = [plot.hline(20.0, "target")];

def measured as plot.Series init plot.points("measured", [1.0, 2.0, 3.0], [1.1, 4.2, 8.8]);
$measured.yErr = [0.4, 0.5, 0.6];             # symmetric error bars
$measured.shape = "square";                    # marker shape
def model as plot.Series init plot.series("model", [1.0, 2.0, 3.0], [1.0, 4.0, 9.0]);
plot.save(plot.chart([$measured, $model], $opts), "fit.svg");

Runnable: examples/modules/plot_demo.j.

Surface

CallReturnsNotes
plot.defaults()OptionsA 640x400 canvas, blue on white, sans-serif 11px, grid + top-right legend + tooltips on.
plot.series(name, xs, ys)SeriesA line series (set .mark / .color / .dash / .yErr / .shape).
plot.points(name, xs, ys)SeriesA points (scatter) series.
plot.chart(series, opts)stringRender a list of Series on shared axes, with a legend for named series.
plot.line(xs, ys, opts)stringOne line series in opts.color.
plot.scatter(xs, ys, opts)stringOne points series in opts.color.
plot.bar(labels, values, opts)stringA single-series bar chart, one bar per category (negatives diverge).
plot.bars(labels, series, opts)stringA multi-series bar chart - grouped or stacked per opts.barMode.
plot.histogram(data, bins, opts)stringA histogram of data into bins equal-width buckets.
plot.hline(value, label)RefLineA horizontal reference line (append to opts.refLines).
plot.vline(value, label)RefLineA vertical reference line.
plot.floats(xs)list of floatLift a list of int to list of float.
plot.save(svg, path)stringWrite an SVG string to a file with fs; returns the path.

Chart data is list of float (use plot.floats or float literals). Each render returns a complete SVG document. Empty / mismatched input, a ragged bars series, a non-positive value on a log axis, an unknown mark / shape (in chart) or legendPos / barMode, a width / height or fontSize below 1, a negative margin, margins that leave no plot area, or histogram bins outside 1 to 10000 throw a catchable Error{kind: "plot"}. Jennifer values have no methods, so saving is plot.save($svg, path), not $svg.save(path).

The output is safe to build from untrusted data: text is XML-escaped, and every caller-supplied attribute value (color / background / fontFamily, series and ref-line colours) is attribute-escaped, so a hostile string cannot break out and inject markup. Per-mark tooltip names are truncated to keep output bounded, and data magnitudes past the int range (>= 2^63) render in scientific notation rather than failing.

Series

Build with plot.series (line) or plot.points, then set the fields.

FieldType
namestringthe legend label ("" = not listed)
xslist of floatthe x coordinates (unused by bars)
yslist of floatthe y coordinates / bar values
colorstringthe series colour ("" = a palette colour)
markstring"line", "points", "both" or "area"
dashbooldraw the line dashed
yErrlist of floatsymmetric error bars when the same length as ys (linear y axis only; [] = none)
shapestringthe scatter marker: "circle", "square", "triangle" or "diamond"

Options

Copy plot.defaults() and set the fields you want.

FieldDefault
width, height640, 400canvas size in px
title, xLabel, yLabel""captions ("" = none; yLabel is rotated)
color"#3366cc"the default data colour (single-series line / scatter / bar)
background"#ffffff"the canvas background
fontFamily, fontSize"sans-serif", 11CSS font family; base tick-label size (title and captions scale off it)
gridtruegridlines (false = short tick marks)
legend, legendPostrue, "top-right"legend on/off; "top-right" / "top-left" / "bottom-right" / "bottom-left"
xLog, yLogfalselog10 scale, decade ticks, positive values only
xDate, dateFormatfalse, ""x values are Unix seconds, labelled as dates with calendar-boundary ticks; strftime pattern ("" = chosen by tick spacing)
marginLeft, marginRight, marginTop, marginBottom55, 18, 38, 46margins in px
barMode"grouped""grouped" or "stacked" for a multi-series bars; negative values diverge below a zero baseline
barLabelsfalsedraw each grouped bar's value
hovertrueattach a <title> to every mark for native browser hover text
refLines[]a list of RefLine from hline / vline

Scope

Data charts over math for the axis math. Not supported: a secondary y-axis, per-series point size, and log bases other than 10. For graph / network diagrams (nodes and edges) rather than data charts, see dot.

Academy talks

TalkWhat it shows
Numbers and data: decimal, stats, linalg, ml, plot and dotline and grouped bar charts as SVG
Sequencing data with Jenniferplotting a Sanger trace as SVG

API reference

Every exported function, struct and constant of plot, with its signature, generated from the module's doc comments: the plot API reference.