Struct plotpy::Plot

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

doc_plot.svg

§Drawing an image from data and setting the ticks

See Matplotlib example

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

doc_plot_2.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 Python and saves the python script and figure

§Input
  • figure_path – may be a String, &str, or Path
§Notes
  1. You may want to call Plot::set_show_errors() to enable the display of Python errors (if any)
source

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

Calls Python, saves the python script and figure, and shows the plot window

§Input
  • figure_path – may be a String, &str, or Path
§Notes
  1. You may want to call Plot::set_show_errors() to enable the display of Python errors (if any)
  2. This function will also save a figure as Plot::save() does
source

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

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
  1. You may want to call Plot::set_show_errors() to enable the display of Python errors (if any)
  2. This function will also save a figure as Plot::save() does
  3. This method only works in a Jupyter Notebook
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

Enables the display of python errors (if any)

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

§Notes
  1. 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
source

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

Adds a title to all sub-plots

§Notes
  1. 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
  2. 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.
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_labels<'a, S, T, U>( &mut self, ticks: &'a T, labels: &[S], ) -> &mut Self
where S: Display, T: AsVector<'a, U>, U: 'a + Display,

Sets the ticks and labels along x

source

pub fn set_ticks_y_labels<'a, S, T, U>( &mut self, ticks: &'a T, labels: &[S], ) -> &mut Self
where S: Display, T: AsVector<'a, U>, U: 'a + Display,

Sets the ticks and labels along y

source

pub fn set_ticks_x_fontsize(&mut self, fontsize: f64) -> &mut Self

Sets the fontsize of the ticks for the x-axis

source

pub fn set_ticks_y_fontsize(&mut self, fontsize: f64) -> &mut Self

Sets the fontsize of the ticks for the y-axis

source

pub fn set_ticks_z_fontsize(&mut self, fontsize: f64) -> &mut Self

Sets the fontsize of the ticks for the z-axis

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_fontsize(&mut self, fontsize: f64) -> &mut Self

Sets the fontsize of the label for the x-axis

source

pub fn set_label_y_fontsize(&mut self, fontsize: f64) -> &mut Self

Sets the fontsize of the label for the y-axis

source

pub fn set_label_z_fontsize(&mut self, fontsize: f64) -> &mut Self

Sets the fontsize of the label for the z-axis

source

pub fn set_label_x_color(&mut self, color: &str) -> &mut Self

Sets the color of the label for the x-axis

source

pub fn set_label_y_color(&mut self, color: &str) -> &mut Self

Sets the color of the label for the y-axis

source

pub fn set_label_z_color(&mut self, color: &str) -> &mut Self

Sets the color of the label for the z-axis

source

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

source

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

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 set_horiz_line( &mut self, y: f64, color: &str, line_style: &str, line_width: f64, ) -> &mut Self

Draws an infinite horizontal line at y

source

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

source

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)

source

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

Writes extra python commands

source

pub fn set_python_exe(&mut self, python_exe: &str) -> &mut Self

Sets the Python3 executable command

The default is python3

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>,

source§

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>,

source§

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.