Skip to main content

histogram/
histogram.rs

1use plotlars::{Axis, Histogram, Legend, Plot, Rgb, Text, TickDirection};
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    let axis = Axis::new()
18        .show_line(true)
19        .show_grid(true)
20        .value_thousands(true)
21        .tick_direction(TickDirection::OutSide);
22
23    Histogram::builder()
24        .data(&dataset)
25        .x("body_mass_g")
26        .group("species")
27        .opacity(0.5)
28        .colors(vec![Rgb(255, 165, 0), Rgb(147, 112, 219), Rgb(46, 139, 87)])
29        .plot_title(Text::from("Histogram").font("Arial").size(18))
30        .x_title(Text::from("body mass (g)").font("Arial").size(15))
31        .y_title(Text::from("count").font("Arial").size(15))
32        .legend_title(Text::from("species").font("Arial").size(15))
33        .x_axis(&axis)
34        .y_axis(&axis)
35        .legend(&Legend::new().x(0.9))
36        .build()
37        .plot();
38}