Skip to main content

lineplot/
lineplot.rs

1use plotlars::{Axis, Line, LinePlot, Plot, Rgb, Text, TickDirection};
2use polars::prelude::*;
3
4fn main() {
5    let x_values: Vec<f64> = (0..1000)
6        .map(|i| {
7            let step = (2.0 * std::f64::consts::PI - 0.0) / 999.0;
8            0.0 + step * i as f64
9        })
10        .collect();
11    let sine_values = x_values
12        .iter()
13        .map(|arg0: &f64| f64::sin(*arg0))
14        .collect::<Vec<_>>();
15    let cosine_values = x_values
16        .iter()
17        .map(|arg0: &f64| f64::cos(*arg0))
18        .collect::<Vec<_>>();
19
20    let dataset = df![
21        "x" => &x_values,
22        "sine" => &sine_values,
23        "cosine" => &cosine_values,
24    ]
25    .unwrap();
26
27    LinePlot::builder()
28        .data(&dataset)
29        .x("x")
30        .y("sine")
31        .additional_lines(vec!["cosine"])
32        .colors(vec![Rgb(255, 0, 0), Rgb(0, 255, 0)])
33        .lines(vec![Line::Solid, Line::Dot])
34        .width(3.0)
35        .with_shape(false)
36        .plot_title(Text::from("Line Plot").font("Arial").size(18))
37        .x_axis(
38            &Axis::new()
39                .tick_direction(TickDirection::OutSide)
40                .axis_position(0.5)
41                .tick_values(vec![
42                    0.5 * std::f64::consts::PI,
43                    std::f64::consts::PI,
44                    1.5 * std::f64::consts::PI,
45                    2.0 * std::f64::consts::PI,
46                ])
47                .tick_labels(vec!["π/2", "π", "3π/2", "2π"]),
48        )
49        .y_axis(
50            &Axis::new()
51                .tick_direction(TickDirection::OutSide)
52                .tick_values(vec![-1.0, 0.0, 1.0])
53                .tick_labels(vec!["-1", "0", "1"]),
54        )
55        .build()
56        .plot();
57}