Skip to main content

scatter/
scatter.rs

1use bland::{Figure, Marker, PaperSize, Stroke};
2
3fn main() {
4    // Two synthetic clusters with a fitted line.
5    let cluster_a_x: Vec<f64> = (0..50).map(|i| 0.4 * i as f64 + 1.0).collect();
6    let cluster_a_y: Vec<f64> = cluster_a_x
7        .iter()
8        .enumerate()
9        .map(|(i, x)| 0.7 * x + 1.0 + ((i as f64 * 0.71) % 1.0 - 0.5) * 2.0)
10        .collect();
11
12    let cluster_b_x: Vec<f64> = (0..50).map(|i| 0.4 * i as f64 + 1.0).collect();
13    let cluster_b_y: Vec<f64> = cluster_b_x
14        .iter()
15        .enumerate()
16        .map(|(i, x)| 0.5 * x + 4.0 + ((i as f64 * 1.31 + 0.4) % 1.0 - 0.5) * 1.5)
17        .collect();
18
19    let fit_x = vec![1.0, 21.0];
20    let fit_y = vec![1.7, 15.7];
21
22    let fig = Figure::new()
23        .size(PaperSize::A5Landscape)
24        .title("Linear fit comparison")
25        .xlabel("input")
26        .ylabel("response")
27        .scatter(&cluster_a_x, &cluster_a_y, |s| {
28            s.label("control").marker(Marker::CircleOpen)
29        })
30        .scatter(&cluster_b_x, &cluster_b_y, |s| {
31            s.label("treated").marker(Marker::TriangleFilled)
32        })
33        .line(&fit_x, &fit_y, |l| l.label("y = 0.7x + 1").stroke(Stroke::DashDot))
34        .legend_bottom_right();
35
36    std::fs::create_dir_all("out").expect("create out/");
37    std::fs::write("out/scatter.svg", fig.to_svg()).expect("write svg");
38    println!("wrote out/scatter.svg");
39}