pub struct Legend { /* private fields */ }
Expand description

Generates a Legend

Example

use plotpy::{Curve, Legend, Plot, StrError};
use russell_lab::Vector;

fn main() -> Result<(), StrError> {
    // generate (x,y) points
    let x  = Vector::linspace(0.0, 5.0, 6)?;
    let y1 = x.get_mapped(|v| 0.5 * v);
    let y2 = x.get_mapped(|v| 1.0 * v);
    let y3 = x.get_mapped(|v| 1.5 * v);
    let y4 = x.get_mapped(|v| 2.0 * v);

    // 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 = 0.5 x");
    curve2.set_label("y = 1.0 x");
    curve3.set_label("y = 1.5 x");
    curve4.set_label("y = 2.0 x");
    curve1.draw(&x, &y1);
    curve2.draw(&x, &y2);
    curve3.draw(&x, &y3);
    curve4.draw(&x, &y4);

    // configure and draw legends
    let mut legend1 = Legend::new();
    legend1.set_fontsize(14.0)
        .set_handle_len(6.0)
        .set_num_col(2)
        .set_outside(true)
        .set_show_frame(false);
    legend1.draw();
    let mut legend2 = Legend::new();
    legend2.draw();

    // add curves and legends to subplots
    let mut plot = Plot::new();
    plot.set_subplot(2, 1, 1)
        .add(&curve1)
        .add(&curve2)
        .add(&curve3)
        .add(&curve4)
        .add(&legend1);
    plot.set_subplot(2, 1, 2)
        .add(&curve1)
        .add(&curve2)
        .add(&curve3)
        .add(&curve4)
        .add(&legend2);

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

doc_legend.svg

See also integration tests in the tests directory

Implementations

Creates a new Legend object

Draws legend

Sets the fontsize

Sets the length of legend’s indicator line

Sets the number of columns

Sets the location

Options:

  • “best”, “right”, “center left”
  • Note: Only used if outside == false

Sets option to put legend outside of plot area

Sets option to show frame around legend

Sets the normalized coordinates when drawing an outside legend

Example: [0.0, 1.02, 1.0, 0.102]

Trait Implementations

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.