Skip to main content

boxplot/
boxplot.rs

1use bland::{Figure, Hatch, PaperSize};
2
3fn main() {
4    // Synthetic samples per group.
5    let control: Vec<f64> = (0..30)
6        .map(|i| 5.0 + ((i as f64 * 0.43) % 1.0 - 0.5) * 3.0)
7        .collect();
8    let mut treated: Vec<f64> = (0..30)
9        .map(|i| 7.0 + ((i as f64 * 0.71 + 0.1) % 1.0 - 0.5) * 4.0)
10        .collect();
11    treated.push(15.0); // outlier
12
13    let groups = vec![
14        ("control".to_string(), control),
15        ("treated".to_string(), treated),
16    ];
17
18    let fig = Figure::new()
19        .size(PaperSize::A5Landscape)
20        .title("Distribution by group")
21        .ylabel("response")
22        .boxplot(&groups, |b| b.hatch(Hatch::Diagonal).label("Q1–Q3"))
23        .legend_top_right();
24
25    std::fs::create_dir_all("out").expect("create out/");
26    std::fs::write("out/boxplot.svg", fig.to_svg()).expect("write svg");
27    println!("wrote out/boxplot.svg");
28}