pub struct Legend { /* private fields */ }Expand description
A structure representing a customizable plot legend.
§Example
use polars::prelude::*;
use plotlars::{Histogram, Legend, Orientation, Plot, Rgb};
let dataset = LazyCsvReader::new(PlPath::new("data/penguins.csv"))
.finish()
.unwrap()
.select([
col("species"),
col("sex").alias("gender"),
col("flipper_length_mm").cast(DataType::Int16),
col("body_mass_g").cast(DataType::Int16),
])
.collect()
.unwrap();
let legend = Legend::new()
.orientation(Orientation::Horizontal)
.border_width(1)
.x(0.78)
.y(0.825);
Histogram::builder()
.data(&dataset)
.x("body_mass_g")
.group("species")
.colors(vec![
Rgb(255, 0, 0),
Rgb(0, 255, 0),
Rgb(0, 0, 255),
])
.opacity(0.5)
.x_title("Body Mass (g)")
.y_title("Frequency")
.legend_title("Species")
.legend(&legend)
.build()
.plot();
Implementations§
Source§impl Legend
impl Legend
Sourcepub fn new() -> Self
pub fn new() -> Self
Creates a new Legend instance with default values.
Examples found in repository?
examples/barplot.rs (line 27)
5fn main() {
6 let dataset = df![
7 "animal" => &["giraffe", "giraffe", "orangutan", "orangutan", "monkey", "monkey"],
8 "gender" => &vec!["female", "male", "female", "male", "female", "male"],
9 "value" => &vec![20.0f32, 25.0, 14.0, 18.0, 23.0, 31.0],
10 "error" => &vec![1.0, 0.5, 1.5, 1.0, 0.5, 1.5],
11 ]
12 .unwrap();
13
14 BarPlot::builder()
15 .data(&dataset)
16 .labels("animal")
17 .values("value")
18 .orientation(Orientation::Vertical)
19 .group("gender")
20 .error("error")
21 .colors(vec![Rgb(255, 127, 80), Rgb(64, 224, 208)])
22 .plot_title(Text::from("Bar Plot").font("Arial").size(18))
23 .x_title(Text::from("animal").font("Arial").size(15))
24 .y_title(Text::from("value").font("Arial").size(15))
25 .legend_title(Text::from("gender").font("Arial").size(15))
26 .legend(
27 &Legend::new()
28 .orientation(Orientation::Horizontal)
29 .y(1.0)
30 .x(0.4),
31 )
32 .build()
33 .plot();
34}More examples
examples/histogram.rs (line 36)
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}examples/boxplot.rs (line 34)
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 BoxPlot::builder()
19 .data(&dataset)
20 .labels("species")
21 .values("body_mass_g")
22 .orientation(Orientation::Vertical)
23 .group("gender")
24 .box_points(true)
25 .point_offset(-1.5)
26 .jitter(0.01)
27 .opacity(0.1)
28 .colors(vec![Rgb(0, 191, 255), Rgb(57, 255, 20), Rgb(255, 105, 180)])
29 .plot_title(Text::from("Box Plot").font("Arial").size(18))
30 .x_title(Text::from("species").font("Arial").size(15))
31 .y_title(Text::from("body mass (g)").font("Arial").size(15))
32 .legend_title(Text::from("gender").font("Arial").size(15))
33 .y_axis(&Axis::new().value_thousands(true))
34 .legend(&Legend::new().border_width(1).x(0.9))
35 .build()
36 .plot();
37}examples/scatterplot.rs (line 38)
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 .tick_direction(TickDirection::OutSide)
21 .value_thousands(true);
22
23 ScatterPlot::builder()
24 .data(&dataset)
25 .x("body_mass_g")
26 .y("flipper_length_mm")
27 .group("species")
28 .opacity(0.5)
29 .size(12)
30 .colors(vec![Rgb(178, 34, 34), Rgb(65, 105, 225), Rgb(255, 140, 0)])
31 .shapes(vec![Shape::Circle, Shape::Square, Shape::Diamond])
32 .plot_title(Text::from("Scatter Plot").font("Arial").size(20).x(0.065))
33 .x_title("body mass (g)")
34 .y_title("flipper length (mm)")
35 .legend_title("species")
36 .x_axis(&axis.clone().value_range(vec![2500.0, 6500.0]))
37 .y_axis(&axis.clone().value_range(vec![170.0, 240.0]))
38 .legend(&Legend::new().x(0.85).y(0.15))
39 .build()
40 .plot();
41}examples/scatterpolar.rs (line 124)
74fn grouped_scatter_polar() {
75 // Create sample data - comparing two products across multiple metrics
76 let angles = vec![
77 0., 60., 120., 180., 240., 300., 360., // Product A
78 0., 60., 120., 180., 240., 300., 360., // Product B
79 ];
80 let values = vec![
81 7.0, 8.5, 6.0, 5.5, 9.0, 8.0, 7.0, // Product A values
82 6.0, 7.0, 8.0, 9.0, 6.5, 7.5, 6.0, // Product B values
83 ];
84 let products = vec![
85 "Product A",
86 "Product A",
87 "Product A",
88 "Product A",
89 "Product A",
90 "Product A",
91 "Product A",
92 "Product B",
93 "Product B",
94 "Product B",
95 "Product B",
96 "Product B",
97 "Product B",
98 "Product B",
99 ];
100
101 let dataset = DataFrame::new(vec![
102 Column::new("angle".into(), angles),
103 Column::new("score".into(), values),
104 Column::new("product".into(), products),
105 ])
106 .unwrap();
107
108 ScatterPolar::builder()
109 .data(&dataset)
110 .theta("angle")
111 .r("score")
112 .group("product")
113 .mode(Mode::LinesMarkers)
114 .colors(vec![
115 Rgb(255, 99, 71), // Tomato red
116 Rgb(60, 179, 113), // Medium sea green
117 ])
118 .shapes(vec![Shape::Circle, Shape::Square])
119 .lines(vec![Line::Solid, Line::Dash])
120 .width(2.5)
121 .size(8)
122 .plot_title(Text::from("Product Comparison").font("Arial").size(24))
123 .legend_title(Text::from("Products").font("Arial").size(14))
124 .legend(&Legend::new().x(0.85).y(0.95))
125 .build()
126 .plot();
127}examples/timeseriesplot.rs (line 29)
5fn main() {
6 // Example 1: Revenue and Cost with advanced styling
7 let revenue_dataset = LazyCsvReader::new(PlPath::new("data/revenue_and_cost.csv"))
8 .finish()
9 .unwrap()
10 .select([
11 col("Date").cast(DataType::String),
12 col("Revenue").cast(DataType::Int32),
13 col("Cost").cast(DataType::Int32),
14 ])
15 .collect()
16 .unwrap();
17
18 TimeSeriesPlot::builder()
19 .data(&revenue_dataset)
20 .x("Date")
21 .y("Revenue")
22 .additional_series(vec!["Cost"])
23 .size(8)
24 .colors(vec![Rgb(0, 0, 255), Rgb(255, 0, 0)])
25 .lines(vec![Line::Dash, Line::Solid])
26 .with_shape(true)
27 .shapes(vec![Shape::Circle, Shape::Square])
28 .plot_title(Text::from("Time Series Plot").font("Arial").size(18))
29 .legend(&Legend::new().x(0.05).y(0.9))
30 .x_title("x")
31 .y_title(Text::from("y").color(Rgb(0, 0, 255)))
32 .y_title2(Text::from("y2").color(Rgb(255, 0, 0)))
33 .y_axis(
34 &Axis::new()
35 .value_color(Rgb(0, 0, 255))
36 .show_grid(false)
37 .zero_line_color(Rgb(0, 0, 0)),
38 )
39 .y_axis2(
40 &Axis::new()
41 .axis_side(plotlars::AxisSide::Right)
42 .value_color(Rgb(255, 0, 0))
43 .show_grid(false),
44 )
45 .build()
46 .plot();
47
48 // Example 2: Temperature data with date parsing
49 let temperature_dataset = LazyCsvReader::new(PlPath::new("data/debilt_2023_temps.csv"))
50 .with_has_header(true)
51 .with_try_parse_dates(true)
52 .finish()
53 .unwrap()
54 .with_columns(vec![
55 (col("tavg") / lit(10)).alias("tavg"),
56 (col("tmin") / lit(10)).alias("tmin"),
57 (col("tmax") / lit(10)).alias("tmax"),
58 ])
59 .collect()
60 .unwrap();
61
62 TimeSeriesPlot::builder()
63 .data(&temperature_dataset)
64 .x("date")
65 .y("tavg")
66 .additional_series(vec!["tmin", "tmax"])
67 .colors(vec![Rgb(128, 128, 128), Rgb(0, 122, 255), Rgb(255, 128, 0)])
68 .lines(vec![Line::Solid, Line::Dot, Line::Dot])
69 .plot_title("Temperature at De Bilt (2023)")
70 .legend_title("Legend")
71 .build()
72 .plot();
73}Sourcepub fn background_color(self, color: Rgb) -> Self
pub fn background_color(self, color: Rgb) -> Self
Sets the background color of the legend.
§Argument
color- AnRgbstruct representing the background color.
Sourcepub fn border_color(self, color: Rgb) -> Self
pub fn border_color(self, color: Rgb) -> Self
Sourcepub fn border_width(self, width: usize) -> Self
pub fn border_width(self, width: usize) -> Self
Sets the border width of the legend.
§Argument
width- Ausizevalue representing the width of the border.
Examples found in repository?
examples/boxplot.rs (line 34)
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 BoxPlot::builder()
19 .data(&dataset)
20 .labels("species")
21 .values("body_mass_g")
22 .orientation(Orientation::Vertical)
23 .group("gender")
24 .box_points(true)
25 .point_offset(-1.5)
26 .jitter(0.01)
27 .opacity(0.1)
28 .colors(vec![Rgb(0, 191, 255), Rgb(57, 255, 20), Rgb(255, 105, 180)])
29 .plot_title(Text::from("Box Plot").font("Arial").size(18))
30 .x_title(Text::from("species").font("Arial").size(15))
31 .y_title(Text::from("body mass (g)").font("Arial").size(15))
32 .legend_title(Text::from("gender").font("Arial").size(15))
33 .y_axis(&Axis::new().value_thousands(true))
34 .legend(&Legend::new().border_width(1).x(0.9))
35 .build()
36 .plot();
37}Sourcepub fn font(self, font: impl Into<String>) -> Self
pub fn font(self, font: impl Into<String>) -> Self
Sets the font of the legend labels.
§Argument
font- A value that can be converted into aString, representing the font name for the labels.
Sourcepub fn orientation(self, orientation: Orientation) -> Self
pub fn orientation(self, orientation: Orientation) -> Self
Sets the orientation of the legend.
§Argument
orientation- AnOrientationenum value representing the layout direction of the legend.
Examples found in repository?
examples/barplot.rs (line 28)
5fn main() {
6 let dataset = df![
7 "animal" => &["giraffe", "giraffe", "orangutan", "orangutan", "monkey", "monkey"],
8 "gender" => &vec!["female", "male", "female", "male", "female", "male"],
9 "value" => &vec![20.0f32, 25.0, 14.0, 18.0, 23.0, 31.0],
10 "error" => &vec![1.0, 0.5, 1.5, 1.0, 0.5, 1.5],
11 ]
12 .unwrap();
13
14 BarPlot::builder()
15 .data(&dataset)
16 .labels("animal")
17 .values("value")
18 .orientation(Orientation::Vertical)
19 .group("gender")
20 .error("error")
21 .colors(vec![Rgb(255, 127, 80), Rgb(64, 224, 208)])
22 .plot_title(Text::from("Bar Plot").font("Arial").size(18))
23 .x_title(Text::from("animal").font("Arial").size(15))
24 .y_title(Text::from("value").font("Arial").size(15))
25 .legend_title(Text::from("gender").font("Arial").size(15))
26 .legend(
27 &Legend::new()
28 .orientation(Orientation::Horizontal)
29 .y(1.0)
30 .x(0.4),
31 )
32 .build()
33 .plot();
34}Sourcepub fn x(self, x: f64) -> Self
pub fn x(self, x: f64) -> Self
Sets the horizontal position of the legend.
§Argument
x- Af64value representing the horizontal position of the legend.
Examples found in repository?
examples/barplot.rs (line 30)
5fn main() {
6 let dataset = df![
7 "animal" => &["giraffe", "giraffe", "orangutan", "orangutan", "monkey", "monkey"],
8 "gender" => &vec!["female", "male", "female", "male", "female", "male"],
9 "value" => &vec![20.0f32, 25.0, 14.0, 18.0, 23.0, 31.0],
10 "error" => &vec![1.0, 0.5, 1.5, 1.0, 0.5, 1.5],
11 ]
12 .unwrap();
13
14 BarPlot::builder()
15 .data(&dataset)
16 .labels("animal")
17 .values("value")
18 .orientation(Orientation::Vertical)
19 .group("gender")
20 .error("error")
21 .colors(vec![Rgb(255, 127, 80), Rgb(64, 224, 208)])
22 .plot_title(Text::from("Bar Plot").font("Arial").size(18))
23 .x_title(Text::from("animal").font("Arial").size(15))
24 .y_title(Text::from("value").font("Arial").size(15))
25 .legend_title(Text::from("gender").font("Arial").size(15))
26 .legend(
27 &Legend::new()
28 .orientation(Orientation::Horizontal)
29 .y(1.0)
30 .x(0.4),
31 )
32 .build()
33 .plot();
34}More examples
examples/histogram.rs (line 36)
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}examples/boxplot.rs (line 34)
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 BoxPlot::builder()
19 .data(&dataset)
20 .labels("species")
21 .values("body_mass_g")
22 .orientation(Orientation::Vertical)
23 .group("gender")
24 .box_points(true)
25 .point_offset(-1.5)
26 .jitter(0.01)
27 .opacity(0.1)
28 .colors(vec![Rgb(0, 191, 255), Rgb(57, 255, 20), Rgb(255, 105, 180)])
29 .plot_title(Text::from("Box Plot").font("Arial").size(18))
30 .x_title(Text::from("species").font("Arial").size(15))
31 .y_title(Text::from("body mass (g)").font("Arial").size(15))
32 .legend_title(Text::from("gender").font("Arial").size(15))
33 .y_axis(&Axis::new().value_thousands(true))
34 .legend(&Legend::new().border_width(1).x(0.9))
35 .build()
36 .plot();
37}examples/scatterplot.rs (line 38)
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 .tick_direction(TickDirection::OutSide)
21 .value_thousands(true);
22
23 ScatterPlot::builder()
24 .data(&dataset)
25 .x("body_mass_g")
26 .y("flipper_length_mm")
27 .group("species")
28 .opacity(0.5)
29 .size(12)
30 .colors(vec![Rgb(178, 34, 34), Rgb(65, 105, 225), Rgb(255, 140, 0)])
31 .shapes(vec![Shape::Circle, Shape::Square, Shape::Diamond])
32 .plot_title(Text::from("Scatter Plot").font("Arial").size(20).x(0.065))
33 .x_title("body mass (g)")
34 .y_title("flipper length (mm)")
35 .legend_title("species")
36 .x_axis(&axis.clone().value_range(vec![2500.0, 6500.0]))
37 .y_axis(&axis.clone().value_range(vec![170.0, 240.0]))
38 .legend(&Legend::new().x(0.85).y(0.15))
39 .build()
40 .plot();
41}examples/scatterpolar.rs (line 124)
74fn grouped_scatter_polar() {
75 // Create sample data - comparing two products across multiple metrics
76 let angles = vec![
77 0., 60., 120., 180., 240., 300., 360., // Product A
78 0., 60., 120., 180., 240., 300., 360., // Product B
79 ];
80 let values = vec![
81 7.0, 8.5, 6.0, 5.5, 9.0, 8.0, 7.0, // Product A values
82 6.0, 7.0, 8.0, 9.0, 6.5, 7.5, 6.0, // Product B values
83 ];
84 let products = vec![
85 "Product A",
86 "Product A",
87 "Product A",
88 "Product A",
89 "Product A",
90 "Product A",
91 "Product A",
92 "Product B",
93 "Product B",
94 "Product B",
95 "Product B",
96 "Product B",
97 "Product B",
98 "Product B",
99 ];
100
101 let dataset = DataFrame::new(vec![
102 Column::new("angle".into(), angles),
103 Column::new("score".into(), values),
104 Column::new("product".into(), products),
105 ])
106 .unwrap();
107
108 ScatterPolar::builder()
109 .data(&dataset)
110 .theta("angle")
111 .r("score")
112 .group("product")
113 .mode(Mode::LinesMarkers)
114 .colors(vec![
115 Rgb(255, 99, 71), // Tomato red
116 Rgb(60, 179, 113), // Medium sea green
117 ])
118 .shapes(vec![Shape::Circle, Shape::Square])
119 .lines(vec![Line::Solid, Line::Dash])
120 .width(2.5)
121 .size(8)
122 .plot_title(Text::from("Product Comparison").font("Arial").size(24))
123 .legend_title(Text::from("Products").font("Arial").size(14))
124 .legend(&Legend::new().x(0.85).y(0.95))
125 .build()
126 .plot();
127}examples/timeseriesplot.rs (line 29)
5fn main() {
6 // Example 1: Revenue and Cost with advanced styling
7 let revenue_dataset = LazyCsvReader::new(PlPath::new("data/revenue_and_cost.csv"))
8 .finish()
9 .unwrap()
10 .select([
11 col("Date").cast(DataType::String),
12 col("Revenue").cast(DataType::Int32),
13 col("Cost").cast(DataType::Int32),
14 ])
15 .collect()
16 .unwrap();
17
18 TimeSeriesPlot::builder()
19 .data(&revenue_dataset)
20 .x("Date")
21 .y("Revenue")
22 .additional_series(vec!["Cost"])
23 .size(8)
24 .colors(vec![Rgb(0, 0, 255), Rgb(255, 0, 0)])
25 .lines(vec![Line::Dash, Line::Solid])
26 .with_shape(true)
27 .shapes(vec![Shape::Circle, Shape::Square])
28 .plot_title(Text::from("Time Series Plot").font("Arial").size(18))
29 .legend(&Legend::new().x(0.05).y(0.9))
30 .x_title("x")
31 .y_title(Text::from("y").color(Rgb(0, 0, 255)))
32 .y_title2(Text::from("y2").color(Rgb(255, 0, 0)))
33 .y_axis(
34 &Axis::new()
35 .value_color(Rgb(0, 0, 255))
36 .show_grid(false)
37 .zero_line_color(Rgb(0, 0, 0)),
38 )
39 .y_axis2(
40 &Axis::new()
41 .axis_side(plotlars::AxisSide::Right)
42 .value_color(Rgb(255, 0, 0))
43 .show_grid(false),
44 )
45 .build()
46 .plot();
47
48 // Example 2: Temperature data with date parsing
49 let temperature_dataset = LazyCsvReader::new(PlPath::new("data/debilt_2023_temps.csv"))
50 .with_has_header(true)
51 .with_try_parse_dates(true)
52 .finish()
53 .unwrap()
54 .with_columns(vec![
55 (col("tavg") / lit(10)).alias("tavg"),
56 (col("tmin") / lit(10)).alias("tmin"),
57 (col("tmax") / lit(10)).alias("tmax"),
58 ])
59 .collect()
60 .unwrap();
61
62 TimeSeriesPlot::builder()
63 .data(&temperature_dataset)
64 .x("date")
65 .y("tavg")
66 .additional_series(vec!["tmin", "tmax"])
67 .colors(vec![Rgb(128, 128, 128), Rgb(0, 122, 255), Rgb(255, 128, 0)])
68 .lines(vec![Line::Solid, Line::Dot, Line::Dot])
69 .plot_title("Temperature at De Bilt (2023)")
70 .legend_title("Legend")
71 .build()
72 .plot();
73}Sourcepub fn y(self, y: f64) -> Self
pub fn y(self, y: f64) -> Self
Sets the vertical position of the legend.
§Argument
y- Af64value representing the vertical position of the legend.
Examples found in repository?
examples/barplot.rs (line 29)
5fn main() {
6 let dataset = df![
7 "animal" => &["giraffe", "giraffe", "orangutan", "orangutan", "monkey", "monkey"],
8 "gender" => &vec!["female", "male", "female", "male", "female", "male"],
9 "value" => &vec![20.0f32, 25.0, 14.0, 18.0, 23.0, 31.0],
10 "error" => &vec![1.0, 0.5, 1.5, 1.0, 0.5, 1.5],
11 ]
12 .unwrap();
13
14 BarPlot::builder()
15 .data(&dataset)
16 .labels("animal")
17 .values("value")
18 .orientation(Orientation::Vertical)
19 .group("gender")
20 .error("error")
21 .colors(vec![Rgb(255, 127, 80), Rgb(64, 224, 208)])
22 .plot_title(Text::from("Bar Plot").font("Arial").size(18))
23 .x_title(Text::from("animal").font("Arial").size(15))
24 .y_title(Text::from("value").font("Arial").size(15))
25 .legend_title(Text::from("gender").font("Arial").size(15))
26 .legend(
27 &Legend::new()
28 .orientation(Orientation::Horizontal)
29 .y(1.0)
30 .x(0.4),
31 )
32 .build()
33 .plot();
34}More examples
examples/scatterplot.rs (line 38)
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 .tick_direction(TickDirection::OutSide)
21 .value_thousands(true);
22
23 ScatterPlot::builder()
24 .data(&dataset)
25 .x("body_mass_g")
26 .y("flipper_length_mm")
27 .group("species")
28 .opacity(0.5)
29 .size(12)
30 .colors(vec![Rgb(178, 34, 34), Rgb(65, 105, 225), Rgb(255, 140, 0)])
31 .shapes(vec![Shape::Circle, Shape::Square, Shape::Diamond])
32 .plot_title(Text::from("Scatter Plot").font("Arial").size(20).x(0.065))
33 .x_title("body mass (g)")
34 .y_title("flipper length (mm)")
35 .legend_title("species")
36 .x_axis(&axis.clone().value_range(vec![2500.0, 6500.0]))
37 .y_axis(&axis.clone().value_range(vec![170.0, 240.0]))
38 .legend(&Legend::new().x(0.85).y(0.15))
39 .build()
40 .plot();
41}examples/scatterpolar.rs (line 124)
74fn grouped_scatter_polar() {
75 // Create sample data - comparing two products across multiple metrics
76 let angles = vec![
77 0., 60., 120., 180., 240., 300., 360., // Product A
78 0., 60., 120., 180., 240., 300., 360., // Product B
79 ];
80 let values = vec![
81 7.0, 8.5, 6.0, 5.5, 9.0, 8.0, 7.0, // Product A values
82 6.0, 7.0, 8.0, 9.0, 6.5, 7.5, 6.0, // Product B values
83 ];
84 let products = vec![
85 "Product A",
86 "Product A",
87 "Product A",
88 "Product A",
89 "Product A",
90 "Product A",
91 "Product A",
92 "Product B",
93 "Product B",
94 "Product B",
95 "Product B",
96 "Product B",
97 "Product B",
98 "Product B",
99 ];
100
101 let dataset = DataFrame::new(vec![
102 Column::new("angle".into(), angles),
103 Column::new("score".into(), values),
104 Column::new("product".into(), products),
105 ])
106 .unwrap();
107
108 ScatterPolar::builder()
109 .data(&dataset)
110 .theta("angle")
111 .r("score")
112 .group("product")
113 .mode(Mode::LinesMarkers)
114 .colors(vec![
115 Rgb(255, 99, 71), // Tomato red
116 Rgb(60, 179, 113), // Medium sea green
117 ])
118 .shapes(vec![Shape::Circle, Shape::Square])
119 .lines(vec![Line::Solid, Line::Dash])
120 .width(2.5)
121 .size(8)
122 .plot_title(Text::from("Product Comparison").font("Arial").size(24))
123 .legend_title(Text::from("Products").font("Arial").size(14))
124 .legend(&Legend::new().x(0.85).y(0.95))
125 .build()
126 .plot();
127}examples/timeseriesplot.rs (line 29)
5fn main() {
6 // Example 1: Revenue and Cost with advanced styling
7 let revenue_dataset = LazyCsvReader::new(PlPath::new("data/revenue_and_cost.csv"))
8 .finish()
9 .unwrap()
10 .select([
11 col("Date").cast(DataType::String),
12 col("Revenue").cast(DataType::Int32),
13 col("Cost").cast(DataType::Int32),
14 ])
15 .collect()
16 .unwrap();
17
18 TimeSeriesPlot::builder()
19 .data(&revenue_dataset)
20 .x("Date")
21 .y("Revenue")
22 .additional_series(vec!["Cost"])
23 .size(8)
24 .colors(vec![Rgb(0, 0, 255), Rgb(255, 0, 0)])
25 .lines(vec![Line::Dash, Line::Solid])
26 .with_shape(true)
27 .shapes(vec![Shape::Circle, Shape::Square])
28 .plot_title(Text::from("Time Series Plot").font("Arial").size(18))
29 .legend(&Legend::new().x(0.05).y(0.9))
30 .x_title("x")
31 .y_title(Text::from("y").color(Rgb(0, 0, 255)))
32 .y_title2(Text::from("y2").color(Rgb(255, 0, 0)))
33 .y_axis(
34 &Axis::new()
35 .value_color(Rgb(0, 0, 255))
36 .show_grid(false)
37 .zero_line_color(Rgb(0, 0, 0)),
38 )
39 .y_axis2(
40 &Axis::new()
41 .axis_side(plotlars::AxisSide::Right)
42 .value_color(Rgb(255, 0, 0))
43 .show_grid(false),
44 )
45 .build()
46 .plot();
47
48 // Example 2: Temperature data with date parsing
49 let temperature_dataset = LazyCsvReader::new(PlPath::new("data/debilt_2023_temps.csv"))
50 .with_has_header(true)
51 .with_try_parse_dates(true)
52 .finish()
53 .unwrap()
54 .with_columns(vec![
55 (col("tavg") / lit(10)).alias("tavg"),
56 (col("tmin") / lit(10)).alias("tmin"),
57 (col("tmax") / lit(10)).alias("tmax"),
58 ])
59 .collect()
60 .unwrap();
61
62 TimeSeriesPlot::builder()
63 .data(&temperature_dataset)
64 .x("date")
65 .y("tavg")
66 .additional_series(vec!["tmin", "tmax"])
67 .colors(vec![Rgb(128, 128, 128), Rgb(0, 122, 255), Rgb(255, 128, 0)])
68 .lines(vec![Line::Solid, Line::Dot, Line::Dot])
69 .plot_title("Temperature at De Bilt (2023)")
70 .legend_title("Legend")
71 .build()
72 .plot();
73}Trait Implementations§
Auto Trait Implementations§
impl Freeze for Legend
impl RefUnwindSafe for Legend
impl Send for Legend
impl Sync for Legend
impl Unpin for Legend
impl UnwindSafe for Legend
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 moreSource§impl<T> Key for Twhere
T: Clone,
impl<T> Key for Twhere
T: Clone,
Source§impl<T> Pointable for T
impl<T> Pointable for T
Source§impl<T> PolicyExt for Twhere
T: ?Sized,
impl<T> PolicyExt for Twhere
T: ?Sized,
Source§impl<R, P> ReadPrimitive<R> for P
impl<R, P> ReadPrimitive<R> for P
Source§fn read_from_little_endian(read: &mut R) -> Result<Self, Error>
fn read_from_little_endian(read: &mut R) -> Result<Self, Error>
Read this value from the supplied reader. Same as
ReadEndian::read_from_little_endian().