Struct plotpy::Canvas

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

Implements functions to draw 2D and 3D features, including poly-lines and Bezier curves

§Examples

§Drawing functions with polyline set by an array

use plotpy::{Canvas, Plot};

fn main() -> Result<(), &'static str> {
    // canvas object and common options
    let mut canvas = Canvas::new();
    canvas.set_line_width(3.0).set_edge_color("#cd0000").set_face_color("#eeea83");

    // draw arc
    canvas.draw_arc(0.5, 0.5, 0.4, 195.0, -15.0);

    // draw arrow
    canvas.set_arrow_scale(50.0).set_arrow_style("fancy");
    canvas.draw_arrow(0.4, 0.3, 0.6, 0.5);

    // draw circle
    canvas.set_face_color("None").set_edge_color("#1f9c25").set_line_width(6.0);
    canvas.draw_circle(0.5, 0.5, 0.5);

    // draw polyline
    canvas.set_line_width(3.0).set_edge_color("blue");
    let a = 0.2;
    let c = f64::sqrt(3.0) / 2.0;
    let p = &[[0.1, 0.5], [0.1 + a, 0.5], [0.1 + a / 2.0, 0.5 + a * c]];
    let q = &[[0.9, 0.5], [0.9 - a, 0.5], [0.9 - a / 2.0, 0.5 + a * c]];
    canvas.draw_polyline(p, true);
    canvas.draw_polyline(q, false);

    // add canvas to plot
    let mut plot = Plot::new();
    plot.set_hide_axes(true)
        .set_equal_axes(true)
        .set_range(-0.05, 1.05, -0.05, 1.05)
        .add(&canvas);

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

doc_canvas.svg

§Cubic Bezier and use of begin/end functions

use plotpy::{Canvas, Plot, PolyCode, StrError};

fn main() -> Result<(), StrError> {
    // codes
    let data = [
        (3.0, 0.0, PolyCode::MoveTo),
        (1.0, 1.5, PolyCode::Curve4),
        (0.0, 4.0, PolyCode::Curve4),
        (2.5, 3.9, PolyCode::Curve4),
        (3.0, 3.8, PolyCode::LineTo),
        (3.5, 3.9, PolyCode::LineTo),
        (6.0, 4.0, PolyCode::Curve4),
        (5.0, 1.5, PolyCode::Curve4),
        (3.0, 0.0, PolyCode::Curve4),
    ];

    // polycurve
    let mut canvas = Canvas::new();
    canvas.set_face_color("#f88989").set_edge_color("red");
    canvas.polycurve_begin();
    for (x, y, code) in data {
        canvas.polycurve_add(x, y, code);
    }
    canvas.polycurve_end(true);

    // add canvas to plot
    let mut plot = Plot::new();
    plot.add(&canvas);

    // save figure
    plot.set_range(1.0, 5.0, 0.0, 4.0)
        .set_frame_borders(false)
        .set_hide_axes(true)
        .set_equal_axes(true)
        .set_show_errors(true);
    plot.save("/tmp/plotpy/doc_tests/doc_canvas_polycurve.svg")?;
    Ok(())
}

doc_canvas_polycurve.svg

See also integration tests in the tests directory

Implementations§

source§

impl Canvas

source

pub fn new() -> Self

source

pub fn draw_arc<T>(&mut self, xc: T, yc: T, r: T, ini_angle: T, fin_angle: T)
where T: Display,

Draws arc (2D only)

source

pub fn draw_arrow<T>(&mut self, xi: T, yi: T, xf: T, yf: T)
where T: Display,

Draws arrow (2D only)

source

pub fn draw_circle<T>(&mut self, xc: T, yc: T, r: T)
where T: Display,

Draws circle (2D only)

source

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

Begins drawing a polycurve (straight segments, quadratic Bezier, and cubic Bezier) (2D only)

§Warning

You must call Canvas::polycurve_add next, followed by Canvas::polycurve_end when finishing adding points. Otherwise, Python/Matplotlib will fail.

source

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

Adds point to a polycurve (straight segments, quadratic Bezier, and cubic Bezier) (2D only)

§Warning

You must call Canvas::polycurve_begin first, otherwise Python/Matplotlib will fail. Afterwards, you must call Canvas::polycurve_end when finishing adding points.

source

pub fn polycurve_end(&mut self, closed: bool) -> &mut Self

Ends drawing a polycurve (straight segments, quadratic Bezier, and cubic Bezier) (2D only)

