Skip to main content

scatter/
scatter.rs

1use ggplot_rs::prelude::*;
2use polars::prelude::*;
3
4fn main() -> Result<(), Box<dyn std::error::Error>> {
5    let sepal_length: Vec<f64> = (0..50).map(|i| 4.5 + i as f64 * 0.05).collect();
6    let sepal_width: Vec<f64> = (0..50)
7        .map(|i| 2.0 + (i as f64 * 0.3).sin() + i as f64 * 0.02)
8        .collect();
9    let species: Vec<&str> = (0..50)
10        .map(|i| match i % 3 {
11            0 => "setosa",
12            1 => "versicolor",
13            _ => "virginica",
14        })
15        .collect();
16
17    let df = df! {
18        "sepal_length" => sepal_length,
19        "sepal_width" => sepal_width,
20        "species" => species,
21    }?;
22
23    GGPlot::new(df)
24        .aes(
25            Aes::new()
26                .x("sepal_length")
27                .y("sepal_width")
28                .color("species"),
29        )
30        .geom_point()
31        .title("Iris Scatter Plot")
32        .xlab("Sepal Length")
33        .ylab("Sepal Width")
34        .save("scatter.svg")?;
35
36    println!("Saved scatter.svg");
37    Ok(())
38}