Struct plotpy::Legend

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

Generates a Legend

§Example

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

fn main() -> Result<(), StrError> {
    // generate (x,y) points
    let x  = linspace(0.0, 5.0, 6);
    let y1: Vec<_> = x.iter().map(|v| 0.5 * *v).collect();
    let y2: Vec<_> = x.iter().map(|v| 1.0 * *v).collect();
    let y3: Vec<_> = x.iter().map(|v| 1.5 * *v).collect();
    let y4: Vec<_> = x.iter().map(|v| 2.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 = 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§

source§

impl Legend

source

pub fn new() -> Self

Creates a new Legend object

source

pub fn draw(&mut self)

Draws legend

source

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

Sets the fontsize

source

pub fn set_handle_len(&mut self, length: f64) -> &mut Self

Sets the length of legend’s indicator line

source

pub fn set_num_col(&mut self, num_columns: usize) -> &mut Self

Sets the number of columns

source

pub fn set_location(&mut self, location: &str) -> &mut Self

Sets the location

Options:

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

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

Sets option to put legend outside of plot area

source

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

Sets option to show frame around legend

source

pub fn set_x_coords(&mut self, coords: &[f64]) -> &mut Self

Sets the normalized coordinates when drawing an outside legend

Example: [0.0, 1.02, 1.0, 0.102]

Trait Implementations§

source§

impl GraphMaker for Legend

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 Legend

§

impl RefUnwindSafe for Legend

§

impl Send for Legend

§

impl Sync for Legend

§

impl Unpin for Legend

§

impl UnwindSafe for Legend

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.