Legend

Struct Legend 

Source
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();

example

Implementations§

Source§

impl Legend

Source

pub fn new() -> Self

Creates a new Legend instance with default values.

Examples found in repository?
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}
More examples
Hide additional examples
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/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        //sort by length of group name
21        .sort_groups_by(|a,b| a.len().cmp(&b.len())) 
22        .error("error")
23        .colors(vec![Rgb(255, 127, 80), Rgb(64, 224, 208)])
24        .plot_title(Text::from("Bar Plot").font("Arial").size(18))
25        .x_title(Text::from("animal").font("Arial").size(15))
26        .y_title(Text::from("value").font("Arial").size(15))
27        .legend_title(Text::from("gender").font("Arial").size(15))
28        .legend(
29            &Legend::new()
30                .orientation(Orientation::Horizontal)
31                .y(1.0)
32                .x(0.4),
33        )
34        .build()
35        .plot();
36}
examples/scatterplot.rs (line 39)
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        .sort_groups_by(|a, b| if a.len() == b.len() { a.cmp(b) } else { a.len().cmp(&b.len()) }) //sort by length unless equal length then lexical
29        .opacity(0.5)
30        .size(12)
31        .colors(vec![Rgb(178, 34, 34), Rgb(65, 105, 225), Rgb(255, 140, 0)])
32        .shapes(vec![Shape::Circle, Shape::Square, Shape::Diamond])
33        .plot_title(Text::from("Scatter Plot").font("Arial").size(20).x(0.065))
34        .x_title("body mass (g)")
35        .y_title("flipper length (mm)")
36        .legend_title("species")
37        .x_axis(&axis.clone().value_range(vec![2500.0, 6500.0]))
38        .y_axis(&axis.clone().value_range(vec![170.0, 240.0]))
39        .legend(&Legend::new().x(0.85).y(0.15))
40        .build()
41        .plot();
42}
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}
Source

pub fn background_color(self, color: Rgb) -> Self

Sets the background color of the legend.

§Argument
  • color - An Rgb struct representing the background color.
Source

pub fn border_color(self, color: Rgb) -> Self

Sets the border color of the legend.

§Argument
  • color - An Rgb struct representing the border color.
Source

pub fn border_width(self, width: usize) -> Self

Sets the border width of the legend.

§Argument
  • width - A usize value 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}
Source

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 a String, representing the font name for the labels.
Source

pub fn orientation(self, orientation: Orientation) -> Self

Sets the orientation of the legend.

§Argument
  • orientation - An Orientation enum value representing the layout direction 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        //sort by length of group name
21        .sort_groups_by(|a,b| a.len().cmp(&b.len())) 
22        .error("error")
23        .colors(vec![Rgb(255, 127, 80), Rgb(64, 224, 208)])
24        .plot_title(Text::from("Bar Plot").font("Arial").size(18))
25        .x_title(Text::from("animal").font("Arial").size(15))
26        .y_title(Text::from("value").font("Arial").size(15))
27        .legend_title(Text::from("gender").font("Arial").size(15))
28        .legend(
29            &Legend::new()
30                .orientation(Orientation::Horizontal)
31                .y(1.0)
32                .x(0.4),
33        )
34        .build()
35        .plot();
36}
Source

pub fn x(self, x: f64) -> Self

Sets the horizontal position of the legend.

§Argument
  • x - A f64 value representing the horizontal position of the legend.
Examples found in repository?
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}
More examples
Hide additional examples
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/barplot.rs (line 32)
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        //sort by length of group name
21        .sort_groups_by(|a,b| a.len().cmp(&b.len())) 
22        .error("error")
23        .colors(vec![Rgb(255, 127, 80), Rgb(64, 224, 208)])
24        .plot_title(Text::from("Bar Plot").font("Arial").size(18))
25        .x_title(Text::from("animal").font("Arial").size(15))
26        .y_title(Text::from("value").font("Arial").size(15))
27        .legend_title(Text::from("gender").font("Arial").size(15))
28        .legend(
29            &Legend::new()
30                .orientation(Orientation::Horizontal)
31                .y(1.0)
32                .x(0.4),
33        )
34        .build()
35        .plot();
36}
examples/scatterplot.rs (line 39)
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        .sort_groups_by(|a, b| if a.len() == b.len() { a.cmp(b) } else { a.len().cmp(&b.len()) }) //sort by length unless equal length then lexical
29        .opacity(0.5)
30        .size(12)
31        .colors(vec![Rgb(178, 34, 34), Rgb(65, 105, 225), Rgb(255, 140, 0)])
32        .shapes(vec![Shape::Circle, Shape::Square, Shape::Diamond])
33        .plot_title(Text::from("Scatter Plot").font("Arial").size(20).x(0.065))
34        .x_title("body mass (g)")
35        .y_title("flipper length (mm)")
36        .legend_title("species")
37        .x_axis(&axis.clone().value_range(vec![2500.0, 6500.0]))
38        .y_axis(&axis.clone().value_range(vec![170.0, 240.0]))
39        .legend(&Legend::new().x(0.85).y(0.15))
40        .build()
41        .plot();
42}
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}
Source

