Skip to main content

loess_smooth/
loess_smooth.rs

1use ggplot_rs::prelude::*;
2use polars::prelude::*;
3
4fn main() -> Result<(), Box<dyn std::error::Error>> {
5    // Generate noisy sine wave data
6    let x: Vec<f64> = (0..80).map(|i| (i as f64) * 0.1).collect();
7    let y: Vec<f64> = (0..80)
8        .map(|i| {
9            let xv = (i as f64) * 0.1;
10            let noise = ((i * 17 + 3) % 11) as f64 / 11.0 - 0.5; // deterministic pseudo-noise
11            (xv * 0.8).sin() * 2.0 + noise * 1.5
12        })
13        .collect();
14
15    let df = df! {
16        "x" => &x,
17        "y" => &y,
18    }?;
19
20    // Linear smooth (default)
21    GGPlot::new(df.clone())
22        .aes(Aes::new().x("x").y("y"))
23        .geom_point()
24        .geom_smooth()
25        .title("Linear Smooth (method = lm)")
26        .save("smooth_lm.svg")?;
27
28    println!("Saved smooth_lm.svg");
29
30    // LOESS smooth
31    GGPlot::new(df)
32        .aes(Aes::new().x("x").y("y"))
33        .geom_point()
34        .geom_smooth_with(GeomSmooth::default().loess(0.3))
35        .title("LOESS Smooth (span = 0.3)")
36        .save("smooth_loess.svg")?;
37
38    println!("Saved smooth_loess.svg");
39    Ok(())
40}