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(())
}
See also integration tests in the tests directory
Implementations§
source§impl Legend
impl Legend
sourcepub fn set_fontsize(&mut self, fontsize: f64) -> &mut Self
pub fn set_fontsize(&mut self, fontsize: f64) -> &mut Self
Sets the fontsize
sourcepub fn set_handle_len(&mut self, length: f64) -> &mut Self
pub fn set_handle_len(&mut self, length: f64) -> &mut Self
Sets the length of legend’s indicator line
sourcepub fn set_num_col(&mut self, num_columns: usize) -> &mut Self
pub fn set_num_col(&mut self, num_columns: usize) -> &mut Self
Sets the number of columns
sourcepub fn set_location(&mut self, location: &str) -> &mut Self
pub fn set_location(&mut self, location: &str) -> &mut Self
Sets the location
Options:
- “best”, “right”, “center left”
- Note: Only used if outside == false
sourcepub fn set_outside(&mut self, flag: bool) -> &mut Self
pub fn set_outside(&mut self, flag: bool) -> &mut Self
Sets option to put legend outside of plot area
sourcepub fn set_show_frame(&mut self, flag: bool) -> &mut Self
pub fn set_show_frame(&mut self, flag: bool) -> &mut Self
Sets option to show frame around legend
sourcepub fn set_x_coords(&mut self, coords: &[f64]) -> &mut Self
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
impl GraphMaker for Legend
source§fn get_buffer<'a>(&'a self) -> &'a String
fn get_buffer<'a>(&'a self) -> &'a String
Returns the text buffer with Python3 commands
source§fn clear_buffer(&mut self)
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> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more