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 librarythat isdifferent than Matplotlib. The difference happens because we wantconveniencefor the Rust developer while getting thefantastic quality of Matplotlib 😀.
Internally, we useMatplotlib via a Python 3 script.First, we generate a python code in a directory of your choice (e.g.,/tmp/plotpy
),and then we callpython3 using Rust’sstd::process::Command.
The Python script has the same name as the figure name given to thePlot::save function,but with the.py
extension. The figure name can have the (png, pdf, or svg) extension(seeMatplotlib)for more information regarding file extensions.
We generate the Python script with the preamble listed inPYTHON_HEADER and the fileshould be useful for double checking or even directly adding Python/Matplotlib commands,in case the functionality is not implemented here yet.
When callingPlot::save() orPlot::show(), if an error occurs, we generate a logfile 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 asCanvas,Curve,Contour,Histogram,Surface,Text (and more) and then passing them toPlot for the generationof the files mentioned above. ThePlot::show() function may also be used to immediatelysee the plot or drawing on the screen.
Alternatively,if evcxr is installed, the functionPlot::show_in_jupyter() can be used to show the resulting figure in a Jupyter notebook.
Each structure (e.g.Curve,Legend, orText) defines many configuration optionsthat can be set by calling their ownset_...
function. Typically, these structures providedraw_...
functions to plot/draw features. Afterwards, we callPlot::add to add these structuresto thePlot and then callPlot::save. Thedraw
method of each object must be calledbefore adding toPlot
.
§Example
useplotpy::{generate3d, Plot, StrError, Surface};fnmain() ->Result<(), StrError> {letmutsurface = 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 surfaceletn =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 plotletmutplot = Plot::new(); plot.add(&surface);// save figureplot.save("/tmp/plotpy/example_main.svg")?;Ok(())}
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)
- Histogram
- Generates a Histogram plot
- Image
- Generates an image plot (imshow)
- Inset
Axes - Implements the capability to add inset Axes to existing Axes.
- Legend
- Generates a Legend
- Plot
- Driver structure that calls Python
- Slope
Icon - Creates an icon to indicate the slope of lines
- Super
Title Params - Holds parameters for the SuperTitle
- Surface
- Generates a 3D a surface (or wireframe, or both)
- Text
- Creates text to be added to a plot
Enums§
- Poly
Code - 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
- Graph
Maker - Defines the trait used by Plot to add graph entities
Functions§
- generate2d
- Generates 2d points (meshgrid)
- generate3d
- Generates 3d points (function over meshgrid)
- linspace
- Returns evenly spaced numbers over a specified closed interval
- sign
- Implements the sign function
- suq_cos
- Implements the superquadric auxiliary involving cos(x)
- suq_sin
- Implements the superquadric function involving sin(x)
Type Aliases§
- StrError
- Defines a type alias for the error type as a static string