Struct plotpy::Plot

source ·
pub struct Plot { /* private fields */ }
Expand description

Driver structure that calls Python

§Example

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(())
}

doc_plot.svg

See also integration tests in the tests directory

Implementations§

source§

impl Plot

source

pub fn new() -> Self

Creates new Plot object

source

pub fn add(&mut self, graph: &dyn GraphMaker) -> &mut Self

Adds new graph entity

source

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)

source

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)

source

pub fn set_save_transparent(&mut self, transparent: bool) -> &mut Self

Sets the transparency during saving

source

pub fn save<S>(&self, figure_path: &S) -> Result<(), StrError>
where S: AsRef<OsStr> + ?Sized,

Calls python3 and saves the python script and figure

§Input
  • figure_path – may be a String, &str, or Path
§Note

Call set_show_errors to configure how the errors (if any) are printed.

source

pub fn save_and_show<S>(&self, figure_path: &S) -> Result<(), StrError>
where S: AsRef<OsStr> + ?Sized,

Calls python3, saves the python script and figure, and show the plot window

§Input
  • figure_path – may be a String, &str, or Path
§Note

Call set_show_errors to configure how the errors (if any) are printed.

source

pub fn clear_current_axes(&mut self) -> &mut Self

Clears the current axes

source

pub fn clear_current_figure(&mut self) -> &mut Self

Clears current figure

source

pub fn legend(&mut self) -> &mut Self

Adds legend to plot (see Legend for further options)

source

pub fn grid_and_labels(&mut self, xlabel: &str, ylabel: &str) -> &mut Self

Adds grid and labels

source

pub fn grid_labels_legend(&mut self, xlabel: &str, ylabel: &str) -> &mut Self

Adds grid, labels, and legend

source

pub fn set_show_errors(&mut self, option: bool) -> &mut Self

Sets flag to print python errors (if any) when calling save

source

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 grid
  • col – number of columns in the subplot_3d grid
  • index – activate current 3D subplot; indices start at one (1-based)
source

pub fn set_subplot(&mut self, row: usize, col: usize, index: usize) -> &mut Self

Configures subplots

§Input
  • row – number of rows in the subplot grid
  • col – number of columns in the subplot grid
  • index – activate current subplot; indices start at one (1-based)
source

pub fn set_gridspec( &mut self, grid_handle: &str, row: usize, col: usize, options: &str ) -> &mut Self

Configures subplots using GridSpec

§Input
source

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_gridspec
  • i_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”
source

pub fn set_rotation_ticks_x(&mut self, rotation: f64) -> &mut Self

Sets the rotation of ticks along the x-axis

source

pub fn set_rotation_ticks_y(&mut self, rotation: f64) -> &mut Self

Sets the rotation of ticks along the y-axis

source

pub fn set_align_labels(&mut self) -> &mut Self

Aligns the labels when using subplots

source

pub fn set_title(&mut self, title: &str) -> &mut Self

Adds a title to the plot or sub-plot

source

pub fn set_super_title( &mut self, title: &str, params: Option<SuperTitleParams> ) -> &mut Self

Adds a title to all sub-plots

source

pub fn set_horizontal_gap(&mut self, value: f64) -> &mut Self

Sets the horizontal gap between subplots

source

pub fn set_vertical_gap(&mut self, value: f64) -> &mut Self

Sets the vertical gap between subplots

source

pub fn set_gaps(&mut self, horizontal: f64, vertical: f64) -> &mut Self

Sets the horizontal and vertical gap between subplots

source

pub fn set_equal_axes(&mut self, equal: bool) -> &mut Self

Sets same scale for both axes

source

pub fn set_figure_size_inches(&mut self, width: f64, height: f64) -> &mut Self

Sets the figure size in inches

source

pub fn set_figure_size_points(&mut self, width: f64, height: f64) -> &mut Self

Sets the figure size in points

source

pub fn set_hide_xticks(&mut self) -> &mut Self

Sets an option to hide the ticks along the x axis

source

pub fn set_hide_yticks(&mut self) -> &mut Self

