plot::axes
On this page
- Structs
Axes71alphaannotateareaaspectbarbar_atbarhbarsboxplotcmapcolorcolorbarcontourcontourfdashdatesedgeerrorbarerrorbar_xyfigurefill_betweengridheatmaphisthlinehspanimshowinvert_xinvert_ylabelleftlegendlenlineline_ylogmarkerminorno_legendpngquiverrangerightsavescattershowsizesize_pxsizesstepsvgtermtextthemetitlevaluesviolinvlinevspanwidthwith_marginsxformatxlabelxlimxscalexticksyformatylabelylimyscaleyticks
- Functions
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 AxisOne axis of an Axes.
let ax = plot::axes().xlabel("t").xlim(0.0, 10.0);
assert((try ax.svg()).contains(">t<"));
AxesSource
pub struct AxesA 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).
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<"));
The number of layers.
nabiassert(plot::line([0, 1], [1, 0]).scatter([0, 1], [0, 1]).len() == 2);A line through the points, in order. NaN values break the line.
nabilet svg = try plot::axes().line([0, 1, 2], [1.0, 3.0, 2.0]).svg(); assert(svg.contains("<path"));A line against the index:
y[i]atx = i.nabiassert((try plot::axes().line_y([3.0, 1.0, 2.0]).svg()).contains("<path"));A step line: the value changes at each
xasatsays.nabiuse plot::StepAt; assert((try plot::axes().step([0, 1, 2], [1, 3, 2], StepAt::Post).svg()).contains("<path"));Markers at the points. Chain
sizesorvaluesto size or colour each by a value.nabilet svg = try plot::axes().scatter([1, 2, 3], [3, 1, 2]).svg(); assert(svg.contains("<circle"));Bars of
heightsat categorieslabels, one per label.nabilet svg = try plot::axes().bar(["a", "b", "c"], [3, 5, 2]).svg(); assert(svg.contains(">b<"));Bars of
heightsat numeric positionsx,widthwide in x units.nabiassert((try plot::axes().bar_at([1.0, 2.5], [3, 4], width: 0.5).svg()).contains("<path"));Horizontal bars of
widthsat categorieslabelsup the y axis.nabiassert((try plot::axes().barh(["low", "high"], [1, 3]).svg()).contains(">high<"));
Sourcepub fn bars(§self, labels: List<str>, series: List<(str, List<f64>)>, stacked: bool = false) -> Axes 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.nabilet ax = plot::axes().bars(["2023", "2024"], [("north", [3.0, 4.0]), ("south", [2.0, 5.0])], stacked: true); assert((try ax.svg()).contains(">south<"));A histogram of
datainbins(NumPy's rules;Bins::Autoby default).densityscales the bars to unit area.nabiuse 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"));The region between
y1andy2overx(fill_between).nabiassert((try plot::axes().fill_between([0, 1, 2], [0, 1, 0], [1, 2, 1]).svg()).contains("<path"));The area between
yand zero, with the curve drawn on top.nabiassert((try plot::axes().area([0, 1, 2], [1, 3, 2]).svg()).contains("fill-opacity"));Error bars:
yerrsymmetric abouty.nabiassert((try plot::axes().errorbar([1, 2], [2.0, 3.0], [0.5, 0.2]).svg()).contains("<path"));
Sourcepub 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 Error bars with separate lower and upper extents in y and optional extents in x (empty lists for none).
nabilet 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"));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.nabilet 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<"));Violin plots: each group's kernel density (Gaussian, Scott's bandwidth) mirrored about its category, with a median tick.
nabilet ax = plot::axes().violin([[1.0, 2.0, 2.5, 3.0], [2.0, 4.0, 4.5]]); assert((try ax.svg()).contains("<path"));A heatmap of a grid: cell
(i, j)coloured byz[i][j], columnjat x from 0, rowiat y from 0 (rows go up).extentsets the covered rectangle(x0, x1, y0, y1)instead. Chaincmap,vmin/vmaxandcolorbar.nabilet svg = try plot::axes().heatmap([[1.0, 2.0], [3.0, 4.0]]).colorbar("").svg(); assert(svg.contains("data:image/png;base64,"));Like
heatmapwith row 0 at the top, as an image is stored.nabiassert((try plot::axes().imshow([[0.0, 1.0], [1.0, 0.0]]).svg()).contains("<image"));
Sourcepub fn contour<G: Grid>(§self, z: G, levels: List<f64> = [], extent: (f64, f64, f64, f64) | none = none) -> Axes Contour lines of a grid at
levels(nice values over the data range when empty), overextentas forheatmap(grid points at integer positions by default).nabilet 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"));
Sourcepub fn contourf<G: Grid>(§self, z: G, levels: List<f64> = [], extent: (f64, f64, f64, f64) | none = none) -> Axes Filled contour bands of a grid between
levels.nabilet 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"));
Sourcepub fn quiver<X: Data, Y: Data, U: Data, V: Data>(§self, x: X, y: Y, u: U, v: V, scale: f64 = 0.0) -> Axes Arrows from
(x, y)along(u, v).scalemultiplies the vectors (data units per vector unit); 0 fits the longest arrow to the spacing of the points.nabiassert((try plot::axes().quiver([0, 1], [0, 0], [1, 0], [0, 1]).svg()).contains("<path"));Text at a data position, starting there (math markup allowed).
nabiassert((try plot::axes().line([0, 1], [0, 1]).text(0.5, 0.5, "mid").svg()).contains(">mid<"));Text at
(tx, ty)with an arrow pointing at(x, y).nabiassert((try plot::line([0, 1, 2], [0, 2, 1]).annotate("top", 1.0, 2.0, 1.5, 2.2).svg()).contains(">top<"));A horizontal line across the plot at
y.nabiassert((try plot::line([0, 1], [0, 2]).hline(1.0).dash(plot::Dash::Dashed).svg()).contains("stroke-dasharray"));A vertical line across the plot at
x.nabiassert((try plot::line([0, 1], [0, 2]).vline(0.5).svg()).contains("<path"));A horizontal band between
y0andy1.nabiassert((try plot::line([0, 1], [0, 2]).hspan(0.5, 1.0).svg()).contains("fill-opacity"));A vertical band between
x0andx1.nabiassert((try plot::line([0, 1], [0, 2]).vspan(0.2, 0.4).color("C3").svg()).contains("fill-opacity"));The last layer's legend label.
nabiassert((try plot::line([0, 1], [0, 1]).label("data").svg()).contains(">data<"));The last layer's colour: a
Color,"#rrggbb", a name, or"C0"to"C9"for the theme's cycle.nabiassert((try plot::line([0, 1], [0, 1]).color("#d55e00").svg()).contains("#d55e00"));The last layer's edge colour (markers, bars, boxes).
nabiassert((try plot::scatter([0, 1], [0, 1]).edge("black").svg()).contains("stroke=\"#000000\""));The last layer's opacity, 0 to 1.
nabiassert((try plot::line([0, 1], [0, 1]).alpha(0.5).svg()).contains("stroke-opacity=\"0.5\""));The last layer's line width in px.
nabiassert((try plot::line([0, 1], [0, 1]).width(3.0).svg()).contains("stroke-width=\"3\""));The last layer's dash pattern.
nabiassert((try plot::line([0, 1], [0, 1]).dash(plot::Dash::Dotted).svg()).contains("stroke-dasharray"));The last layer's marker; on a line it marks every point.
nabiuse plot::Marker; assert((try plot::line([0, 1], [0, 1]).marker(Marker::Square).svg()).contains("Z"));The last layer's marker size in px (diameter), or its text size.
nabiassert((try plot::scatter([0, 1], [0, 1]).size(10.0).svg()).contains("r=\"5\""));Per-point marker sizes (diameters in px) for the last scatter.
nabiassert((try plot::scatter([0, 1], [0, 1]).sizes([4.0, 12.0]).svg()).contains("r=\"6\""));Per-point values for the last scatter, coloured through the colormap (
cmap, the theme's by default).nabilet ax = plot::scatter([0, 1, 2], [0, 1, 2]).values([0.0, 0.5, 1.0]).colorbar("v"); assert((try ax.svg()).contains("#fde725"));The colormap of the last layer (heatmap, contour, coloured scatter).
nabiassert((try plot::heatmap([[0.0, 1.0]]).cmap(plot::Colormap::gray()).svg()).contains("<image"));The value range the colormap spans for the last layer (data range by default).
nabiassert((try plot::heatmap([[0.0, 1.0]]).range(-1.0, 1.0).colorbar("").svg()).contains("\u{2212}1"));The x axis title.
nabiassert((try plot::line([0, 1], [0, 1]).xlabel("time [s]").svg()).contains(">time [s]<"));The y axis title (the right axis after
right()).nabiassert((try plot::line([0, 1], [0, 1]).ylabel("$\\Delta p$").svg()).contains("\u{394}p"));The title above the plot.
nabiassert((try plot::line([0, 1], [0, 1]).title("Ramp").svg()).contains(">Ramp<"));The x view limits; NaN leaves one automatic.
nabiassert((try plot::line([0, 1], [0, 1]).xlim(0.0, 4.0).svg()).contains(">4<"));The y view limits (the right axis after
right()).nabiassert((try plot::line([0, 1], [0, 1]).ylim(f64::NAN, 3.0).svg()).contains(">3.0<"));The x axis scale.
nabiuse 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"));The y axis scale (the right axis after
right()).nabiuse plot::Scale; let ax = plot::line([0, 1, 2], [1, 100, 10000]).yscale(Scale::Log(10.0)); assert((try ax.svg()).contains(">4</tspan>"));Log scales in base 10:
"x","y"or"xy".nabiassert((try plot::line([1, 10, 100], [1, 10, 100]).log("xy").svg()).contains(">2</tspan>"));A date x axis: x values are seconds since 1970-01-01 UTC (
plot::date(2024, 3, 1)).nabilet 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-"));The x tick label format.
nabiuse plot::Format; assert((try plot::axes().line([0, 2000], [0, 1]).xformat(Format::Si).svg()).contains(">1.5k<"));The y tick label format (the right axis after
right()).nabiuse plot::Format; assert((try plot::line([0, 1], [0.0, 0.5]).yformat(Format::Percent).svg()).contains(">50%<"));Fixed x ticks, labelled by the format (or by
labels).nabiassert((try plot::line([0, 2], [0, 1]).xticks([0.0, 1.0, 2.0], ["lo", "mid", "hi"]).svg()).contains(">mid<"));Fixed y ticks (the right axis after
right()).nabiassert((try plot::line([0, 2], [0, 1]).yticks([0.0, 1.0]).svg()).contains(">1<"));Minor ticks on both axes.
nabiassert((try plot::line([0, 1], [0, 1]).minor().svg()).contains("stroke-width=\"0.75\""));Grid lines on (or off) for both axes.
nabiassert(!(try plot::line([0, 1], [0, 1]).grid(false).svg()).contains("#e9e4e6"));The fraction of the data range added on each side (0.05 by default).
nabiassert((try plot::line([0, 1], [0, 1]).with_margins(0.0).svg()).contains("<path"));Flips the x axis.
nabiassert((try plot::line([0, 1], [0, 1]).invert_x().svg()).contains("<path"));Flips the y axis.
nabiassert((try plot::line([0, 1], [0, 1]).invert_y().svg()).contains("<path"));Fixes y units per x unit on screen: 1.0 for equal scales.
nabiassert((try plot::line([0, 1], [0, 1]).aspect(1.0).svg()).contains("<path"));Later layers and
ylabel,ylim,yscale, ... go to a second y axis on the right (a twin axis).nabilet ax = plot::axes().line([0, 1], [0, 1]).ylabel("left").right().line([0, 1], [100, 50]).ylabel("right"); assert((try ax.svg()).contains(">right<"));Later layers go to the left y axis again.
nabilet ax = plot::line([0, 1], [0, 1]).right().line([0, 1], [9, 5]).left().ylabel("left side"); assert((try ax.svg()).contains(">left side<"));A legend at
loc(Loc::Bestby default). Layers with a label get one automatically; this places it, or turns it on for none.nabiuse plot::Loc; assert((try plot::line([0, 1], [0, 1]).label("a").legend(Loc::LowerRight).svg()).contains(">a<"));No legend.
nabiassert(!(try plot::line([0, 1], [0, 1]).label("a").no_legend().svg()).contains(">a<"));A colorbar for the last coloured layer, titled
label.nabiassert((try plot::heatmap([[1.0, 2.0]]).colorbar("level").svg()).contains(">level<"));The theme when this Axes is rendered on its own.
nabiassert((try plot::line([0, 1], [0, 1]).theme(plot::Theme::dark()).svg()).contains("#141114"));The figure size in px when rendered on its own.
nabiassert((try plot::line([0, 1], [0, 1]).size_px(300.0, 200.0).svg()).contains("width=\"300\""));This Axes as a one-panel figure.
nabiassert(plot::line([0, 1], [0, 1]).figure().panels().len() == 1);The SVG document.
nabiassert((try plot::line([0, 1], [0, 1]).svg()).ends_with("</svg>\n"));A PNG image;
scalemultiplies the pixel size (2 for HiDPI).nabiassert((try plot::line([0, 1], [0, 1]).size_px(40.0, 30.0).png())[1] == 80);
Sourcepub fn term(§self, cols: usize = 80, rows: usize = 24, color: bool = false) throws (PlotError) -> str A text rendering for a terminal,
colsxrowscharacters.nabiassert((try plot::line([0, 1], [0, 1]).term(cols: 30, rows: 8)).lines().count() == 8);Writes an
.svgor.pngfile, by the extension.nabiuse 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);Prints the terminal rendering.
nabitry plot::line([0, 1], [0, 1]).show();
Trait implementations 2
impl RichDisplay for Axesimpl ops::Add<Axes> for Axes
Functions
axesSource
pub fn axes() -> Axes An empty Axes.
assert((try plot::axes().svg()).contains("</svg>"));