Struct plotpy::Curve

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

Generates a curve (aka line-plot) given two arrays (x,y)

§Notes

  • This struct corresponds to the plot function of Matplotlib.
  • You may plot a Scatter plot by setting line_style = “None”

§Examples

§Using methods to set the points

use plotpy::{Curve, Plot, StrError};
use std::f64::consts::PI;

fn main() -> Result<(), StrError> {
    // configure curve
    let mut curve = Curve::new();
    curve.set_line_width(2.0);

    // add points
    const N: usize = 30;
    curve.points_begin();
    for i in 0..N {
        let x = (i as f64) * 2.0 * PI / ((N - 1) as f64);
        let y = f64::sin(x);
        curve.points_add(x, y);
    }
    curve.points_end();

    // add curve to plot
    let mut plot = Plot::new();
    plot.add(&curve).grid_and_labels("x", "y");

    // configure multiple-of-pi formatter
    let minor_every = PI / 12.0;
    plot.set_ticks_x_multiple_of_pi(minor_every);

    // save figure
    plot.save("/tmp/plotpy/doc_tests/doc_curve_methods.svg")?;
    Ok(())
}

doc_curve_methods.svg

§Using Vector with point data

use plotpy::{linspace, Curve, Plot, StrError};

fn main() -> Result<(), StrError> {
    // generate (x,y) points
    let x = linspace(-1.0, 1.0, 21);
    let y: Vec<_> = x.iter().map(|v| 1.0 / (1.0 + f64::exp(-5.0 * *v))).collect();

    // configure curve
    let mut curve = Curve::new();
    curve
        .set_label("logistic function")
        .set_line_alpha(0.8)
        .set_line_color("#5f9cd8")
        .set_line_style("-")
        .set_line_width(5.0)
        .set_marker_color("#eeea83")
        .set_marker_every(5)
        .set_marker_line_color("#da98d1")
        .set_marker_line_width(2.5)
        .set_marker_size(20.0)
        .set_marker_style("*");

    // draw curve
    curve.draw(&x, &y);

    // add curve to plot
    let mut plot = Plot::new();
    plot.add(&curve).set_num_ticks_y(11).grid_labels_legend("x", "y");

    // save figure
    plot.save("/tmp/plotpy/doc_tests/doc_curve.svg")?;
    Ok(())
}

doc_curve_vector.svg

See also integration tests in the tests directory

Output from some integration tests:

integ_curve.svg

integ_curve_3d.svg

Implementations§

source§

impl Curve

source

pub fn new() -> Self

Creates new Curve object

source

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

Begins adding points to the curve (2D only)

§Warning

This function must be followed by Curve::points_add and Curve::points_end, otherwise Python/Matplotlib will fail.

source

pub fn points_add<T>(&mut self, x: T, y: T) -> &mut Self
where T: Display,

Adds point to the curve (2D only)

§Warning

This function must be called after Curve::points_begin and must be followed by Curve::points_end, otherwise Python/Matplotlib will fail.

source

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

Ends adding points to the curve (2D only)

§Warning

This function must be called after Curve::points_begin and Curve::points_add, otherwise Python/Matplotlib will fail.

source

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

Begins adding 3D points to the curve

§Warning

This function must be followed by Curve::points_3d_add and Curve::points_3d_end, otherwise Python/Matplotlib will fail

source

pub fn points_3d_add<T>(&mut self, x: T, y: T, z: T) -> &mut Self
where T: Display,

Adds 3D point to the curve

§Warning

This function must be called after Curve::points_3d_begin and must be followed by Curve::points_3d_end, otherwise Python/Matplotlib will fail.

source

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

Ends adding 3D points to the curve

§Warning

This function must be called after Curve::points_3d_begin and Curve::points_3d_add, otherwise Python/Matplotlib will fail.

source

pub fn draw<'a, T, U>(&mut self, x: &'a T, y: &'a T)
where T: AsVector<'a, U>, U: 'a + Display,

Draws curve

§Input
  • x - abscissa values
  • y - ordinate values
§Notes
  • The type U of the input array must be a number.
source

pub fn draw_3d<'a, T, U>(&mut self, x: &'a T, y: &'a T, z: &'a T)
where T: AsVector<'a, U>, U: 'a + Display,

Draws curve in 3D plot

§Input
  • x - x values
  • y - y values
  • z - z values
§Notes
  • The type U of the input array must be a number.
source

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

Sets the name of this curve in the legend

source

pub fn set_line_alpha(&mut self, alpha: f64) -> &mut Self

Sets the opacity of lines (0, 1]. A<1e-14 => A=1.0

source

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

Sets the color of lines

source

pub fn draw_ray(&mut self, xa: f64, ya: f64, endpoint: RayEndpoint)

Draws a ray (an infinite line)

  • For horizontal rays, only ya is used
  • For vertical rays, only xa is used
source

pub fn set_line_style(&mut self, style: &str) -> &mut Self

Sets the style of lines

Options:

source

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

Sets the width of lines

source

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

Sets the color of markers

source

pub fn set_marker_every(&mut self, every: usize) -> &mut Self

Sets the increment of data points to use when drawing markers

source

pub fn set_marker_void(&mut self, flag: bool) -> &mut Self

Sets the option to draw a void marker (draw edge only)

source

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

Sets the edge color of markers

source

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

Sets the edge width of markers

source

pub fn set_marker_size(&mut self, size: f64) -> &mut Self

Sets the size of markers

source

pub fn set_marker_style(&mut self, style: &str) -> &mut Self

Sets the style of markers

Examples:

source

pub fn set_stop_clip(&mut self, flag: bool) -> &mut Self

Sets the flag to stop clipping features within margins

Trait Implementations§

source§

impl GraphMaker for Curve

source§

fn get_buffer<'a>(&'a self) -> &'a String

Returns the text buffer with Python3 commands
source§

fn clear_buffer(&mut self)

Clear the text buffer with Python commands

Auto Trait Implementations§

§

impl Freeze for Curve

§

impl RefUnwindSafe for Curve

§

impl Send for Curve

§

impl Sync for Curve

§

impl Unpin for Curve

§

impl UnwindSafe for Curve

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.