Sets an option to hide the ticks along the y axis

source

pub fn set_hide_zticks(&mut self) -> &mut Self

Sets an option to hide the ticks along the z axis

source

pub fn set_hide_axes(&mut self, hide: bool) -> &mut Self

Sets an option to hide/show all axes

source

pub fn set_range_3d( &mut self, xmin: f64, xmax: f64, ymin: f64, ymax: f64, zmin: f64, zmax: f64 ) -> &mut Self

Sets axes limits

source

pub fn set_range( &mut self, xmin: f64, xmax: f64, ymin: f64, ymax: f64 ) -> &mut Self

Sets axes limits

source

pub fn set_range_from_vec(&mut self, limits: &[f64]) -> &mut Self

Sets axes limits from vector

source

pub fn set_xmin(&mut self, xmin: f64) -> &mut Self

Sets minimum x

source

pub fn set_xmax(&mut self, xmax: f64) -> &mut Self

Sets maximum x

source

pub fn set_ymin(&mut self, ymin: f64) -> &mut Self

Sets minimum y

source

pub fn set_ymax(&mut self, ymax: f64) -> &mut Self

Sets maximum y

source

pub fn set_zmin(&mut self, zmin: f64) -> &mut Self

Sets minimum z

source

pub fn set_zmax(&mut self, zmax: f64) -> &mut Self

Sets maximum z

source

pub fn set_xrange(&mut self, xmin: f64, xmax: f64) -> &mut Self

Sets x-range (i.e. limits)

source

pub fn set_yrange(&mut self, ymin: f64, ymax: f64) -> &mut Self

Sets y-range (i.e. limits)

source

pub fn set_zrange(&mut self, zmin: f64, zmax: f64) -> &mut Self

Sets z-range (i.e. limits)

source

pub fn set_num_ticks_x(&mut self, num: usize) -> &mut Self

Sets number of ticks along x

source

pub fn set_num_ticks_y(&mut self, num: usize) -> &mut Self

Sets number of ticks along y

source

pub fn set_num_ticks_z(&mut self, num: usize) -> &mut Self

Sets number of ticks along z

source

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
source

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
source

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). Example PI / 12.0

Note: This function sets the major ticks as PI / 2.0.

source

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). Example PI / 12.0

Note: This function sets the major ticks as PI / 2.0.

source

pub fn set_log_x(&mut self, log: bool) -> &mut Self

Sets a log10 x-scale

§Note

set_log_x(true) must be called before adding curves.

source

pub fn set_log_y(&mut self, log: bool) -> &mut Self

Sets a log10 y-scale

§Note

set_log_y(true) must be called before adding curves.

source

pub fn set_label_x(&mut self, label: &str) -> &mut Self

Sets the label for the x-axis

source

pub fn set_label_y(&mut self, label: &str) -> &mut Self

Sets the label for the y-axis

source

pub fn set_label_z(&mut self, label: &str) -> &mut Self

Sets the label for the z-axis

source

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

source

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

source

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

source

pub fn set_labels(&mut self, xlabel: &str, ylabel: &str) -> &mut Self

Sets the labels for the x and y axes

source

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

source

pub fn set_inv_x(&mut self) -> &mut Self

Sets inverted x-axis

source

pub fn set_inv_y(&mut self) -> &mut Self

Sets inverted y-axis

source

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 plane
  • azimuth – is the azimuth angle in the x,y plane
source

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

source

pub fn set_frame_borders(&mut self, show_all: bool) -> &mut Self

Sets visibility of all frame borders

source

pub fn extra(&mut self, commands: &str) -> &mut Self

Writes extra python commands

Auto Trait Implementations§

§

impl Freeze for Plot

§

impl RefUnwindSafe for Plot

§

impl Send for Plot

§

impl Sync for Plot

§

impl Unpin for Plot

§

impl UnwindSafe for Plot

Blanket Implementations§

source§

impl<T> Any for T
where T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for T
where T: ?Sized,

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T, U> Into<U> for T
where U: From<T>,

source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.