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

Draws arc (2D only)

Draws arrow (2D only)

Draws circle (2D only)

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.

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.

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.

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

Note: The first and last commands are ignored.

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

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.

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.

Draws polyline (2D or 3D)

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)

Sets the edge color (shared among features)

Sets the face color (shared among features)

Sets the line width of edge (shared among features)

Sets the arrow scale

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

Sets the text color

Sets the text horizontal alignment

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

Sets the text vertical alignment

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

Sets the text font size

Sets the text rotation

Sets the alternative text color

Sets the alternative text horizontal alignment

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

Sets the alternative text vertical alignment

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

Sets the alternative text font size

Sets the alternative text rotation

Sets the flag to stop clipping features within margins

Trait Implementations

Returns the text buffer with Python3 commands

Clear the text buffer with Python commands

Auto Trait Implementations

Blanket Implementations

Gets the TypeId of self. Read more

Immutably borrows from an owned value. Read more

Mutably borrows from an owned value. Read more

Returns the argument unchanged.

Calls U::from(self).

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

The type returned in the event of a conversion error.

Performs the conversion.

The type returned in the event of a conversion error.

Performs the conversion.