Plotting
The built-in plot module draws charts. A chart is a value: you build it with plot functions,
store or return it like any other value, and show it with print. In the
playground a printed chart appears in the Output section of Results, in order
with printed text. The command line saves each printed chart as an
SVG file.
import plot
xs = [1, 2, 3, 4, 5]squares = [1, 4, 9, 16, 25]chart = plot.figure( [plot.line(xs, squares, label="n²"), plot.scatter(xs, xs, label="n")], title="Squares", xlabel="n",)print(chart)As text, for example in copied output, a chart prints as a short description such as
<figure "Squares", 2 series>.
Series
Section titled “Series”Each function returns one series. Printing a series on its own shows it as a chart with default settings.
| Function | Draws |
|---|---|
plot.line(x, y, label="", color="") |
A line through the points (x[i], y[i]), in list order. |
plot.scatter(x, y, label="", color="") |
A dot at each point (x[i], y[i]). |
plot.bar(x, height, label="", color="") |
One bar per category in x, with the matching height. |
plot.hist(x, bins=10, label="", color="") |
Counts of the values in x, in equal-width bins. |
x,y, andheightare lists or tuples of numbers.xandymust have the same length.- Bar categories in
xare strings or numbers. binsis a whole number from 1 to 1000. Bins span the smallest to the largest value.labelnames the series in hover tooltips and, when a figure has several series, in its legend. A figure with one series is named by its title instead.coloris one of the palette names"blue","orange","teal","gold","pink","green","purple", and"red", which adapt to light and dark themes, or a fixed colour:"#rrggbb","#rgb", or an(r, g, b)tuple of whole numbers from 0 to 255. Without one, series take the palette’s colours in order.
Figures
Section titled “Figures”plot.figure(series, title="", xlabel="", ylabel="", xlim=(), ylim=(), legend=True, grid=True)
combines one series, or a list of series, into a chart.
| Parameter | Meaning |
|---|---|
title |
The chart’s heading. |
xlabel, ylabel |
Axis titles. |
xlim, ylim |
(low, high) to fix an axis range. () fits the axis to the data. |
legend |
Show a legend for labelled series when the figure has several. |
grid |
Draw light grid lines behind the data. |
Several bar series in one figure appear as grouped bars and must share the same categories. Bars cannot share a figure with lines, dots, or histograms. A figure holds at most 8 series and 10,000 points in total. A very narrow axis range may show only its endpoints as tick labels.
import plot
months = ["Jun", "Jul", "Aug"]print( plot.figure( [plot.bar(months, [120, 340, 260], label="2024"), plot.bar(months, [90, 410, 300], label="2025")], title="Rainfall", ylabel="mm", ))Charts from commit loops
Section titled “Charts from commit loops”Charts are ordinary values, so a commit loop can collect data and a function can return the chart. A
print inside a loop round follows the usual transaction rules: the
chart appears only when its round commits.
import plot
def halving(start): init: step = 0 value = start steps = [] values = [] while value >= 1: step' = step + 1 value' = value / 2 steps' = steps + [step] values' = values + [value] return plot.figure(plot.line(steps, values), title="Halving", xlabel="step")
print(halving(64))Names and imports
Section titled “Names and imports”import plot uses the built-in module only when there is no plot.nlk file next to the importing
file. from plot import line, figure and aliases such as import plot as p work as for other
modules.