pub struct TimeSeriesPlot { /* private fields */ }Expand description
A structure representing a time series plot.
The TimeSeriesPlot struct facilitates the creation and customization of time series plots with various options
for data selection, grouping, layout configuration, and aesthetic adjustments. It supports the addition of multiple
series, customization of marker shapes, colors, sizes, opacity settings, and comprehensive layout customization
including titles, axes, and legends.
§Arguments
data- A reference to theDataFramecontaining the data to be plotted.x- A string slice specifying the column name to be used for the x-axis, typically representing time or dates.y- A string slice specifying the column name to be used for the y-axis, typically representing the primary metric.additional_series- An optional vector of string slices specifying additional y-axis columns to be plotted as series.facet- An optional string slice specifying the column name to be used for faceting (creating multiple subplots).facet_config- An optional reference to aFacetConfigstruct for customizing facet behavior (grid dimensions, scales, gaps, etc.).size- An optionalusizespecifying the size of the markers or line thickness.color- An optionalRgbvalue specifying the color of the markers. This is used whengroupis not specified.colors- An optional vector ofRgbvalues specifying the colors for the markers. This is used whengroupis specified to differentiate between groups.with_shape- An optionalboolindicating whether to use shapes for markers in the plot.shape- An optionalShapespecifying the shape of the markers. This is used whengroupis not specified.shapes- An optional vector ofShapevalues specifying multiple shapes for the markers when plotting multiple groups.width- An optionalf64specifying the width of the plotted lines.line- An optionalLineStylespecifying the style of the line. This is used whenadditional_seriesis not specified.lines- An optional vector ofLineStyleenums specifying the styles of lines for each plotted series. This is used whenadditional_seriesis specified to differentiate between multiple series.plot_title- An optionalTextstruct specifying the title of the plot.x_title- An optionalTextstruct specifying the title of the x-axis.y_title- An optionalTextstruct specifying the title of the y-axis.y2_title- An optionalTextstruct specifying the title of the secondary y-axis.legend_title- An optionalTextstruct specifying the title of the legend.x_axis- An optional reference to anAxisstruct for customizing the x-axis.y_axis- An optional reference to anAxisstruct for customizing the y-axis.y2_axis- An optional reference to anAxisstruct for customizing the secondary y-axis.legend- An optional reference to aLegendstruct for customizing the legend of the plot (e.g., positioning, font, etc.).
§Examples
use polars::prelude::*;
use plotlars::{Axis, Legend, Line, Plot, Rgb, Shape, Text, TimeSeriesPlot};
let dataset = LazyCsvReader::new(PlPath::new("data/revenue_and_cost.csv"))
.finish()
.unwrap()
.select([
col("Date").cast(DataType::String),
col("Revenue").cast(DataType::Int32),
col("Cost").cast(DataType::Int32),
])
.collect()
.unwrap();
TimeSeriesPlot::builder()
.data(&dataset)
.x("Date")
.y("Revenue")
.additional_series(vec!["Cost"])
.size(8)
.colors(vec![
Rgb(0, 0, 255),
Rgb(255, 0, 0),
])
.lines(vec![
Line::Dash,
Line::Solid,
])
.with_shape(true)
.shapes(vec![
Shape::Circle,
Shape::Square,
])
.plot_title(
Text::from("Time Series Plot")
.font("Arial")
.size(18)
)
.legend(
&Legend::new()
.x(0.05)
.y(0.9)
)
.x_title("x")
.y_title(
Text::from("y")
.color(Rgb(0, 0, 255))
)
.y2_title(
Text::from("y2")
.color(Rgb(255, 0, 0))
)
.y_axis(
&Axis::new()
.value_color(Rgb(0, 0, 255))
.show_grid(false)
.zero_line_color(Rgb(0, 0, 0))
)
.y2_axis(
&Axis::new()
.axis_side(plotlars::AxisSide::Right)
.value_color(Rgb(255, 0, 0))
.show_grid(false)
)
.build()
.plot();
use polars::prelude::*;
use plotlars::{Plot, TimeSeriesPlot, Rgb, Line};
let dataset = LazyCsvReader::new(PlPath::new("data/debilt_2023_temps.csv"))
.with_has_header(true)
.with_try_parse_dates(true)
.finish()
.unwrap()
.with_columns(vec![
(col("tavg") / lit(10)).alias("tavg"),
(col("tmin") / lit(10)).alias("tmin"),
(col("tmax") / lit(10)).alias("tmax"),
])
.collect()
.unwrap();
TimeSeriesPlot::builder()
.data(&dataset)
.x("date")
.y("tavg")
.additional_series(vec!["tmin", "tmax"])
.colors(vec![
Rgb(128, 128, 128),
Rgb(0, 122, 255),
Rgb(255, 128, 0),
])
.lines(vec![
Line::Solid,
Line::Dot,
Line::Dot,
])
.plot_title("Temperature at De Bilt (2023)")
.legend_title("Legend")
.build()
.plot();
Implementations§
Source§impl TimeSeriesPlot
impl TimeSeriesPlot
Sourcepub fn builder<'f1, 'f2, 'f3, 'f4, 'f5, 'f6, 'f7, 'f8, 'f9, 'f10>() -> TimeSeriesPlotBuilder<'f1, 'f2, 'f3, 'f4, 'f5, 'f6, 'f7, 'f8, 'f9, 'f10>
pub fn builder<'f1, 'f2, 'f3, 'f4, 'f5, 'f6, 'f7, 'f8, 'f9, 'f10>() -> TimeSeriesPlotBuilder<'f1, 'f2, 'f3, 'f4, 'f5, 'f6, 'f7, 'f8, 'f9, 'f10>
Examples found in repository?
examples/faceting.rs (line 594)
582fn timeseriesplot_example() {
583 let dataset = CsvReadOptions::default()
584 .with_has_header(true)
585 .try_into_reader_with_file_path(Some("data/financial_timeseries.csv".into()))
586 .unwrap()
587 .finish()
588 .unwrap();
589
590 let facet_config = FacetConfig::new()
591 .highlight_facet(true)
592 .unhighlighted_color(Rgb(220, 220, 220));
593
594 TimeSeriesPlot::builder()
595 .data(&dataset)
596 .x("date")
597 .y("revenue")
598 .additional_series(vec!["costs"])
599 .facet("region")
600 .facet_config(&facet_config)
601 .plot_title(Text::from("Regional Financial Metrics"))
602 .x_title("Month")
603 .y_title("Amount ($)")
604 .legend_title("Metric")
605 .width(2.0)
606 .with_shape(false)
607 .colors(vec![Rgb(255, 105, 180), Rgb(30, 144, 255)])
608 .lines(vec![Line::Solid, Line::Dash])
609 .build()
610 .plot();
611}More examples
examples/timeseriesplot.rs (line 16)
4fn main() {
5 let revenue_dataset = LazyCsvReader::new(PlPath::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(PlPath::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}examples/subplot_grid.rs (line 97)
15fn regular_grid_example() {
16 let dataset1 = LazyCsvReader::new(PlPath::new("data/animal_statistics.csv"))
17 .finish()
18 .unwrap()
19 .collect()
20 .unwrap();
21
22 let plot1 = BarPlot::builder()
23 .data(&dataset1)
24 .labels("animal")
25 .values("value")
26 .orientation(Orientation::Vertical)
27 .group("gender")
28 .sort_groups_by(|a, b| a.len().cmp(&b.len()))
29 .error("error")
30 .colors(vec![Rgb(255, 127, 80), Rgb(64, 224, 208)])
31 .plot_title(Text::from("Bar Plot").x(-0.05).y(1.35).size(14))
32 .y_title(Text::from("value").x(-0.055).y(0.76))
33 .x_title(Text::from("animal").x(0.97).y(-0.2))
34 .legend(
35 &Legend::new()
36 .orientation(Orientation::Horizontal)
37 .x(0.4)
38 .y(1.2),
39 )
40 .build();
41
42 let dataset2 = LazyCsvReader::new(PlPath::new("data/penguins.csv"))
43 .finish()
44 .unwrap()
45 .select([
46 col("species"),
47 col("sex").alias("gender"),
48 col("flipper_length_mm").cast(DataType::Int16),
49 col("body_mass_g").cast(DataType::Int16),
50 ])
51 .collect()
52 .unwrap();
53
54 let axis = Axis::new()
55 .show_line(true)
56 .tick_direction(TickDirection::OutSide)
57 .value_thousands(true);
58
59 let plot2 = ScatterPlot::builder()
60 .data(&dataset2)
61 .x("body_mass_g")
62 .y("flipper_length_mm")
63 .group("species")
64 .sort_groups_by(|a, b| {
65 if a.len() == b.len() {
66 a.cmp(b)
67 } else {
68 a.len().cmp(&b.len())
69 }
70 })
71 .opacity(0.5)
72 .size(12)
73 .colors(vec![Rgb(178, 34, 34), Rgb(65, 105, 225), Rgb(255, 140, 0)])
74 .shapes(vec![Shape::Circle, Shape::Square, Shape::Diamond])
75 .plot_title(Text::from("Scatter Plot").x(-0.075).y(1.35).size(14))
76 .x_title(Text::from("body mass (g)").y(-0.4))
77 .y_title(Text::from("flipper length (mm)").x(-0.078).y(0.5))
78 .legend_title("species")
79 .x_axis(&axis.clone().value_range(vec![2500.0, 6500.0]))
80 .y_axis(&axis.clone().value_range(vec![170.0, 240.0]))
81 .legend(&Legend::new().x(0.98).y(0.95))
82 .build();
83
84 let dataset3 = LazyCsvReader::new(PlPath::new("data/debilt_2023_temps.csv"))
85 .with_has_header(true)
86 .with_try_parse_dates(true)
87 .finish()
88 .unwrap()
89 .with_columns(vec![
90 (col("tavg") / lit(10)).alias("avg"),
91 (col("tmin") / lit(10)).alias("min"),
92 (col("tmax") / lit(10)).alias("max"),
93 ])
94 .collect()
95 .unwrap();
96
97 let plot3 = TimeSeriesPlot::builder()
98 .data(&dataset3)
99 .x("date")
100 .y("avg")
101 .additional_series(vec!["min", "max"])
102 .colors(vec![Rgb(128, 128, 128), Rgb(0, 122, 255), Rgb(255, 128, 0)])
103 .lines(vec![Line::Solid, Line::Dot, Line::Dot])
104 .plot_title(Text::from("Time Series Plot").x(-0.05).y(1.35).size(14))
105 .y_title(Text::from("temperature (ºC)").x(-0.055).y(0.6))
106 .legend(&Legend::new().x(0.9).y(1.25))
107 .build();
108
109 let plot4 = BoxPlot::builder()
110 .data(&dataset2)
111 .labels("species")
112 .values("body_mass_g")
113 .orientation(Orientation::Vertical)
114 .group("gender")
115 .box_points(true)
116 .point_offset(-1.5)
117 .jitter(0.01)
118 .opacity(0.1)
119 .colors(vec![Rgb(0, 191, 255), Rgb(57, 255, 20), Rgb(255, 105, 180)])
120 .plot_title(Text::from("Box Plot").x(-0.075).y(1.35).size(14))
121 .x_title(Text::from("species").y(-0.3))
122 .y_title(Text::from("body mass (g)").x(-0.08).y(0.5))
123 .legend_title(Text::from("gender").size(12))
124 .y_axis(&Axis::new().value_thousands(true))
125 .legend(&Legend::new().x(1.0))
126 .build();
127
128 SubplotGrid::regular()
129 .plots(vec![&plot1, &plot2, &plot3, &plot4])
130 .rows(2)
131 .cols(2)
132 .v_gap(0.4)
133 .title(
134 Text::from("Regular Subplot Grid")
135 .size(16)
136 .font("Arial bold")
137 .y(0.95),
138 )
139 .build()
140 .plot();
141}examples/dimensions.rs (line 44)
7fn main() {
8 let penguins_dataset = LazyCsvReader::new(PlPath::new("data/penguins.csv"))
9 .finish()
10 .unwrap()
11 .select([
12 col("species"),
13 col("sex").alias("gender"),
14 col("flipper_length_mm").cast(DataType::Int16),
15 col("body_mass_g").cast(DataType::Int16),
16 ])
17 .collect()
18 .unwrap();
19
20 let temperature_dataset = LazyCsvReader::new(PlPath::new("data/debilt_2023_temps.csv"))
21 .with_has_header(true)
22 .with_try_parse_dates(true)
23 .finish()
24 .unwrap()
25 .with_columns(vec![
26 (col("tavg") / lit(10)).alias("tavg"),
27 (col("tmin") / lit(10)).alias("tmin"),
28 (col("tmax") / lit(10)).alias("tmax"),
29 ])
30 .collect()
31 .unwrap();
32
33 let animals_dataset = LazyCsvReader::new(PlPath::new("data/animal_statistics.csv"))
34 .finish()
35 .unwrap()
36 .collect()
37 .unwrap();
38
39 let axis = Axis::new()
40 .show_line(true)
41 .tick_direction(TickDirection::OutSide)
42 .value_thousands(true);
43
44 let plot1 = TimeSeriesPlot::builder()
45 .data(&temperature_dataset)
46 .x("date")
47 .y("tavg")
48 .additional_series(vec!["tmin", "tmax"])
49 .colors(vec![Rgb(128, 128, 128), Rgb(0, 122, 255), Rgb(255, 128, 0)])
50 .lines(vec![Line::Solid, Line::Dot, Line::Dot])
51 .plot_title(
52 Text::from("De Bilt Temperature 2023")
53 .font("Arial Bold")
54 .size(16),
55 )
56 .y_title(Text::from("temperature (°C)").size(13).x(-0.08))
57 // .legend_title(Text::from("Measure").size(12))
58 .legend(&Legend::new().x(0.1).y(0.9))
59 .build();
60
61 let plot2 = ScatterPlot::builder()
62 .data(&penguins_dataset)
63 .x("body_mass_g")
64 .y("flipper_length_mm")
65 .group("species")
66 .sort_groups_by(|a, b| {
67 if a.len() == b.len() {
68 a.cmp(b)
69 } else {
70 a.len().cmp(&b.len())
71 }
72 })
73 .opacity(0.6)
74 .size(10)
75 .colors(vec![Rgb(178, 34, 34), Rgb(65, 105, 225), Rgb(255, 140, 0)])
76 .shapes(vec![Shape::Circle, Shape::Square, Shape::Diamond])
77 .plot_title(Text::from("Penguin Morphology").font("Arial Bold").size(16))
78 .x_title(Text::from("body mass (g)").size(13))
79 .y_title(Text::from("flipper length (mm)").size(13).x(-0.11))
80 .legend_title(Text::from("Species").size(12))
81 .x_axis(&axis.clone().value_range(vec![2500.0, 6500.0]))
82 .y_axis(&axis.clone().value_range(vec![170.0, 240.0]))
83 .legend(&Legend::new().x(0.85).y(0.4))
84 .build();
85
86 let plot3 = BarPlot::builder()
87 .data(&animals_dataset)
88 .labels("animal")
89 .values("value")
90 .orientation(Orientation::Vertical)
91 .group("gender")
92 .sort_groups_by(|a, b| a.len().cmp(&b.len()))
93 .error("error")
94 .colors(vec![Rgb(255, 127, 80), Rgb(64, 224, 208)])
95 .plot_title(Text::from("Animal Statistics").font("Arial Bold").size(16))
96 .x_title(Text::from("animal").size(13))
97 .y_title(Text::from("value").size(13))
98 .legend_title(Text::from("Gender").size(12))
99 .legend(
100 &Legend::new()
101 .orientation(Orientation::Horizontal)
102 .x(0.35)
103 .y(0.9),
104 )
105 .build();
106
107 let plot4 = BoxPlot::builder()
108 .data(&penguins_dataset)
109 .labels("species")
110 .values("body_mass_g")
111 .orientation(Orientation::Vertical)
112 .group("gender")
113 .box_points(true)
114 .point_offset(-1.5)
115 .jitter(0.01)
116 .opacity(0.15)
117 .colors(vec![Rgb(0, 191, 255), Rgb(57, 255, 20), Rgb(255, 105, 180)])
118 .plot_title(
119 Text::from("Body Mass Distribution")
120 .font("Arial Bold")
121 .size(16),
122 )
123 .x_title(Text::from("species").size(13))
124 .y_title(Text::from("body mass (g)").size(13).x(-0.12))
125 .legend_title(Text::from("Gender").size(12))
126 .y_axis(&Axis::new().value_thousands(true))
127 .legend(&Legend::new().x(0.85).y(0.9))
128 .build();
129
130 let dimensions = Dimensions::new().width(1400).height(850).auto_size(false);
131
132 SubplotGrid::regular()
133 .plots(vec![&plot1, &plot2, &plot3, &plot4])
134 .rows(2)
135 .cols(2)
136 .v_gap(0.3)
137 .h_gap(0.2)
138 .dimensions(&dimensions)
139 .title(
140 Text::from("Scientific Data Visualization Dashboard")
141 .size(26)
142 .font("Arial Bold"),
143 )
144 .build()
145 .plot();
146}Trait Implementations§
Source§impl Clone for TimeSeriesPlot
impl Clone for TimeSeriesPlot
Source§fn clone(&self) -> TimeSeriesPlot
fn clone(&self) -> TimeSeriesPlot
Returns a duplicate of the value. Read more
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
Performs copy-assignment from
source. Read moreSource§impl Serialize for TimeSeriesPlot
impl Serialize for TimeSeriesPlot
impl PlotHelper for TimeSeriesPlot
Auto Trait Implementations§
impl Freeze for TimeSeriesPlot
impl !RefUnwindSafe for TimeSeriesPlot
impl !Send for TimeSeriesPlot
impl !Sync for TimeSeriesPlot
impl Unpin for TimeSeriesPlot
impl !UnwindSafe for TimeSeriesPlot
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
Source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
Converts
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
Converts
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more