pub struct Plot { /* private fields */ }
Expand description
Driver structure that calls Python
§Examples
§Drawing curves
use plotpy::{linspace, Curve, Plot, StrError};
fn main() -> Result<(), StrError> {
// generate (x,y) points
let n = 11;
let x = linspace(-1.0, 1.0, n);
let y1 = x.clone();
let y2: Vec<_> = x.iter().map(|v| f64::abs(*v)).collect();
let y3: Vec<_> = x.iter().map(|v| f64::exp(1.0 + *v) - 1.0).collect();
let y4: Vec<_> = x.iter().map(|v| f64::sqrt(1.0 + *v)).collect();
// configure and draw curves
let mut curve1 = Curve::new();
let mut curve2 = Curve::new();
let mut curve3 = Curve::new();
let mut curve4 = Curve::new();
curve1.set_label("y = x");
curve2.set_label("y = |x|").set_line_color("#cd0000");
curve3.set_label("y = exp(1+x)-1").set_line_color("#e79955");
curve4.set_label("y = sqrt(1+x)").set_line_color("#b566ab");
curve1.draw(&x, &y1);
curve2.draw(&x, &y2);
curve3.draw(&x, &y3);
curve4.draw(&x, &y4);
// configure plot
let mut plot = Plot::new();
plot.set_super_title("FOUR CURVES", None).set_gaps(0.35, 0.5);
// add curve to subplot
plot.set_subplot(2, 2, 1)
.set_title("first")
.add(&curve1)
.grid_labels_legend("x", "y")
.set_equal_axes(true);
// add curve to subplot
plot.set_subplot(2, 2, 2)
.set_title("second")
.add(&curve2)
.grid_labels_legend("x", "y");
// add curve to subplot
plot.set_subplot(2, 2, 3)
.set_title("third")
.add(&curve3)
.set_range(-1.0, 1.0, 0.0, 6.0)
.grid_labels_legend("x", "y");
// add curve to subplot
plot.set_subplot(2, 2, 4)
.set_title("fourth")
.add(&curve4)
.grid_labels_legend("x", "y");
// save figure
plot.save("/tmp/plotpy/doc_tests/doc_plot.svg")?;
Ok(())
}
§Drawing an image from data and setting the ticks
use plotpy::{Image, Plot, Text, StrError};
fn main() -> Result<(), StrError> {
// data
let vegetables = [
"cucumber",
"tomato",
"lettuce",
"asparagus",
"potato",
"wheat",
"barley",
];
let farmers = [
"Farmer Joe",
"Upland Bros.",
"Smith Gardening",
"Agrifun",
"Organiculture",
"BioGoods Ltd.",
"Cornylee Corp.",
];
let harvest = [
[0.8, 2.4, 2.5, 3.9, 0.0, 4.0, 0.0],
[2.4, 0.0, 4.0, 1.0, 2.7, 0.0, 0.0],
[1.1, 2.4, 0.8, 4.3, 1.9, 4.4, 0.0],
[0.6, 0.0, 0.3, 0.0, 3.1, 0.0, 0.0],
[0.7, 1.7, 0.6, 2.6, 2.2, 6.2, 0.0],
[1.3, 1.2, 0.0, 0.0, 0.0, 3.2, 5.1],
[0.1, 2.0, 0.0, 1.4, 0.0, 1.9, 6.3],
];
// draw image
let mut img = Image::new();
img.draw(&harvest);
// set tick labels
let mut plot = Plot::new();
let ticks: Vec<_> = (0..vegetables.len()).into_iter().collect();
plot.add(&img)
.set_rotation_ticks_x(45.0)
.set_ticks_x_labels(&ticks, &farmers)
.set_ticks_y_labels(&ticks, &vegetables);
// add text
let mut text = Text::new();
text.set_color("white").set_align_horizontal("center");
for i in 0..vegetables.len() {
for j in 0..farmers.len() {
text.draw(j as f64, i as f64, harvest[i][j].to_string().as_str());
}
}
plot.add(&text);
// save figure
plot.save("/tmp/plotpy/doc_tests/doc_plot_2.svg")?;
Ok(())
}
See also integration tests in the tests directory
Implementations§
source§impl Plot
impl Plot
sourcepub fn add(&mut self, graph: &dyn GraphMaker) -> &mut Self
pub fn add(&mut self, graph: &dyn GraphMaker) -> &mut Self
Adds new graph entity
sourcepub fn set_save_tight(&mut self, tight: bool) -> &mut Self
pub fn set_save_tight(&mut self, tight: bool) -> &mut Self
Tells matplotlib to try to figure out the tight bounding box of the figure (default = true)
sourcepub fn set_save_pad_inches(&mut self, pad_inches: f64) -> &mut Self
pub fn set_save_pad_inches(&mut self, pad_inches: f64) -> &mut Self
Sets the padding around the figure when the ‘tight’ layout is enabled during saving
This option may circumvent rare problems when matplotlib fails to compute the best bounding box (e.g., when the labels of 3D plots are ignored)
sourcepub fn set_save_transparent(&mut self, transparent: bool) -> &mut Self
pub fn set_save_transparent(&mut self, transparent: bool) -> &mut Self
Sets the transparency during saving
sourcepub fn save<S>(&self, figure_path: &S) -> Result<(), StrError>
pub fn save<S>(&self, figure_path: &S) -> Result<(), StrError>
Calls Python and saves the python script and figure
§Input
figure_path
– may be a String, &str, or Path
§Notes
- You may want to call Plot::set_show_errors() to enable the display of Python errors (if any)
sourcepub fn show<S>(&self, figure_path: &S) -> Result<(), StrError>
pub fn show<S>(&self, figure_path: &S) -> Result<(), StrError>
Calls Python, saves the python script and figure, and shows the plot window
§Input
figure_path
– may be a String, &str, or Path
§Notes
- You may want to call Plot::set_show_errors() to enable the display of Python errors (if any)
- This function will also save a figure as Plot::save() does
sourcepub fn show_in_jupyter<S>(&self, figure_path: &S) -> Result<(), StrError>
pub fn show_in_jupyter<S>(&self, figure_path: &S) -> Result<(), StrError>
Calls Python, saves the python script and figure, and shows the result in a Jupyter notebook
Important: This function requires evcxr_jupyter.
§Input
figure_path
– may be a String, &str or Path
§Notes
- You may want to call Plot::set_show_errors() to enable the display of Python errors (if any)
- This function will also save a figure as Plot::save() does
- This method only works in a Jupyter Notebook
sourcepub fn clear_current_axes(&mut self) -> &mut Self
pub fn clear_current_axes(&mut self) -> &mut Self
Clears the current axes
sourcepub fn clear_current_figure(&mut self) -> &mut Self
pub fn clear_current_figure(&mut self) -> &mut Self
Clears current figure
sourcepub fn grid_and_labels(&mut self, xlabel: &str, ylabel: &str) -> &mut Self
pub fn grid_and_labels(&mut self, xlabel: &str, ylabel: &str) -> &mut Self
Adds grid and labels
sourcepub fn grid_labels_legend(&mut self, xlabel: &str, ylabel: &str) -> &mut Self
pub fn grid_labels_legend(&mut self, xlabel: &str, ylabel: &str) -> &mut Self
Adds grid, labels, and legend
sourcepub fn set_show_errors(&mut self, option: bool) -> &mut Self
pub fn set_show_errors(&mut self, option: bool) -> &mut Self
Enables the display of python errors (if any)
sourcepub fn set_subplot_3d(
&mut self,
row: usize,
col: usize,
index: usize,
) -> &mut Self
pub fn set_subplot_3d( &mut self, row: usize, col: usize, index: usize, ) -> &mut Self
Configures 3D subplots
§Input
row
– number of rows in the subplot_3d gridcol
– number of columns in the subplot_3d gridindex
– activate current 3D subplot; indices start at one (1-based)
sourcepub fn set_subplot(&mut self, row: usize, col: usize, index: usize) -> &mut Self
pub fn set_subplot(&mut self, row: usize, col: usize, index: usize) -> &mut Self
Configures subplots
§Input
row
– number of rows in the subplot gridcol
– number of columns in the subplot gridindex
– activate current subplot; indices start at one (1-based)
sourcepub fn set_gridspec(
&mut self,
grid_handle: &str,
row: usize,
col: usize,
options: &str,
) -> &mut Self
pub fn set_gridspec( &mut self, grid_handle: &str, row: usize, col: usize, options: &str, ) -> &mut Self
Configures subplots using GridSpec
§Input
grid_handle
– an identifier for GridSpec to be used later with Plot::set_subplot_gridrow
– number of rows in the gridcol
– number of columns in the gridoptions
– (may be empty) Comma separated options. Example"wspace=0,hspace=0.35"
. See https://matplotlib.org/stable/api/_as_gen/matplotlib.gridspec.GridSpec.html
sourcepub fn set_subplot_grid(
&mut self,
grid_handle: &str,
i_range: &str,
j_range: &str,
) -> &mut Self
pub fn set_subplot_grid( &mut self, grid_handle: &str, i_range: &str, j_range: &str, ) -> &mut Self
Sets a subplot configured via GridSpec
See function Plot::set_gridspec
§Input
grid_handle
– an identifier for GridSpec defined by Plot::set_gridspeci_range
– the zero-based row index or range such as “0” or “0:2”j_range
– the zero-based column index or range such as “0” or “0:2”
sourcepub fn set_rotation_ticks_x(&mut self, rotation: f64) -> &mut Self
pub fn set_rotation_ticks_x(&mut self, rotation: f64) -> &mut Self
Sets the rotation of ticks along the x-axis
sourcepub fn set_rotation_ticks_y(&mut self, rotation: f64) -> &mut Self
pub fn set_rotation_ticks_y(&mut self, rotation: f64) -> &mut Self
Sets the rotation of ticks along the y-axis
sourcepub fn set_align_labels(&mut self) -> &mut Self
pub fn set_align_labels(&mut self) -> &mut Self
Aligns the labels when using subplots
sourcepub fn set_title(&mut self, title: &str) -> &mut Self
pub fn set_title(&mut self, title: &str) -> &mut Self
Adds a title to the plot or sub-plot
§Notes
- Single quotation marks are replaced by the UTF-8 Right Single Quotation Mark
because the Python script already uses the single quotation mark. Note that cannot use the raw notation
"""
because otherwise some TeX formula wouldn’t work; notably the ones starting with\v
such as\varepsilon
sourcepub fn set_super_title(
&mut self,
title: &str,
params: Option<&SuperTitleParams>,
) -> &mut Self
pub fn set_super_title( &mut self, title: &str, params: Option<&SuperTitleParams>, ) -> &mut Self
Adds a title to all sub-plots
§Notes
- Single quotation marks are replaced by the UTF-8 Right Single Quotation Mark
because the Python script already uses the single quotation mark. Note that cannot use the raw notation
"""
because otherwise some TeX formula wouldn’t work; notably the ones starting with\v
such as\varepsilon
- The TeX handling in the super-title string is more limited than in the title string
because we cannot use Python’s raw string notation (
r''
) here. The reason is that we want the super-title to be wrapped if it is too long and only non-raw strings can do that.
sourcepub fn set_horizontal_gap(&mut self, value: f64) -> &mut Self
pub fn set_horizontal_gap(&mut self, value: f64) -> &mut Self
Sets the horizontal gap between subplots
sourcepub fn set_vertical_gap(&mut self, value: f64) -> &mut Self
pub fn set_vertical_gap(&mut self, value: f64) -> &mut Self
Sets the vertical gap between subplots
sourcepub fn set_gaps(&mut self, horizontal: f64, vertical: f64) -> &mut Self
pub fn set_gaps(&mut self, horizontal: f64, vertical: f64) -> &mut Self
Sets the horizontal and vertical gap between subplots
sourcepub fn set_equal_axes(&mut self, equal: bool) -> &mut Self
pub fn set_equal_axes(&mut self, equal: bool) -> &mut Self
Sets same scale for both axes
sourcepub fn set_figure_size_inches(&mut self, width: f64, height: f64) -> &mut Self
pub fn set_figure_size_inches(&mut self, width: f64, height: f64) -> &mut Self
Sets the figure size in inches
sourcepub fn set_figure_size_points(&mut self, width: f64, height: f64) -> &mut Self
pub fn set_figure_size_points(&mut self, width: f64, height: f64) -> &mut Self
Sets the figure size in points
sourcepub fn set_hide_xticks(&mut self) -> &mut Self
pub fn set_hide_xticks(&mut self) -> &mut Self
Sets an option to hide the ticks along the x axis
sourcepub fn set_hide_yticks(&mut self) -> &mut Self
pub fn set_hide_yticks(&mut self) -> &mut Self
Sets an option to hide the ticks along the y axis
sourcepub fn set_hide_zticks(&mut self) -> &mut Self
pub fn set_hide_zticks(&mut self) -> &mut Self
Sets an option to hide the ticks along the z axis
sourcepub fn set_hide_axes(&mut self, hide: bool) -> &mut Self
pub fn set_hide_axes(&mut self, hide: bool) -> &mut Self
Sets an option to hide/show all axes
sourcepub fn set_range_3d(
&mut self,
xmin: f64,
xmax: f64,
ymin: f64,
ymax: f64,
zmin: f64,
zmax: f64,
) -> &mut Self
pub fn set_range_3d( &mut self, xmin: f64, xmax: f64, ymin: f64, ymax: f64, zmin: f64, zmax: f64, ) -> &mut Self
Sets axes limits
sourcepub fn set_range(
&mut self,
xmin: f64,
xmax: f64,
ymin: f64,
ymax: f64,
) -> &mut Self
pub fn set_range( &mut self, xmin: f64, xmax: f64, ymin: f64, ymax: f64, ) -> &mut Self
Sets axes limits
sourcepub fn set_range_from_vec(&mut self, limits: &[f64]) -> &mut Self
pub fn set_range_from_vec(&mut self, limits: &[f64]) -> &mut Self
Sets axes limits from vector
sourcepub fn set_xrange(&mut self, xmin: f64, xmax: f64) -> &mut Self
pub fn set_xrange(&mut self, xmin: f64, xmax: f64) -> &mut Self
Sets x-range (i.e. limits)
sourcepub fn set_yrange(&mut self, ymin: f64, ymax: f64) -> &mut Self
pub fn set_yrange(&mut self, ymin: f64, ymax: f64) -> &mut Self
Sets y-range (i.e. limits)
sourcepub fn set_zrange(&mut self, zmin: f64, zmax: f64) -> &mut Self
pub fn set_zrange(&mut self, zmin: f64, zmax: f64) -> &mut Self
Sets z-range (i.e. limits)
sourcepub fn set_num_ticks_x(&mut self, num: usize) -> &mut Self
pub fn set_num_ticks_x(&mut self, num: usize) -> &mut Self
Sets number of ticks along x
sourcepub fn set_num_ticks_y(&mut self, num: usize) -> &mut Self
pub fn set_num_ticks_y(&mut self, num: usize) -> &mut Self
Sets number of ticks along y
sourcepub fn set_num_ticks_z(&mut self, num: usize) -> &mut Self
pub fn set_num_ticks_z(&mut self, num: usize) -> &mut Self
Sets number of ticks along z
sourcepub fn set_ticks_x(
&mut self,
major_every: f64,
minor_every: f64,
major_number_format: &str,
) -> &mut Self
pub fn set_ticks_x( &mut self, major_every: f64, minor_every: f64, major_number_format: &str, ) -> &mut Self
Sets the number and format of x-ticks
§Input
major_every
– step for major ticks (ignored if ≤ 0.0)minor_every
– step for major ticks (ignored if ≤ 0.0)major_number_format
– C-style number format for major ticks; e.g. “%.2f” (ignored if empty “”) See matplotlib FormatStrFormatter
sourcepub fn set_ticks_y(
&mut self,
major_every: f64,
minor_every: f64,
major_number_format: &str,
) -> &mut Self
pub fn set_ticks_y( &mut self, major_every: f64, minor_every: f64, major_number_format: &str, ) -> &mut Self
Sets the number and format of y-ticks
§Input
major_every
– step for major ticks (ignored if ≤ 0.0)minor_every
– step for major ticks (ignored if ≤ 0.0)major_number_format
– C-style number format for major ticks; e.g. “%.2f” (ignored if empty “”) See matplotlib FormatStrFormatter
sourcepub fn set_ticks_x_labels<'a, S, T, U>(
&mut self,
ticks: &'a T,
labels: &[S],
) -> &mut Self
pub fn set_ticks_x_labels<'a, S, T, U>( &mut self, ticks: &'a T, labels: &[S], ) -> &mut Self
Sets the ticks and labels along x
sourcepub fn set_ticks_y_labels<'a, S, T, U>(
&mut self,
ticks: &'a T,
labels: &[S],
) -> &mut Self
pub fn set_ticks_y_labels<'a, S, T, U>( &mut self, ticks: &'a T, labels: &[S], ) -> &mut Self
Sets the ticks and labels along y
sourcepub fn set_ticks_x_fontsize(&mut self, fontsize: f64) -> &mut Self
pub fn set_ticks_x_fontsize(&mut self, fontsize: f64) -> &mut Self
Sets the fontsize of the ticks for the x-axis
sourcepub fn set_ticks_y_fontsize(&mut self, fontsize: f64) -> &mut Self
pub fn set_ticks_y_fontsize(&mut self, fontsize: f64) -> &mut Self
Sets the fontsize of the ticks for the y-axis
sourcepub fn set_ticks_z_fontsize(&mut self, fontsize: f64) -> &mut Self
pub fn set_ticks_z_fontsize(&mut self, fontsize: f64) -> &mut Self
Sets the fontsize of the ticks for the z-axis
sourcepub fn set_ticks_x_multiple_of_pi(&mut self, minor_every: f64) -> &mut Self
pub fn set_ticks_x_multiple_of_pi(&mut self, minor_every: f64) -> &mut Self
Sets the x-ticks to multiples of pi
§Input
minor_every
– step for major ticks (ignored if ≤ 0.0). ExamplePI / 12.0
Note: This function sets the major ticks as PI / 2.0
.
sourcepub fn set_ticks_y_multiple_of_pi(&mut self, minor_every: f64) -> &mut Self
pub fn set_ticks_y_multiple_of_pi(&mut self, minor_every: f64) -> &mut Self
Sets the y-ticks to multiples of pi
§Input
minor_every
– step for major ticks (ignored if ≤ 0.0). ExamplePI / 12.0
Note: This function sets the major ticks as PI / 2.0
.
sourcepub fn set_label_x(&mut self, label: &str) -> &mut Self
pub fn set_label_x(&mut self, label: &str) -> &mut Self
Sets the label for the x-axis
sourcepub fn set_label_y(&mut self, label: &str) -> &mut Self
pub fn set_label_y(&mut self, label: &str) -> &mut Self
Sets the label for the y-axis
sourcepub fn set_label_z(&mut self, label: &str) -> &mut Self
pub fn set_label_z(&mut self, label: &str) -> &mut Self
Sets the label for the z-axis
sourcepub fn set_label_x_fontsize(&mut self, fontsize: f64) -> &mut Self
pub fn set_label_x_fontsize(&mut self, fontsize: f64) -> &mut Self
Sets the fontsize of the label for the x-axis
sourcepub fn set_label_y_fontsize(&mut self, fontsize: f64) -> &mut Self
pub fn set_label_y_fontsize(&mut self, fontsize: f64) -> &mut Self
Sets the fontsize of the label for the y-axis
sourcepub fn set_label_z_fontsize(&mut self, fontsize: f64) -> &mut Self
pub fn set_label_z_fontsize(&mut self, fontsize: f64) -> &mut Self
Sets the fontsize of the label for the z-axis
sourcepub fn set_label_x_color(&mut self, color: &str) -> &mut Self
pub fn set_label_x_color(&mut self, color: &str) -> &mut Self
Sets the color of the label for the x-axis
sourcepub fn set_label_y_color(&mut self, color: &str) -> &mut Self
pub fn set_label_y_color(&mut self, color: &str) -> &mut Self
Sets the color of the label for the y-axis
sourcepub fn set_label_z_color(&mut self, color: &str) -> &mut Self
pub fn set_label_z_color(&mut self, color: &str) -> &mut Self
Sets the color of the label for the z-axis
sourcepub fn set_label_y_twinx(&mut self, label: &str) -> &mut Self
pub fn set_label_y_twinx(&mut self, label: &str) -> &mut Self
Sets the label for the y-axis on a twin-x graph
Warning: The curve with a twin-x graph must be added first
sourcepub fn set_label_y_twinx_color(&mut self, color: &str) -> &mut Self
pub fn set_label_y_twinx_color(&mut self, color: &str) -> &mut Self
Sets the color of the label for the y-axis on a twin-x graph
Warning: The curve with a twin-x graph must be added first
sourcepub fn set_label_x_and_pad(&mut self, label: &str, pad: f64) -> &mut Self
pub fn set_label_x_and_pad(&mut self, label: &str, pad: f64) -> &mut Self
Sets the label for the x-axis and the padding
sourcepub fn set_label_y_and_pad(&mut self, label: &str, pad: f64) -> &mut Self
pub fn set_label_y_and_pad(&mut self, label: &str, pad: f64) -> &mut Self
Sets the label for the y-axis and the padding
sourcepub fn set_label_z_and_pad(&mut self, label: &str, pad: f64) -> &mut Self
pub fn set_label_z_and_pad(&mut self, label: &str, pad: f64) -> &mut Self
Sets the label for the z-axis and the padding
sourcepub fn set_labels(&mut self, xlabel: &str, ylabel: &str) -> &mut Self
pub fn set_labels(&mut self, xlabel: &str, ylabel: &str) -> &mut Self
Sets the labels for the x and y axes
sourcepub fn set_labels_3d(
&mut self,
xlabel: &str,
ylabel: &str,
zlabel: &str,
) -> &mut Self
pub fn set_labels_3d( &mut self, xlabel: &str, ylabel: &str, zlabel: &str, ) -> &mut Self
Sets the labels for the x, y, and z axes
sourcepub fn set_camera(&mut self, elev: f64, azimuth: f64) -> &mut Self
pub fn set_camera(&mut self, elev: f64, azimuth: f64) -> &mut Self
Sets camera in 3d graph. Sets the elevation and azimuth of the axes.
§Input
elev
– is the elevation angle in the z planeazimuth
– is the azimuth angle in the x,y plane
sourcepub fn set_frame_border(
&mut self,
left: bool,
right: bool,
bottom: bool,
top: bool,
) -> &mut Self
pub fn set_frame_border( &mut self, left: bool, right: bool, bottom: bool, top: bool, ) -> &mut Self
Sets option to hide (or show) frame borders
sourcepub fn set_frame_borders(&mut self, show_all: bool) -> &mut Self
pub fn set_frame_borders(&mut self, show_all: bool) -> &mut Self
Sets visibility of all frame borders
sourcepub fn set_horiz_line(
&mut self,
y: f64,
color: &str,
line_style: &str,
line_width: f64,
) -> &mut Self
pub fn set_horiz_line( &mut self, y: f64, color: &str, line_style: &str, line_width: f64, ) -> &mut Self
Draws an infinite horizontal line at y
sourcepub fn set_vert_line(
&mut self,
x: f64,
color: &str,
line_style: &str,
line_width: f64,
) -> &mut Self
pub fn set_vert_line( &mut self, x: f64, color: &str, line_style: &str, line_width: f64, ) -> &mut Self
Draws an infinite vertical line at x
sourcepub fn set_cross(
&mut self,
x: f64,
y: f64,
color: &str,
line_style: &str,
line_width: f64,
) -> &mut Self
pub fn set_cross( &mut self, x: f64, y: f64, color: &str, line_style: &str, line_width: f64, ) -> &mut Self
Draws infinite horizontal and vertical lines at (x, y)
sourcepub fn set_python_exe(&mut self, python_exe: &str) -> &mut Self
pub fn set_python_exe(&mut self, python_exe: &str) -> &mut Self
Sets the Python3 executable command
The default is python3