pub fn y(self, y: f64) -> Self

Sets the vertical position of the legend.

§Argument
  • y - A f64 value representing the vertical position of the legend.
Examples found in repository?
examples/barplot.rs (line 31)
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        //sort by length of group name
21        .sort_groups_by(|a,b| a.len().cmp(&b.len())) 
22        .error("error")
23        .colors(vec![Rgb(255, 127, 80), Rgb(64, 224, 208)])
24        .plot_title(Text::from("Bar Plot").font("Arial").size(18))
25        .x_title(Text::from("animal").font("Arial").size(15))
26        .y_title(Text::from("value").font("Arial").size(15))
27        .legend_title(Text::from("gender").font("Arial").size(15))
28        .legend(
29            &Legend::new()
30                .orientation(Orientation::Horizontal)
31                .y(1.0)
32                .x(0.4),
33        )
34        .build()
35        .plot();
36}
More examples
Hide additional examples
examples/scatterplot.rs (line 39)
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        .sort_groups_by(|a, b| if a.len() == b.len() { a.cmp(b) } else { a.len().cmp(&b.len()) }) //sort by length unless equal length then lexical
29        .opacity(0.5)
30        .size(12)
31        .colors(vec![Rgb(178, 34, 34), Rgb(65, 105, 225), Rgb(255, 140, 0)])
32        .shapes(vec![Shape::Circle, Shape::Square, Shape::Diamond])
33        .plot_title(Text::from("Scatter Plot").font("Arial").size(20).x(0.065))
34        .x_title("body mass (g)")
35        .y_title("flipper length (mm)")
36        .legend_title("species")
37        .x_axis(&axis.clone().value_range(vec![2500.0, 6500.0]))
38        .y_axis(&axis.clone().value_range(vec![170.0, 240.0]))
39        .legend(&Legend::new().x(0.85).y(0.15))
40        .build()
41        .plot();
42}
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§

Source§

impl Clone for Legend

Source§

fn clone(&self) -> Legend

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Default for Legend

Source§

fn default() -> Legend

Returns the “default value” for a type. Read more

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> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> DynClone for T
where T: Clone,

Source§

fn __clone_box(&self, _: Private) -> *mut ()

Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> IntoEither for T

Source§

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 more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

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
Source§

impl<T> Key for T
where T: Clone,

Source§

fn align() -> usize

The alignment necessary for the key. Must return a power of two.
Source§

fn size(&self) -> usize

The size of the key in bytes.
Source§

unsafe fn init(&self, ptr: *mut u8)

Initialize the key in the given memory location. Read more
Source§

unsafe fn get<'a>(ptr: *const u8) -> &'a T

Get a reference to the key from the given memory location. Read more
Source§

unsafe fn drop_in_place(ptr: *mut u8)

Drop the key in place. Read more
Source§

impl<T> Pointable for T

Source§

const ALIGN: usize

The alignment of pointer.
Source§

type Init = T

The type for initializers.
Source§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
Source§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
Source§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
Source§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
Source§

impl<T> PolicyExt for T
where T: ?Sized,

Source§

fn and<P, B, E>(self, other: P) -> And<T, P>
where T: Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow only if self and other return Action::Follow. Read more
Source§

fn or<P, B, E>(self, other: P) -> Or<T, P>
where T: Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow if either self or other returns Action::Follow. Read more
Source§

impl<R, P> ReadPrimitive<R> for P
where R: Read + ReadEndian<P>, P: Default,

Source§

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().
Source§

fn read_from_big_endian(read: &mut R) -> Result<Self, Error>

Read this value from the supplied reader. Same as ReadEndian::read_from_big_endian().
Source§

fn read_from_native_endian(read: &mut R) -> Result<Self, Error>

Read this value from the supplied reader. Same as ReadEndian::read_from_native_endian().
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V

Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

impl<T> ErasedDestructor for T
where T: 'static,

Source§

impl<T> PlanCallbackArgs for T

Source§

impl<T> PlanCallbackOut for T