barplot/
barplot.rs

1use polars::prelude::*;
2
3use plotlars::{BarPlot, Legend, Orientation, Plot, Rgb, Text};
4
5fn main() {
6    let dataset = df![
7        "animal" => &["giraffe", "giraffe", "orangutan", "orangutan", "monkey", "monkey"],
8        "gender" => &vec!["female", "male", "female", "male", "female", "male"],
9        "value" => &vec![20.0f32, 25.0, 14.0, 18.0, 23.0, 31.0],
10        "error" => &vec![1.0, 0.5, 1.5, 1.0, 0.5, 1.5],
11    ]
12    .unwrap();
13
14    BarPlot::builder()
15        .data(&dataset)
16        .labels("animal")
17        .values("value")
18        .orientation(Orientation::Vertical)
19        .group("gender")
20        .sort_groups_by(|a, b| a.len().cmp(&b.len()))
21        .error("error")
22        .colors(vec![Rgb(255, 127, 80), Rgb(64, 224, 208)])
23        .plot_title(Text::from("Bar Plot").font("Arial").size(18))
24        .x_title(Text::from("animal").font("Arial").size(15))
25        .y_title(Text::from("value").font("Arial").size(15))
26        .legend_title(Text::from("gender").font("Arial").size(15))
27        .legend(
28            &Legend::new()
29                .orientation(Orientation::Horizontal)
30                .y(1.0)
31                .x(0.4),
32        )
33        .build()
34        .plot();
35}