#plot #scientific-computing #axis #limit #line #data #label

oscirs_plot

Plotting crate for scientific computing

5 unstable releases

0.3.0 Aug 10, 2023
0.2.0 Jun 8, 2023
0.2.0-alpha Jun 7, 2023
0.1.1 Jun 5, 2023
0.1.0 Jun 5, 2023

#206 in Visualization

Download history 16/week @ 2024-04-03 1/week @ 2024-05-22

62 downloads per month
Used in oscirs

Apache-2.0

44KB
706 lines

oscirs_plot

crates.io

A plotting crate for Rust

Description

This crate focuses on plotting data based on float vectors. It currently only supports line plots, but on those line plots you can equalize axes, set custom axis limits, add a legend, title the plot, and set axis labels.

Use

For a quick start, import everything from svgplot_core and initialize a new figure. Use the default constructor and create a Scatterline object. Make the figure variable mutable, as it will be dynamically storing data series internally.

use oscirs_plot::svgplot_core::*;

let mut figure: Scatterline = Scatterline::default();

Once we have a figure, we can set the axis labels and the figure title.

figure.label_x("X axis (unit)");
figure.label_y("Y axis (unit)");
figure.title("This is a plot");

Now we can create some data vectors and specify a style for our series to be plotted with. We'll make this mutable so we can reuse the same style object for another data series. For this example I chose to plot a square root function in blue.

let x: Vec<f32> = (0..=6)
    .map(|x| x as f32)
    .collect();
let y: Vec<f32> = x.clone()
    .into_iter()
    .map(|x| x.sqrt())
    .collect();

let mut style: PlotStyle = PlotStyle {
    stroke_color: Color::Blue,
    ..Default::default()
};

figure.add_data(&x, &y, &style)
    .expect("Failed to add data series");

We can also create scatter plots by specifying a stroke width of 0 and turning markers on. Lets add a scatter series of the line y=x.

let y2: Vec<f32> = x.clone();

style.stroke_color = Color::Red;
style.stroke_width = 0;
style.has_markers = true;

figure.add_data(&x, &y2, &style)
    .expect("Failed to add data series");

To display our plot, we just need to call render() on our figure and specify a file name. The .svg file will be auto-generated and will open at the end of the writing process.

figure.render("Example_Figure").expect("Failed to generate figure");

Dependencies

~77KB