Skip to main content

boxplot/
boxplot.rs

1use plotlars::{Axis, BoxPlot, Legend, Orientation, Plot, Rgb, Text};
2use polars::prelude::*;
3
4fn main() {
5    let dataset = LazyCsvReader::new(PlRefPath::new("data/penguins.csv"))
6        .finish()
7        .unwrap()
8        .select([
9            col("species"),
10            col("sex").alias("gender"),
11            col("flipper_length_mm").cast(DataType::Int16),
12            col("body_mass_g").cast(DataType::Int16),
13        ])
14        .collect()
15        .unwrap();
16
17    BoxPlot::builder()
18        .data(&dataset)
19        .labels("species")
20        .values("body_mass_g")
21        .orientation(Orientation::Vertical)
22        .group("gender")
23        .box_points(true)
24        .point_offset(-1.5)
25        .jitter(0.01)
26        .opacity(0.1)
27        .colors(vec![Rgb(0, 191, 255), Rgb(57, 255, 20), Rgb(255, 105, 180)])
28        .plot_title(Text::from("Box Plot").font("Arial").size(18))
29        .x_title(Text::from("species").font("Arial").size(15))
30        .y_title(Text::from("body mass (g)").font("Arial").size(15).x(-0.04))
31        .legend_title(Text::from("gender").font("Arial").size(15))
32        .y_axis(&Axis::new().value_thousands(true))
33        .legend(&Legend::new().border_width(1).x(0.9))
34        .build()
35        .plot();
36}