pub struct ContourPlot { /* private fields */ }Expand description
A structure representing a contour plot.
The ContourPlot struct enables the creation of contour visualizations that display level
curves of a three‑dimensional surface on a two‑dimensional plane. It offers extensive
configuration options for contour styling, color scaling, axis appearance, legends, and
annotations. Users can fine‑tune the contour interval, choose from predefined color palettes,
reverse or hide the color scale, and set custom titles for both the plot and its axes in
order to improve the readability of complex surfaces.
§Backend Support
| Backend | Supported |
|---|---|
| Plotly | Yes |
| Plotters | – |
§Arguments
data- A reference to theDataFramecontaining the data to be plotted.x- A string slice specifying the column name for x‑axis values.y- A string slice specifying the column name for y‑axis values.z- A string slice specifying the column name for z‑axis values whose magnitude determines each contour line.facet- An optional string slice specifying the column name to be used for faceting (creating multiple subplots).facet_config- An optional reference to aFacetConfigstruct for customizing facet behavior (grid dimensions, scales, gaps, etc.).color_bar- An optional reference to aColorBarstruct for customizing the color bar appearance.color_scale- An optionalPaletteenum for specifying the color palette (e.g.,Palette::Viridis).reverse_scale- An optional boolean to reverse the color scale direction.show_scale- An optional boolean to display the color scale on the plot.contours- An optional reference to aContoursstruct for configuring the contour interval, size, and coloring.plot_title- An optionalTextstruct for setting the title of the plot.x_title- An optionalTextstruct for labeling the x‑axis.y_title- An optionalTextstruct for labeling the y‑axis.x_axis- An optional reference to anAxisstruct for customizing x‑axis appearance.y_axis- An optional reference to anAxisstruct for customizing y‑axis appearance.
§Example
use plotlars::{Coloring, ContourPlot, Palette, Plot, Text};
use polars::prelude::*;
let dataset = LazyCsvReader::new(PlRefPath::new("data/contour_surface.csv"))
.finish()
.unwrap()
.collect()
.unwrap();
ContourPlot::builder()
.data(&dataset)
.x("x")
.y("y")
.z("z")
.color_scale(Palette::Viridis)
.reverse_scale(true)
.coloring(Coloring::Fill)
.show_lines(false)
.plot_title(
Text::from("Contour Plot")
.font("Arial")
.size(18)
)
.build()
.plot();
Implementations§
Source§impl ContourPlot
impl ContourPlot
Sourcepub fn builder<'f1, 'f2, 'f3, 'f4, 'f5, 'f6, 'f7, 'f8, 'f9, 'f10>() -> ContourPlotBuilder<'f1, 'f2, 'f3, 'f4, 'f5, 'f6, 'f7, 'f8, 'f9, 'f10>
pub fn builder<'f1, 'f2, 'f3, 'f4, 'f5, 'f6, 'f7, 'f8, 'f9, 'f10>() -> ContourPlotBuilder<'f1, 'f2, 'f3, 'f4, 'f5, 'f6, 'f7, 'f8, 'f9, 'f10>
Examples found in repository?
examples/plotly_contourplot.rs (line 6)
3fn main() {
4 let dataset = CsvReader::new("data/contour_surface.csv").finish().unwrap();
5
6 ContourPlot::builder()
7 .data(&dataset)
8 .x("x")
9 .y("y")
10 .z("z")
11 .color_scale(Palette::Viridis)
12 .reverse_scale(true)
13 .coloring(Coloring::Fill)
14 .show_lines(false)
15 .plot_title(Text::from("Contour Plot").font("Arial").size(18))
16 .build()
17 .plot();
18}More examples
examples/plotly_faceting.rs (line 138)
74fn contourplot_example() {
75 let mut x_vals = Vec::new();
76 let mut y_vals = Vec::new();
77 let mut z_vals = Vec::new();
78 let mut patterns = Vec::new();
79
80 let grid_size = 25;
81
82 type SurfaceFn = Box<dyn Fn(f64, f64) -> f64>;
83 let functions: Vec<(&str, SurfaceFn)> = vec![
84 ("Gaussian", Box::new(|x, y| (-x * x - y * y).exp())),
85 ("Saddle", Box::new(|x, y| x * x - y * y)),
86 (
87 "Ripple",
88 Box::new(|x, y| {
89 let r = (x * x + y * y).sqrt();
90 (r * 2.0).sin() / (r + 0.1)
91 }),
92 ),
93 ("Paraboloid", Box::new(|x, y| x * x + y * y)),
94 ("Wave", Box::new(|x, y| (x * 2.0).sin() * (y * 2.0).cos())),
95 ("Diagonal", Box::new(|x, y| ((x + y) * 2.0).sin())),
96 ];
97
98 for (name, func) in &functions {
99 let mut raw_z = Vec::new();
100 let mut coords = Vec::new();
101
102 for i in 0..grid_size {
103 for j in 0..grid_size {
104 let x = (i as f64 - 12.0) / 3.0;
105 let y = (j as f64 - 12.0) / 3.0;
106 let z = func(x, y);
107 raw_z.push(z);
108 coords.push((x, y));
109 }
110 }
111
112 // Normalize to [-1, 1]
113 let z_min = raw_z.iter().cloned().fold(f64::INFINITY, f64::min);
114 let z_max = raw_z.iter().cloned().fold(f64::NEG_INFINITY, f64::max);
115 let range = z_max - z_min;
116
117 for (idx, &(x, y)) in coords.iter().enumerate() {
118 let z = if range > 0.0 {
119 2.0 * (raw_z[idx] - z_min) / range - 1.0
120 } else {
121 0.0
122 };
123 x_vals.push(x);
124 y_vals.push(y);
125 z_vals.push(z);
126 patterns.push(*name);
127 }
128 }
129
130 let contour_data = df! {
131 "x" => x_vals,
132 "y" => y_vals,
133 "z" => z_vals,
134 "pattern" => patterns,
135 }
136 .unwrap();
137
138 ContourPlot::builder()
139 .data(&contour_data)
140 .x("x")
141 .y("y")
142 .z("z")
143 .facet("pattern")
144 .facet_config(&FacetConfig::new().rows(2).cols(3).scales(FacetScales::Free))
145 .plot_title(Text::from("Mathematical Surface Patterns").size(16))
146 .x_title(Text::from("X Axis"))
147 .y_title(Text::from("Y Axis"))
148 .build()
149 .plot();
150}Source§impl ContourPlot
impl ContourPlot
pub fn try_new( data: &DataFrame, x: &str, y: &str, z: &str, facet: Option<&str>, facet_config: Option<&FacetConfig>, color_bar: Option<&ColorBar>, color_scale: Option<Palette>, reverse_scale: Option<bool>, show_scale: Option<bool>, show_lines: Option<bool>, coloring: Option<Coloring>, plot_title: Option<Text>, x_title: Option<Text>, y_title: Option<Text>, x_axis: Option<&Axis>, y_axis: Option<&Axis>, legend: Option<&Legend>, ) -> Result<ContourPlot, PlotlarsError>
pub fn try_builder<'f1, 'f2, 'f3, 'f4, 'f5, 'f6, 'f7, 'f8, 'f9, 'f10>() -> ContourPlotTryBuilder<'f1, 'f2, 'f3, 'f4, 'f5, 'f6, 'f7, 'f8, 'f9, 'f10>
Trait Implementations§
Source§impl Clone for ContourPlot
impl Clone for ContourPlot
Source§fn clone(&self) -> ContourPlot
fn clone(&self) -> ContourPlot
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 moreAuto Trait Implementations§
impl Freeze for ContourPlot
impl RefUnwindSafe for ContourPlot
impl Send for ContourPlot
impl Sync for ContourPlot
impl Unpin for ContourPlot
impl UnsafeUnpin for ContourPlot
impl UnwindSafe for ContourPlot
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