histogram/
histogram.rs

1use polars::prelude::*;
2
3use plotlars::{Axis, Histogram, Legend, Plot, Rgb, Text, TickDirection};
4
5fn main() {
6    let dataset = LazyCsvReader::new(PlPath::new("data/penguins.csv"))
7        .finish()
8        .unwrap()
9        .select([
10            col("species"),
11            col("sex").alias("gender"),
12            col("flipper_length_mm").cast(DataType::Int16),
13            col("body_mass_g").cast(DataType::Int16),
14        ])
15        .collect()
16        .unwrap();
17
18    let axis = Axis::new()
19        .show_line(true)
20        .show_grid(true)
21        .value_thousands(true)
22        .tick_direction(TickDirection::OutSide);
23
24    Histogram::builder()
25        .data(&dataset)
26        .x("body_mass_g")
27        .group("species")
28        .opacity(0.5)
29        .colors(vec![Rgb(255, 165, 0), Rgb(147, 112, 219), Rgb(46, 139, 87)])
30        .plot_title(Text::from("Histogram").font("Arial").size(18))
31        .x_title(Text::from("body mass (g)").font("Arial").size(15))
32        .y_title(Text::from("count").font("Arial").size(15))
33        .legend_title(Text::from("species").font("Arial").size(15))
34        .x_axis(&axis)
35        .y_axis(&axis)
36        .legend(&Legend::new().x(0.9))
37        .build()
38        .plot();
39}