aboutsummaryrefslogtreecommitdiff
path: root/examples/3d-plot.rs
diff options
context:
space:
mode:
Diffstat (limited to 'examples/3d-plot.rs')
-rw-r--r--examples/3d-plot.rs47
1 files changed, 47 insertions, 0 deletions
diff --git a/examples/3d-plot.rs b/examples/3d-plot.rs
new file mode 100644
index 0000000..7d27e1f
--- /dev/null
+++ b/examples/3d-plot.rs
@@ -0,0 +1,47 @@
+use plotters::prelude::*;
+fn main() -> Result<(), Box<dyn std::error::Error>> {
+ let area = SVGBackend::new("plotters-doc-data/3d-plot.svg", (1024, 760)).into_drawing_area();
+
+ area.fill(&WHITE)?;
+
+ let x_axis = (-3.0..3.0).step(0.1);
+ let z_axis = (-3.0..3.0).step(0.1);
+
+ let mut chart = ChartBuilder::on(&area)
+ .caption(format!("3D Plot Test"), ("sans", 20))
+ .build_cartesian_3d(x_axis.clone(), -3.0..3.0, z_axis.clone())?;
+
+ chart.with_projection(|mut pb| {
+ pb.yaw = 0.5;
+ pb.scale = 0.9;
+ pb.into_matrix()
+ });
+
+ chart.configure_axes().draw()?;
+
+ chart
+ .draw_series(SurfaceSeries::<f64, _, f64>::new(
+ x_axis.values(),
+ z_axis.values(),
+ |&x, &z| (x * x + z * z).cos(),
+ &BLUE.mix(0.2),
+ ))?
+ .label("Surface")
+ .legend(|(x, y)| Rectangle::new([(x + 5, y - 5), (x + 15, y + 5)], BLUE.mix(0.5).filled()));
+
+ chart
+ .draw_series(LineSeries::new(
+ (-100..100)
+ .map(|y| y as f64 / 40.0)
+ .map(|y| ((y * 10.0).sin(), y, (y * 10.0).cos())),
+ &BLACK,
+ ))?
+ .label("Line")
+ .legend(|(x, y)| PathElement::new(vec![(x, y), (x + 20, y)], &BLACK));
+
+ chart
+ .configure_series_labels()
+ .border_style(&BLACK)
+ .draw()?;
+ Ok(())
+}