pub struct SurfacePlot { /* private fields */ }Expand description
A structure representing a 3-D surface plot.
The SurfacePlot struct is designed to build and customize 3-dimensional
surface visualizations. It supports fine-grained control over the color
mapping of z values, interactive color bars, lighting effects that enhance
depth perception, and global opacity settings. Layout elements such as the
plot title and axis labels can also be configured through the builder API,
allowing you to embed the surface seamlessly in complex dashboards.
§Arguments
data– A reference to theDataFramethat supplies the data. It must contain three numeric columns whose names are given byx,yandz.x– The column name to be used for the x-axis coordinates. Each distinct x value becomes a row in the underlying z grid.y– The column name to be used for the y-axis coordinates. Each distinct y value becomes a column in the z grid.z– The column name that provides the z-axis heights. These values are mapped to colors according tocolor_scale/reverse_scale.color_bar– An optional Reference to aColorBardescribing the appearance of the color legend (length, tick formatting, border, etc.).color_scale– An optionalPalettethat defines the color gradient (e.g. Viridis, Cividis).reverse_scale– An optionalboolindicating whether the chosencolor_scaleshould run in the opposite direction.show_scale– An optionalboolthat toggles the visibility of the color bar. Useful when you have multiple surfaces that share an external legend.lighting– An optional Reference to aLightingstruct that specifies ambient, diffuse, specular components, roughness, fresnel and light position. Leaving itNoneapplies Plotly’s default Phong shading.opacity– An optionalf64in[0.0, 1.0]that sets the global transparency of the surface (1 = opaque, 0 = fully transparent).facet– An optional string slice specifying the column name to create faceted subplots (one surface per category).facet_config– An optional reference to aFacetConfigstruct for customizing facet layout (ncol, nrow, gap sizes, etc.).plot_title– An optionalTextthat customizes the title (content, font, size, alignment).legend– An optional reference to aLegendstruct for legend customization.
§Example
use ndarray::Array;
use plotlars::{ColorBar, Lighting, Palette, Plot, SurfacePlot, Text};
use polars::prelude::*;
use std::iter;
let n: usize = 100;
let (x_base, _): (Vec<f64>, Option<usize>) = Array::linspace(-10.0, 10.0, n).into_raw_vec_and_offset();
let (y_base, _): (Vec<f64>, Option<usize>) = Array::linspace(-10.0, 10.0, n).into_raw_vec_and_offset();
let x = x_base
.iter()
.flat_map(|&xi| iter::repeat_n(xi, n))
.collect::<Vec<_>>();
let y = y_base
.iter()
.cycle()
.take(n * n)
.cloned()
.collect::<Vec<_>>();
let z = x_base
.iter()
.flat_map(|i| {
y_base
.iter()
.map(|j| 1.0 / (j * j + 5.0) * j.sin() + 1.0 / (i * i + 5.0) * i.cos())
.collect::<Vec<_>>()
})
.collect::<Vec<_>>();
let dataset = df![
"x" => &x,
"y" => &y,
"z" => &z,
]
.unwrap();
SurfacePlot::builder()
.data(&dataset)
.x("x")
.y("y")
.z("z")
.plot_title(
Text::from("Surface Plot")
.font("Arial")
.size(18),
)
.color_bar(
&ColorBar::new()
.border_width(1),
)
.color_scale(Palette::Cividis)
.reverse_scale(true)
.opacity(0.5)
.build()
.plot();
Implementations§
Source§impl SurfacePlot
impl SurfacePlot
Sourcepub fn builder<'f1, 'f2, 'f3, 'f4, 'f5, 'f6, 'f7, 'f8, 'f9>() -> SurfacePlotBuilder<'f1, 'f2, 'f3, 'f4, 'f5, 'f6, 'f7, 'f8, 'f9>
pub fn builder<'f1, 'f2, 'f3, 'f4, 'f5, 'f6, 'f7, 'f8, 'f9>() -> SurfacePlotBuilder<'f1, 'f2, 'f3, 'f4, 'f5, 'f6, 'f7, 'f8, 'f9>
Examples found in repository?
examples/surfaceplot.rs (line 42)
6fn main() {
7 let n: usize = 100;
8 let (x_base, _): (Vec<f64>, Option<usize>) =
9 Array::linspace(-10.0, 10.0, n).into_raw_vec_and_offset();
10 let (y_base, _): (Vec<f64>, Option<usize>) =
11 Array::linspace(-10.0, 10.0, n).into_raw_vec_and_offset();
12
13 let x = x_base
14 .iter()
15 .flat_map(|&xi| iter::repeat_n(xi, n))
16 .collect::<Vec<_>>();
17
18 let y = y_base
19 .iter()
20 .cycle()
21 .take(n * n)
22 .cloned()
23 .collect::<Vec<_>>();
24
25 let z = x_base
26 .iter()
27 .flat_map(|i| {
28 y_base
29 .iter()
30 .map(|j| 1.0 / (j * j + 5.0) * j.sin() + 1.0 / (i * i + 5.0) * i.cos())
31 .collect::<Vec<_>>()
32 })
33 .collect::<Vec<_>>();
34
35 let dataset = df![
36 "x" => &x,
37 "y" => &y,
38 "z" => &z,
39 ]
40 .unwrap();
41
42 SurfacePlot::builder()
43 .data(&dataset)
44 .x("x")
45 .y("y")
46 .z("z")
47 .plot_title(Text::from("Surface Plot").font("Arial").size(18))
48 .color_bar(&ColorBar::new().border_width(1))
49 .color_scale(Palette::Cividis)
50 .reverse_scale(true)
51 .opacity(0.5)
52 .build()
53 .plot();
54}More examples
examples/faceting.rs (line 653)
609fn surfaceplot_example() {
610 let n: usize = 50;
611 let (x_base, _): (Vec<f64>, Option<usize>) =
612 Array::linspace(-5., 5., n).into_raw_vec_and_offset();
613 let (y_base, _): (Vec<f64>, Option<usize>) =
614 Array::linspace(-5., 5., n).into_raw_vec_and_offset();
615
616 let mut x_all = Vec::new();
617 let mut y_all = Vec::new();
618 let mut z_all = Vec::new();
619 let mut category_all = Vec::new();
620
621 type SurfaceFunction = Box<dyn Fn(f64, f64) -> f64>;
622 let functions: Vec<(&str, SurfaceFunction)> = vec![
623 (
624 "Sine Wave",
625 Box::new(|xi: f64, yj: f64| (xi * xi + yj * yj).sqrt().sin()),
626 ),
627 ("Saddle", Box::new(|xi: f64, yj: f64| xi * xi - yj * yj)),
628 (
629 "Gaussian",
630 Box::new(|xi: f64, yj: f64| (-0.5 * (xi * xi + yj * yj)).exp()),
631 ),
632 ];
633
634 for (name, func) in &functions {
635 for &xi in x_base.iter() {
636 for &yj in y_base.iter() {
637 x_all.push(xi);
638 y_all.push(yj);
639 z_all.push(func(xi, yj));
640 category_all.push(*name);
641 }
642 }
643 }
644
645 let dataset = df![
646 "x" => &x_all,
647 "y" => &y_all,
648 "z" => &z_all,
649 "function" => &category_all,
650 ]
651 .unwrap();
652
653 SurfacePlot::builder()
654 .data(&dataset)
655 .x("x")
656 .y("y")
657 .z("z")
658 .facet("function")
659 .facet_config(&FacetConfig::new().cols(3).rows(1).h_gap(0.08).v_gap(0.12))
660 .plot_title(
661 Text::from("3D Mathematical Functions")
662 .font("Arial")
663 .size(20),
664 )
665 .color_scale(Palette::Viridis)
666 .opacity(0.9)
667 .build()
668 .plot();
669}Trait Implementations§
Source§impl Clone for SurfacePlot
impl Clone for SurfacePlot
Source§fn clone(&self) -> SurfacePlot
fn clone(&self) -> SurfacePlot
Returns a duplicate of the value. Read more
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
Performs copy-assignment from
source. Read moreSource§impl Serialize for SurfacePlot
impl Serialize for SurfacePlot
impl PlotHelper for SurfacePlot
Auto Trait Implementations§
impl Freeze for SurfacePlot
impl !RefUnwindSafe for SurfacePlot
impl !Send for SurfacePlot
impl !Sync for SurfacePlot
impl Unpin for SurfacePlot
impl !UnwindSafe for SurfacePlot
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 more