scatter_svg/
scatter_svg.rs

1use plotlib::page::Page;
2use plotlib::repr::Plot;
3use plotlib::style::{PointMarker, PointStyle};
4use plotlib::view::ContinuousView;
5
6fn main() {
7    // Scatter plots expect a list of pairs
8    let data1 = vec![
9        (-3.0, 2.3),
10        (-1.6, 5.3),
11        (0.3, 0.7),
12        (4.3, -1.4),
13        (6.4, 4.3),
14        (8.5, 3.7),
15    ];
16
17    // We create our scatter plot from the data
18    let s1: Plot = Plot::new(data1).point_style(
19        PointStyle::new()
20            .marker(PointMarker::Square) // setting the marker to be a square
21            .colour("#DD3355"),
22    ); // and a custom colour
23
24    // We can plot multiple data sets in the same view
25    let data2 = vec![(-1.4, 2.5), (7.2, -0.3)];
26    let s2: Plot = Plot::new(data2).point_style(
27        PointStyle::new() // uses the default marker
28            .colour("#35C788"),
29    ); // and a different colour
30
31    // The 'view' describes what set of data is drawn
32    let v = ContinuousView::new()
33        .add(s1)
34        .add(s2)
35        .x_range(-5., 10.)
36        .y_range(-2., 6.)
37        .x_label("Some varying variable")
38        .y_label("The response of something");
39
40    // A page with a single view is then saved to an SVG file
41    Page::single(&v).save("scatter.svg").unwrap();
42}