• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 use plotters::prelude::*;
main() -> Result<(), Box<dyn std::error::Error>>2 fn main() -> Result<(), Box<dyn std::error::Error>> {
3     let area = SVGBackend::new("plotters-doc-data/3d-plot.svg", (1024, 760)).into_drawing_area();
4 
5     area.fill(&WHITE)?;
6 
7     let x_axis = (-3.0..3.0).step(0.1);
8     let z_axis = (-3.0..3.0).step(0.1);
9 
10     let mut chart = ChartBuilder::on(&area)
11         .caption(format!("3D Plot Test"), ("sans", 20))
12         .build_cartesian_3d(x_axis.clone(), -3.0..3.0, z_axis.clone())?;
13 
14     chart.with_projection(|mut pb| {
15         pb.yaw = 0.5;
16         pb.scale = 0.9;
17         pb.into_matrix()
18     });
19 
20     chart.configure_axes().draw()?;
21 
22     chart
23         .draw_series(SurfaceSeries::<f64, _, f64>::new(
24             x_axis.values(),
25             z_axis.values(),
26             |&x, &z| (x * x + z * z).cos(),
27             &BLUE.mix(0.2),
28         ))?
29         .label("Surface")
30         .legend(|(x, y)| Rectangle::new([(x + 5, y - 5), (x + 15, y + 5)], BLUE.mix(0.5).filled()));
31 
32     chart
33         .draw_series(LineSeries::new(
34             (-100..100)
35                 .map(|y| y as f64 / 40.0)
36                 .map(|y| ((y * 10.0).sin(), y, (y * 10.0).cos())),
37             &BLACK,
38         ))?
39         .label("Line")
40         .legend(|(x, y)| PathElement::new(vec![(x, y), (x + 20, y)], &BLACK));
41 
42     chart
43         .configure_series_labels()
44         .border_style(&BLACK)
45         .draw()?;
46     Ok(())
47 }
48