Skip to main content

timeseriesplot/
timeseriesplot.rs

1use plotlars::{Axis, Legend, Line, Plot, Rgb, Shape, Text, TimeSeriesPlot};
2use polars::prelude::*;
3
4fn main() {
5    let revenue_dataset = LazyCsvReader::new(PlRefPath::new("data/revenue_and_cost.csv"))
6        .finish()
7        .unwrap()
8        .select([
9            col("Date").cast(DataType::String),
10            col("Revenue").cast(DataType::Int32),
11            col("Cost").cast(DataType::Int32),
12        ])
13        .collect()
14        .unwrap();
15
16    TimeSeriesPlot::builder()
17        .data(&revenue_dataset)
18        .x("Date")
19        .y("Revenue")
20        .additional_series(vec!["Cost"])
21        .size(8)
22        .colors(vec![Rgb(0, 0, 255), Rgb(255, 0, 0)])
23        .lines(vec![Line::Dash, Line::Solid])
24        .with_shape(true)
25        .shapes(vec![Shape::Circle, Shape::Square])
26        .plot_title(Text::from("Time Series Plot").font("Arial").size(18))
27        .legend(&Legend::new().x(0.05).y(0.9))
28        .x_title("x")
29        .y_title(Text::from("y").color(Rgb(0, 0, 255)))
30        .y2_title(Text::from("y2").color(Rgb(255, 0, 0)))
31        .y_axis(
32            &Axis::new()
33                .value_color(Rgb(0, 0, 255))
34                .show_grid(false)
35                .zero_line_color(Rgb(0, 0, 0)),
36        )
37        .y2_axis(
38            &Axis::new()
39                .axis_side(plotlars::AxisSide::Right)
40                .value_color(Rgb(255, 0, 0))
41                .show_grid(false),
42        )
43        .build()
44        .plot();
45
46    let temperature_dataset = LazyCsvReader::new(PlRefPath::new("data/debilt_2023_temps.csv"))
47        .with_has_header(true)
48        .with_try_parse_dates(true)
49        .finish()
50        .unwrap()
51        .with_columns(vec![
52            (col("tavg") / lit(10)).alias("tavg"),
53            (col("tmin") / lit(10)).alias("tmin"),
54            (col("tmax") / lit(10)).alias("tmax"),
55        ])
56        .collect()
57        .unwrap();
58
59    TimeSeriesPlot::builder()
60        .data(&temperature_dataset)
61        .x("date")
62        .y("tavg")
63        .additional_series(vec!["tmin", "tmax"])
64        .colors(vec![Rgb(128, 128, 128), Rgb(0, 122, 255), Rgb(255, 128, 0)])
65        .lines(vec![Line::Solid, Line::Dot, Line::Dot])
66        .plot_title("Temperature at De Bilt (2023)")
67        .legend_title("Legend")
68        .build()
69        .plot();
70}