color_palettes/
color_palettes.rs1use ggplot_rs::prelude::*;
2use polars::prelude::*;
3
4fn main() -> Result<(), Box<dyn std::error::Error>> {
5 let groups = ["Alpha", "Beta", "Gamma", "Delta", "Epsilon"];
7 let mut x_vals = Vec::new();
8 let mut y_vals = Vec::new();
9 let mut group_vals = Vec::new();
10
11 for (gi, &group) in groups.iter().enumerate() {
12 for j in 0..20 {
13 let base_x = gi as f64 * 2.0 + 1.0;
14 let base_y = (gi as f64 + 1.0) * 3.0;
15 let r = ((gi * 20 + j) * 7 + 13) % 17;
16 x_vals.push(base_x + (r as f64 / 17.0 - 0.5) * 2.0);
17 y_vals.push(base_y + ((r * 3 + 5) % 11) as f64 / 11.0 * 4.0 - 2.0);
18 group_vals.push(group);
19 }
20 }
21
22 let df = df! {
23 "x" => &x_vals,
24 "y" => &y_vals,
25 "group" => &group_vals,
26 }?;
27
28 GGPlot::new(df.clone())
30 .aes(Aes::new().x("x").y("y").color("group"))
31 .geom_point()
32 .scale_color_viridis()
33 .title("Viridis Palette")
34 .save("palette_viridis.svg")?;
35
36 println!("Saved palette_viridis.svg");
37
38 GGPlot::new(df.clone())
40 .aes(Aes::new().x("x").y("y").color("group"))
41 .geom_point()
42 .scale_color_brewer(PaletteName::Set1)
43 .title("Brewer Set1 Palette")
44 .save("palette_brewer_set1.svg")?;
45
46 println!("Saved palette_brewer_set1.svg");
47
48 GGPlot::new(df.clone())
50 .aes(Aes::new().x("x").y("y").color("group"))
51 .geom_point()
52 .scale_color_brewer(PaletteName::Dark2)
53 .title("Brewer Dark2 Palette")
54 .save("palette_brewer_dark2.svg")?;
55
56 println!("Saved palette_brewer_dark2.svg");
57
58 GGPlot::new(df)
60 .aes(Aes::new().x("x").y("y").color("group"))
61 .geom_point()
62 .scale_color_manual(vec![
63 ("Alpha", RGBAColor::new(255, 0, 0)),
64 ("Beta", RGBAColor::new(0, 180, 0)),
65 ("Gamma", RGBAColor::new(0, 0, 255)),
66 ("Delta", RGBAColor::new(255, 165, 0)),
67 ("Epsilon", RGBAColor::new(128, 0, 128)),
68 ])
69 .title("Manual Color Scale")
70 .save("palette_manual.svg")?;
71
72 println!("Saved palette_manual.svg");
73 Ok(())
74}