Skip to main content

Crate plotpy

Crate plotpy 

Source
Expand description

Rust plotting library using Python and Matplotlib

This library implements high-level functions to generate plots and drawings. Although we use Python/Matplotlib, the goal is to provide a convenient Rust library that is different than Matplotlib. The difference happens because we want convenience for the Rust developer while getting the fantastic quality of Matplotlib 😀.

Internally, we use Matplotlib via a Python 3 script. First, we generate a python code in a directory of your choice (e.g., /tmp/plotpy), and then we call python3 using Rust’s std::process::Command.

The Python script has the same name as the figure name given to the Plot::save function, but with the .py extension. The figure name can have the (png, pdf, or svg) extension (see Matplotlib) for more information regarding file extensions.

We generate the Python script with the preamble listed in PYTHON_HEADER and the file should be useful for double checking or even directly adding Python/Matplotlib commands, in case the functionality is not implemented here yet.

When calling Plot::save() or Plot::show(), if an error occurs, we generate a log file in the same output directory with the same filename as the figure (and python script), but with the .log extension.

The typical use of this library is by allocating structures such as Canvas, Curve, Contour, Histogram, Surface, Text (and more) and then passing them to Plot for the generation of the files mentioned above. The Plot::show() function may also be used to immediately see the plot or drawing on the screen.

Alternatively, if evcxr is installed, the function Plot::show_in_jupyter() can be used to show the resulting figure in a Jupyter notebook.

Each structure (e.g. Curve, Legend, or Text) defines many configuration options that can be set by calling their own set_... function. Typically, these structures provide draw_... functions to plot/draw features. Afterwards, we call Plot::add to add these structures to the Plot and then call Plot::save. The draw method of each object must be called before adding to Plot.

§Example

use plotpy::{generate3d, Plot, StrError, Surface};

fn main() -> Result<(), StrError> {
    let mut surface = Surface::new();
    surface
        .set_with_wireframe(true)
        .set_colormap_name("Pastel1")
        .set_with_colorbar(true)
        .set_colorbar_label("temperature")
        .set_wire_line_color("#1862ab")
        .set_wire_line_style(":")
        .set_wire_line_width(0.75);

    // draw surface
    let n = 9;
    let (x, y, z) = generate3d(-2.0, 2.0, -2.0, 2.0, n, n, |x, y| x * x + y * y);
    surface.draw(&x, &y, &z);

    // add surface to plot
    let mut plot = Plot::new();
    plot.add(&surface);

    // save figure
    plot.save("/tmp/plotpy/example_main.svg")?;
    Ok(())
}

example_main.svg

Structs§

Barplot
Generates a Barplot plot
Boxplot
Draw a box and whisker plot
Canvas
Implements functions to draw 2D and 3D features, including poly-lines and Bezier curves
Contour
Generates a contour plot
Curve
Generates a curve (aka line-plot) given two arrays (x,y)
DarkMode
Implements a dark mode enabler for plots
FillBetween
Fills the area between two curves
Histogram
Generates a Histogram plot with support for multiple overlaid or stacked series
Image
Generates an image plot (imshow)
InsetAxes
Implements inset Axes — a magnified sub-view of a region within the main plot
Legend
Generates a Legend from all labelled graph entities in the current axes
Plot
Central plot driver — collects graph entities, generates a Python script, and executes it
SlopeIcon
Creates an icon indicating the slope of a line at a given point
Stream
Visualizes vector fields using streamlines and quiver arrow plots
SuperTitleParams
Holds formatting parameters for the figure-level supertitle
Surface
Generates a 3D surface (or wireframe, or both)
Text
Creates text annotations for 2D or 3D plots with optional bounding boxes

Enums§

PolyCode
Defines the poly-curve code
RayEndpoint
Holds either the second point coordinates of a ray or the slope of the ray

Constants§

PYTHON_HEADER
Commands to be added at the beginning of the Python script

Traits§

AsMatrix
Defines a trait to handle Matrix-like data
AsVector
Defines a trait to handle Vector-like data
GraphMaker
Defines the trait used by Plot to add graph entities

Functions§

generate2d
Generates 2d meshgrid points
generate3d
Generates 3d points by evaluating a function over a 2d meshgrid
linspace
Returns evenly spaced numbers over a specified closed interval
sign
Implements the sign function
suq_cos
Implements the superquadric auxiliary function involving cos(x)
suq_sin
Implements the superquadric auxiliary function involving sin(x)

Type Aliases§

StrError
Defines a type alias for the error type as a static string