packages

plot::axes

On this page

The Axes builder: one plot area with its layers, axes, labels and legend. Every method returns a changed copy, so calls chain; layer methods (line, scatter, ...) add a layer, and style methods (label, color, width, ...) change the last one.

Structs

AxisSource

pub struct Axis

One axis of an Axes.

nabi
let ax = plot::axes().xlabel("t").xlim(0.0, 10.0);
assert((try ax.svg()).contains(">t<"));

AxesSource

pub struct Axes

A plot area: layers drawn against an x axis and a left (and maybe a right) y axis, with a title, a legend and a colorbar. Build it with plot::axes() or start from a layer function such as plot::line; render it with svg, png, term or save, or place it in a figure with | (side by side) and / (stacked).

nabi
let x = plot::linspace(0.0, 6.28, 50);
let y = x.iter().map(|v| v.sin()).collect::<List<f64>>();
let ax = plot::axes()
    .line(x, y).label("sin")
    .xlabel("t [s]").ylabel("$\\alpha$")
    .title("A sine");
let svg = try ax.svg();
assert(svg.starts_with("<svg") && svg.contains(">sin<"));
  • pub fn len(self) -> usize§

    Source

    The number of layers.

    nabi
    assert(plot::line([0, 1], [1, 0]).scatter([0, 1], [0, 1]).len() == 2);
    
  • pub fn line<X: Data, Y: Data>(self, x: X, y: Y) -> Axes§

    Source

    A line through the points, in order. NaN values break the line.

    nabi
    let svg = try plot::axes().line([0, 1, 2], [1.0, 3.0, 2.0]).svg();
    assert(svg.contains("<path"));
    
  • pub fn line_y<Y: Data>(self, y: Y) -> Axes§

    Source

    A line against the index: y[i] at x = i.

    nabi
    assert((try plot::axes().line_y([3.0, 1.0, 2.0]).svg()).contains("<path"));
    
  • pub fn step<X: Data, Y: Data>(self, x: X, y: Y, at: StepAt = StepAt::Post) -> Axes§

    Source

    A step line: the value changes at each x as at says.

    nabi
    use plot::StepAt;
    
    assert((try plot::axes().step([0, 1, 2], [1, 3, 2], StepAt::Post).svg()).contains("<path"));
    
  • pub fn scatter<X: Data, Y: Data>(self, x: X, y: Y) -> Axes§

    Source

    Markers at the points. Chain sizes or values to size or colour each by a value.

    nabi
    let svg = try plot::axes().scatter([1, 2, 3], [3, 1, 2]).svg();
    assert(svg.contains("<circle"));
    
  • pub fn bar<H: Data>(self, labels: List<str>, heights: H) -> Axes§

    Source

    Bars of heights at categories labels, one per label.

    nabi
    let svg = try plot::axes().bar(["a", "b", "c"], [3, 5, 2]).svg();
    assert(svg.contains(">b<"));
    
  • pub fn bar_at<X: Data, H: Data>(self, x: X, heights: H, width: f64 = 0.8) -> Axes§

    Source

    Bars of heights at numeric positions x, width wide in x units.

    nabi
    assert((try plot::axes().bar_at([1.0, 2.5], [3, 4], width: 0.5).svg()).contains("<path"));
    
  • pub fn barh<H: Data>(self, labels: List<str>, widths: H) -> Axes§

    Source

    Horizontal bars of widths at categories labels up the y axis.

    nabi
    assert((try plot::axes().barh(["low", "high"], [1, 3]).svg()).contains(">high<"));
    
  • pub fn bars(self, labels: List<str>, series: List<(str, List<f64>)>, stacked: bool = false) -> Axes§

    Source

    Several series of bars over the same categories, side by side (grouped) or on top of each other (stacked). Each series is a layer with its name as the legend label.

    nabi
    let ax = plot::axes().bars(["2023", "2024"], [("north", [3.0, 4.0]), ("south", [2.0, 5.0])], stacked: true);
    assert((try ax.svg()).contains(">south<"));
    
  • pub fn hist<D: Data>(self, data: D, bins: Bins = Bins::Auto, density: bool = false) -> Axes§

    Source

    A histogram of data in bins (NumPy's rules; Bins::Auto by default). density scales the bars to unit area.

    nabi
    use plot::Bins;
    
    let svg = try plot::axes().hist([1.0, 2.0, 2.5, 3.0, 3.1, 3.2], Bins::Count(3)).svg();
    assert(svg.contains("<path"));
    
  • pub fn fill_between<X: Data, A: Data, B: Data>(self, x: X, y1: A, y2: B) -> Axes§

    Source

    The region between y1 and y2 over x (fill_between).

    nabi
    assert((try plot::axes().fill_between([0, 1, 2], [0, 1, 0], [1, 2, 1]).svg()).contains("<path"));
    
  • pub fn area<X: Data, Y: Data>(self, x: X, y: Y) -> Axes§

    Source

    The area between y and zero, with the curve drawn on top.

    nabi
    assert((try plot::axes().area([0, 1, 2], [1, 3, 2]).svg()).contains("fill-opacity"));
    
  • pub fn errorbar<X: Data, Y: Data, E: Data>(self, x: X, y: Y, yerr: E) -> Axes§

    Source

    Error bars: yerr symmetric about y.

    nabi
    assert((try plot::axes().errorbar([1, 2], [2.0, 3.0], [0.5, 0.2]).svg()).contains("<path"));
    
  • pub fn errorbar_xy<X: Data, Y: Data>(self, x: X, y: Y, ylo: List<f64>, yhi: List<f64>, xlo: List<f64> = [], xhi: List<f64> = []) -> Axes§

    Source

    Error bars with separate lower and upper extents in y and optional extents in x (empty lists for none).

    nabi
    let ax = plot::axes().errorbar_xy([1, 2], [2.0, 3.0], [0.2, 0.3], [0.5, 0.1], xlo: [0.1, 0.1], xhi: [0.2, 0.2]);
    assert((try ax.svg()).contains("<path"));
    
  • pub fn boxplot(self, groups: List<List<f64>>, labels: List<str> = []) -> Axes§

    Source

    Box plots of groups of values, at categories labels (or 1, 2, ... when empty): quartile boxes, median lines, whiskers to 1.5 IQR and the outliers beyond.

    nabi
    let ax = plot::axes().boxplot([[1.0, 2.0, 3.0, 4.0], [2.0, 3.0, 9.0]], ["a", "b"]);
    assert((try ax.svg()).contains(">a<"));
    
  • pub fn violin(self, groups: List<List<f64>>, labels: List<str> = []) -> Axes§

    Source

    Violin plots: each group's kernel density (Gaussian, Scott's bandwidth) mirrored about its category, with a median tick.

    nabi
    let ax = plot::axes().violin([[1.0, 2.0, 2.5, 3.0], [2.0, 4.0, 4.5]]);
    assert((try ax.svg()).contains("<path"));
    
  • pub fn heatmap<G: Grid>(self, z: G, extent: (f64, f64, f64, f64) | none = none) -> Axes§

    Source

    A heatmap of a grid: cell (i, j) coloured by z[i][j], column j at x from 0, row i at y from 0 (rows go up). extent sets the covered rectangle (x0, x1, y0, y1) instead. Chain cmap, vmin/vmax and colorbar.

    nabi
    let svg = try plot::axes().heatmap([[1.0, 2.0], [3.0, 4.0]]).colorbar("").svg();
    assert(svg.contains("data:image/png;base64,"));
    
  • pub fn imshow<G: Grid>(self, z: G) -> Axes§

    Source

    Like heatmap with row 0 at the top, as an image is stored.

    nabi
    assert((try plot::axes().imshow([[0.0, 1.0], [1.0, 0.0]]).svg()).contains("<image"));
    
  • pub fn contour<G: Grid>(self, z: G, levels: List<f64> = [], extent: (f64, f64, f64, f64) | none = none) -> Axes§

    Source

    Contour lines of a grid at levels (nice values over the data range when empty), over extent as for heatmap (grid points at integer positions by default).

    nabi
    let z = [[0.0, 1.0, 0.0], [1.0, 2.0, 1.0], [0.0, 1.0, 0.0]];
    assert((try plot::axes().contour(z, [0.5, 1.5]).svg()).contains("<path"));
    
  • pub fn contourf<G: Grid>(self, z: G, levels: List<f64> = [], extent: (f64, f64, f64, f64) | none = none) -> Axes§

    Source

    Filled contour bands of a grid between levels.

    nabi
    let z = [[0.0, 1.0, 0.0], [1.0, 2.0, 1.0], [0.0, 1.0, 0.0]];
    assert((try plot::axes().contourf(z, [0.0, 1.0, 2.0]).colorbar().svg()).contains("<path"));
    
  • pub fn quiver<X: Data, Y: Data, U: Data, V: Data>(self, x: X, y: Y, u: U, v: V, scale: f64 = 0.0) -> Axes§

    Source

    Arrows from (x, y) along (u, v). scale multiplies the vectors (data units per vector unit); 0 fits the longest arrow to the spacing of the points.

    nabi
    assert((try plot::axes().quiver([0, 1], [0, 0], [1, 0], [0, 1]).svg()).contains("<path"));
    
  • pub fn text(self, x: f64, y: f64, s: str) -> Axes§

    Source

    Text at a data position, starting there (math markup allowed).

    nabi
    assert((try plot::axes().line([0, 1], [0, 1]).text(0.5, 0.5, "mid").svg()).contains(">mid<"));
    
  • pub fn annotate(self, s: str, x: f64, y: f64, tx: f64, ty: f64) -> Axes§

    Source

    Text at (tx, ty) with an arrow pointing at (x, y).

    nabi
    assert((try plot::line([0, 1, 2], [0, 2, 1]).annotate("top", 1.0, 2.0, 1.5, 2.2).svg()).contains(">top<"));
    
  • pub fn hline(self, y: f64) -> Axes§

    Source

    A horizontal line across the plot at y.

    nabi
    assert((try plot::line([0, 1], [0, 2]).hline(1.0).dash(plot::Dash::Dashed).svg()).contains("stroke-dasharray"));
    
  • pub fn vline(self, x: f64) -> Axes§

    Source

    A vertical line across the plot at x.

    nabi
    assert((try plot::line([0, 1], [0, 2]).vline(0.5).svg()).contains("<path"));
    
  • pub fn hspan(self, y0: f64, y1: f64) -> Axes§

    Source

    A horizontal band between y0 and y1.

    nabi
    assert((try plot::line([0, 1], [0, 2]).hspan(0.5, 1.0).svg()).contains("fill-opacity"));
    
  • pub fn vspan(self, x0: f64, x1: f64) -> Axes§

    Source

    A vertical band between x0 and x1.

    nabi
    assert((try plot::line([0, 1], [0, 2]).vspan(0.2, 0.4).color("C3").svg()).contains("fill-opacity"));
    
  • pub fn label(self, s: str) -> Axes§

    Source

    The last layer's legend label.

    nabi
    assert((try plot::line([0, 1], [0, 1]).label("data").svg()).contains(">data<"));
    
  • pub fn color(self, c: ColorSpec) -> Axes§

    Source

    The last layer's colour: a Color, "#rrggbb", a name, or "C0" to "C9" for the theme's cycle.

    nabi
    assert((try plot::line([0, 1], [0, 1]).color("#d55e00").svg()).contains("#d55e00"));
    
  • pub fn edge(self, c: ColorSpec) -> Axes§

    Source

    The last layer's edge colour (markers, bars, boxes).

    nabi
    assert((try plot::scatter([0, 1], [0, 1]).edge("black").svg()).contains("stroke=\"#000000\""));
    
  • pub fn alpha(self, a: f64) -> Axes§

    Source

    The last layer's opacity, 0 to 1.

    nabi
    assert((try plot::line([0, 1], [0, 1]).alpha(0.5).svg()).contains("stroke-opacity=\"0.5\""));
    
  • pub fn width(self, w: f64) -> Axes§

    Source

    The last layer's line width in px.

    nabi
    assert((try plot::line([0, 1], [0, 1]).width(3.0).svg()).contains("stroke-width=\"3\""));
    
  • pub fn dash(self, d: Dash) -> Axes§

    Source

    The last layer's dash pattern.

    nabi
    assert((try plot::line([0, 1], [0, 1]).dash(plot::Dash::Dotted).svg()).contains("stroke-dasharray"));
    
  • pub fn marker(self, m: Marker) -> Axes§

    Source

    The last layer's marker; on a line it marks every point.

    nabi
    use plot::Marker;
    
    assert((try plot::line([0, 1], [0, 1]).marker(Marker::Square).svg()).contains("Z"));
    
  • pub fn size(self, px: f64) -> Axes§

    Source

    The last layer's marker size in px (diameter), or its text size.

    nabi
    assert((try plot::scatter([0, 1], [0, 1]).size(10.0).svg()).contains("r=\"5\""));
    
  • pub fn sizes<S: Data>(self, s: S) -> Axes§

    Source

    Per-point marker sizes (diameters in px) for the last scatter.

    nabi
    assert((try plot::scatter([0, 1], [0, 1]).sizes([4.0, 12.0]).svg()).contains("r=\"6\""));
    
  • pub fn values<V: Data>(self, c: V) -> Axes§

    Source

    Per-point values for the last scatter, coloured through the colormap (cmap, the theme's by default).

    nabi
    let ax = plot::scatter([0, 1, 2], [0, 1, 2]).values([0.0, 0.5, 1.0]).colorbar("v");
    assert((try ax.svg()).contains("#fde725"));
    
  • pub fn cmap(self, m: Colormap) -> Axes§

    Source

    The colormap of the last layer (heatmap, contour, coloured scatter).

    nabi
    assert((try plot::heatmap([[0.0, 1.0]]).cmap(plot::Colormap::gray()).svg()).contains("<image"));
    
  • pub fn range(self, vmin: f64, vmax: f64) -> Axes§

    Source

    The value range the colormap spans for the last layer (data range by default).

    nabi
    assert((try plot::heatmap([[0.0, 1.0]]).range(-1.0, 1.0).colorbar("").svg()).contains("\u{2212}1"));
    
  • pub fn xlabel(self, s: str) -> Axes§

    Source

    The x axis title.

    nabi
    assert((try plot::line([0, 1], [0, 1]).xlabel("time [s]").svg()).contains(">time [s]<"));
    
  • pub fn ylabel(self, s: str) -> Axes§

    Source

    The y axis title (the right axis after right()).

    nabi
    assert((try plot::line([0, 1], [0, 1]).ylabel("$\\Delta p$").svg()).contains("\u{394}p"));
    
  • pub fn title(self, s: str) -> Axes§

    Source

    The title above the plot.

    nabi
    assert((try plot::line([0, 1], [0, 1]).title("Ramp").svg()).contains(">Ramp<"));
    
  • pub fn xlim(self, lo: f64, hi: f64) -> Axes§

    Source

    The x view limits; NaN leaves one automatic.

    nabi
    assert((try plot::line([0, 1], [0, 1]).xlim(0.0, 4.0).svg()).contains(">4<"));
    
  • pub fn ylim(self, lo: f64, hi: f64) -> Axes§

    Source

    The y view limits (the right axis after right()).

    nabi
    assert((try plot::line([0, 1], [0, 1]).ylim(f64::NAN, 3.0).svg()).contains(">3.0<"));
    
  • pub fn xscale(self, s: Scale) -> Axes§

    Source

    The x axis scale.

    nabi
    use plot::Scale;
    
    let svg = try plot::axes().line([1, 10, 100], [1, 2, 3]).xscale(Scale::Log(10.0)).svg();
    assert(svg.contains(">10<") && svg.contains("<tspan"));
    
  • pub fn yscale(self, s: Scale) -> Axes§

    Source

    The y axis scale (the right axis after right()).

    nabi
    use plot::Scale;
    
    let ax = plot::line([0, 1, 2], [1, 100, 10000]).yscale(Scale::Log(10.0));
    assert((try ax.svg()).contains(">4</tspan>"));
    
  • pub fn log(self, axes: str = "y") -> Axes§

    Source

    Log scales in base 10: "x", "y" or "xy".

    nabi
    assert((try plot::line([1, 10, 100], [1, 10, 100]).log("xy").svg()).contains(">2</tspan>"));
    
  • pub fn dates(self) -> Axes§

    Source

    A date x axis: x values are seconds since 1970-01-01 UTC (plot::date(2024, 3, 1)).

    nabi
    let t = [plot::date(2024, 1, 1), plot::date(2024, 6, 1), plot::date(2024, 12, 1)];
    assert((try plot::axes().line(t, [1, 3, 2]).dates().svg()).contains("2024-"));
    
  • pub fn xformat(self, f: Format) -> Axes§

    Source

    The x tick label format.

    nabi
    use plot::Format;
    
    assert((try plot::axes().line([0, 2000], [0, 1]).xformat(Format::Si).svg()).contains(">1.5k<"));
    
  • pub fn yformat(self, f: Format) -> Axes§

    Source

    The y tick label format (the right axis after right()).

    nabi
    use plot::Format;
    
    assert((try plot::line([0, 1], [0.0, 0.5]).yformat(Format::Percent).svg()).contains(">50%<"));
    
  • pub fn xticks(self, at: List<f64>, labels: List<str> | none = none) -> Axes§

    Source

    Fixed x ticks, labelled by the format (or by labels).

    nabi
    assert((try plot::line([0, 2], [0, 1]).xticks([0.0, 1.0, 2.0], ["lo", "mid", "hi"]).svg()).contains(">mid<"));
    
  • pub fn yticks(self, at: List<f64>, labels: List<str> | none = none) -> Axes§

    Source

    Fixed y ticks (the right axis after right()).

    nabi
    assert((try plot::line([0, 2], [0, 1]).yticks([0.0, 1.0]).svg()).contains(">1<"));
    
  • pub fn minor(self) -> Axes§

    Source

    Minor ticks on both axes.

    nabi
    assert((try plot::line([0, 1], [0, 1]).minor().svg()).contains("stroke-width=\"0.75\""));
    
  • pub fn grid(self, on: bool = true) -> Axes§

    Source

    Grid lines on (or off) for both axes.

    nabi
    assert(!(try plot::line([0, 1], [0, 1]).grid(false).svg()).contains("#e9e4e6"));
    
  • pub fn with_margins(self, m: f64) -> Axes§

    Source

    The fraction of the data range added on each side (0.05 by default).

    nabi
    assert((try plot::line([0, 1], [0, 1]).with_margins(0.0).svg()).contains("<path"));
    
  • pub fn invert_x(self) -> Axes§

    Source

    Flips the x axis.

    nabi
    assert((try plot::line([0, 1], [0, 1]).invert_x().svg()).contains("<path"));
    
  • pub fn invert_y(self) -> Axes§

    Source

    Flips the y axis.

    nabi
    assert((try plot::line([0, 1], [0, 1]).invert_y().svg()).contains("<path"));
    
  • pub fn aspect(self, r: f64) -> Axes§

    Source

    Fixes y units per x unit on screen: 1.0 for equal scales.

    nabi
    assert((try plot::line([0, 1], [0, 1]).aspect(1.0).svg()).contains("<path"));
    
  • pub fn right(self) -> Axes§

    Source

    Later layers and ylabel, ylim, yscale, ... go to a second y axis on the right (a twin axis).

    nabi
    let ax = plot::axes().line([0, 1], [0, 1]).ylabel("left").right().line([0, 1], [100, 50]).ylabel("right");
    assert((try ax.svg()).contains(">right<"));
    
  • pub fn left(self) -> Axes§

    Source

    Later layers go to the left y axis again.

    nabi
    let ax = plot::line([0, 1], [0, 1]).right().line([0, 1], [9, 5]).left().ylabel("left side");
    assert((try ax.svg()).contains(">left side<"));
    
  • pub fn legend(self, loc: Loc = Loc::Best) -> Axes§

    Source

    A legend at loc (Loc::Best by default). Layers with a label get one automatically; this places it, or turns it on for none.

    nabi
    use plot::Loc;
    
    assert((try plot::line([0, 1], [0, 1]).label("a").legend(Loc::LowerRight).svg()).contains(">a<"));
    
  • pub fn no_legend(self) -> Axes§

    Source

    No legend.

    nabi
    assert(!(try plot::line([0, 1], [0, 1]).label("a").no_legend().svg()).contains(">a<"));
    
  • pub fn colorbar(self, label: str = "") -> Axes§

    Source

    A colorbar for the last coloured layer, titled label.

    nabi
    assert((try plot::heatmap([[1.0, 2.0]]).colorbar("level").svg()).contains(">level<"));
    
  • pub fn theme(self, t: Theme) -> Axes§

    Source

    The theme when this Axes is rendered on its own.

    nabi
    assert((try plot::line([0, 1], [0, 1]).theme(plot::Theme::dark()).svg()).contains("#141114"));
    
  • pub fn size_px(self, w: f64, h: f64) -> Axes§

    Source

    The figure size in px when rendered on its own.

    nabi
    assert((try plot::line([0, 1], [0, 1]).size_px(300.0, 200.0).svg()).contains("width=\"300\""));
    
  • pub fn figure(self) -> Figure§

    Source

    This Axes as a one-panel figure.

    nabi
    assert(plot::line([0, 1], [0, 1]).figure().panels().len() == 1);
    
  • pub fn svg(self) throws (PlotError) -> str§

    Source

    The SVG document.

    nabi
    assert((try plot::line([0, 1], [0, 1]).svg()).ends_with("</svg>\n"));
    
  • pub fn png(self, scale: f64 = 1.0) throws (PlotError) -> List<u8>§

    Source

    A PNG image; scale multiplies the pixel size (2 for HiDPI).

    nabi
    assert((try plot::line([0, 1], [0, 1]).size_px(40.0, 30.0).png())[1] == 80);
    
  • pub fn term(self, cols: usize = 80, rows: usize = 24, color: bool = false) throws (PlotError) -> str§

    Source

    A text rendering for a terminal, cols x rows characters.

    nabi
    assert((try plot::line([0, 1], [0, 1]).term(cols: 30, rows: 8)).lines().count() == 8);
    
  • pub fn save(self, path: str) throws (PlotError)§

    Source

    Writes an .svg or .png file, by the extension.

    nabi
    use std::{env, fs, random};
    
    let path = env::temp_dir() / f"plot_{random::range(0u64..u64::MAX)}.svg";
    try plot::line([0, 1], [0, 1]).save(f"{path}");
    assert((try fs::read_to_str(path)).starts_with("<svg"));
    try fs::remove_file(path);
    
  • pub fn show(self) throws (PlotError)§

    Source

    Prints the terminal rendering.

    nabi
    try plot::line([0, 1], [0, 1]).show();
    
Trait implementations 2
  • impl RichDisplay for Axes
  • impl ops::Add<Axes> for Axes

Functions

axesSource

pub fn axes() -> Axes

An empty Axes.

nabi
assert((try plot::axes().svg()).contains("</svg>"));