§Warning

This function must be the last one called after Canvas::polycurve_begin and Canvas::polycurve_add. Otherwise, Python/Matplotlib will fail.

source

pub fn draw_polycurve<'a, T, U>( &mut self, points: &'a T, codes: &[PolyCode], closed: bool ) -> Result<(), StrError>
where T: AsMatrix<'a, U>, U: 'a + Display,

Draws polyline with straight segments, quadratic Bezier, or cubic Bezier (2D only)

Note: The first and last commands are ignored.

source

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

Begins adding points to a 3D polyline

§Warning

This function must be followed by Canvas::polyline_3d_add and Canvas::polyline_3d_end, otherwise Python/Matplotlib will fail

source

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

Adds point to a 3D polyline

§Warning

This function must be called after Canvas::polyline_3d_begin and must be followed by Canvas::polyline_3d_end, otherwise Python/Matplotlib will fail.

source

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

Ends adding points to a 3D polyline

§Warning

This function must be called after Canvas::polyline_3d_begin and Canvas::polyline_3d_add, otherwise Python/Matplotlib will fail.

source

pub fn draw_polyline<'a, T, U>(&mut self, points: &'a T, closed: bool)
where T: AsMatrix<'a, U>, U: 'a + Display,

Draws polyline (2D or 3D)

source

pub fn draw_grid( &mut self, xmin: &[f64], xmax: &[f64], ndiv: &[usize], with_point_ids: bool, with_cell_ids: bool ) -> Result<(), StrError>

Draws a 2D or 3D grid

§Input
  • xmin, xmax – min and max coordinates (len = 2 or 3 == ndim)
  • ndiv – number of divisions along each dimension (len = 2 or 3 == ndim)
source

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

Sets the edge color (shared among features)

source

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

Sets the face color (shared among features)

source

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

Sets the line width of edge (shared among features)

source

pub fn set_arrow_scale(&mut self, scale: f64) -> &mut Self

Sets the arrow scale

source

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

Sets the arrow style

Options:

  • -” – Curve : None
  • ->” – CurveB : head_length=0.4,head_width=0.2
  • -[” – BracketB : widthB=1.0,lengthB=0.2,angleB=None
  • -|>” – CurveFilledB : head_length=0.4,head_width=0.2
  • <-” – CurveA : head_length=0.4,head_width=0.2
  • <->” – CurveAB : head_length=0.4,head_width=0.2
  • <|-” – CurveFilledA : head_length=0.4,head_width=0.2
  • <|-|>” – CurveFilledAB : head_length=0.4,head_width=0.2
  • ]-” – BracketA : widthA=1.0,lengthA=0.2,angleA=None
  • ]-[” – BracketAB : widthA=1.0,lengthA=0.2,angleA=None,widthB=1.0,lengthB=0.2,angleB=None
  • fancy” – Fancy : head_length=0.4,head_width=0.4,tail_width=0.4
  • simple” – Simple : head_length=0.5,head_width=0.5,tail_width=0.2
  • wedge” – Wedge : tail_width=0.3,shrink_factor=0.5
  • |-|” – BarAB : widthA=1.0,angleA=None,widthB=1.0,angleB=None
  • As defined in https://matplotlib.org/stable/api/_as_gen/matplotlib.patches.FancyArrowPatch.html
source

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

Sets the text color

source

pub fn set_text_align_horizontal(&mut self, option: &str) -> &mut Self

Sets the text horizontal alignment

Options: “center”, “left”, “right”

source

pub fn set_text_align_vertical(&mut self, option: &str) -> &mut Self

Sets the text vertical alignment

Options: “center”, “top”, “bottom”, “baseline”, “center_baseline”

source

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

Sets the text font size

source

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

Sets the text rotation

source

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

Sets the alternative text color

source

pub fn set_alt_text_align_horizontal(&mut self, option: &str) -> &mut Self

Sets the alternative text horizontal alignment

Options: “center”, “left”, “right”

source

pub fn set_alt_text_align_vertical(&mut self, option: &str) -> &mut Self

Sets the alternative text vertical alignment

Options: “center”, “top”, “bottom”, “baseline”, “center_baseline”

source

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

Sets the alternative text font size

source

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

Sets the alternative text rotation

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 Canvas

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 Canvas

§

impl RefUnwindSafe for Canvas

§

impl Send for Canvas

§

impl Sync for Canvas

§

impl Unpin for Canvas

§

impl UnwindSafe for Canvas

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.