Struct Barplot

Source
pub struct Barplot { /* private fields */ }
Expand description

Generates a Barplot plot

See Matplotlib’s documentation

§Examples

§Basic bar plot

use plotpy::{Barplot, Plot, StrError};
use std::collections::HashMap;

fn main() -> Result<(), StrError> {
    // data
    let x = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9];
    let y = [5, 4, 3, 2, 1, 0, 1, 2, 3, 4];

    // barplot object and options
    let mut bar = Barplot::new();
    bar.draw(&x, &y);

    // save figure
    let mut plot = Plot::new();
    plot.add(&bar)
        .save("/tmp/plotpy/doc_tests/doc_barplot_1.svg")?;
    Ok(())
}

doc_barplot_1.svg

§Using string as labels

The code below implements the Bar Label Demo from Matplotlib documentation

use plotpy::{Barplot, Plot, StrError};
use std::collections::HashMap;

fn main() -> Result<(), StrError> {
    // data
    let species = ["Adelie", "Chinstrap", "Gentoo"];
    let sex_counts = HashMap::from([
        ("Male", [73.0, 34.0, 61.0]), //
        ("Female", [73.0, 34.0, 58.0]),
    ]);

   // barplot object and options
   let mut bar = Barplot::new();
   bar.set_with_text("center");

   // draw bars
   let mut bottom = [0.0, 0.0, 0.0];
   for (sex, sex_count) in &sex_counts {
       bar.set_label(sex)
           .set_bottom(&bottom)
           .draw_with_str(&species, sex_count);
       for i in 0..sex_count.len() {
           bottom[i] += sex_count[i];
       }
   }

    // add barplot to plot and save figure
    let mut plot = Plot::new();
    plot.add(&bar)
        .set_title("Number of penguins by sex")
        .legend()
        .save("/tmp/plotpy/doc_tests/doc_barplot_2.svg")?;
    Ok(())
}

doc_barplot_2.svg

§Horizontal bars

use plotpy::{Barplot, Plot, StrError};

fn main() -> Result<(), StrError> {
    // data
    let fruits = ["Apple", "Banana", "Orange"];
    let prices = [10.0, 20.0, 30.0];
    let errors = [3.0, 2.0, 1.0];

    // barplot object and options
    let mut bar = Barplot::new();
    bar.set_errors(&errors)
        .set_horizontal(true)
        .set_with_text("edge")
        .draw_with_str(&fruits, &prices);

    // save figure
    let mut plot = Plot::new();
    plot.set_inv_y()
        .add(&bar)
        .set_title("Fruits")
        .set_label_x("price")
        .save("/tmp/plotpy/doc_tests/doc_barplot_3.svg")?;
    Ok(())
}

doc_barplot_3.svg

§More examples

See also integration test in the tests directory.

Implementations§

Source§

impl Barplot

Source

pub fn new() -> Self

Creates a new Barplot object

Source

pub fn draw<'a, T, U>(&mut self, x: &'a T, y: &'a T)
where T: AsVector<'a, U>, U: 'a + Display + Num,

Draws the bar plot

Source

pub fn draw_with_str<'a, T, U>(&mut self, x: &[&str], y: &'a T)
where T: AsVector<'a, U>, U: 'a + Display + Num,

Draws the bar plot with strings

Source

pub fn set_label(&mut self, label: &str) -> &mut Self

Sets the name of this bar in the legend

Source

pub fn set_colors(&mut self, colors: &[&str]) -> &mut Self

Sets the colors for each bar

Source

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

Sets the width of the bars

Source

pub fn set_bottom(&mut self, bottom: &[f64]) -> &mut Self

Sets the vertical offset to stack bars

Source

pub fn set_with_text(&mut self, position: &str) -> &mut Self

Sets an option to show the text (labels) of each bar

§Input

position – “edge” or “center”; Use “” to remove the label

See Matplotlib documentation

Source

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

Enables drawing horizontal bars

Source

pub fn set_errors(&mut self, errors: &[f64]) -> &mut Self

Enables error indicators

Source

pub fn set_extra(&mut self, extra: &str) -> &mut Self

Sets extra matplotlib commands (comma separated)

Important: The extra commands must be comma separated. For example:

param1=123,param2='hello'

See Matplotlib’s documentation for extra parameters

Trait Implementations§

Source§

impl GraphMaker for Barplot

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§

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>,

Source§

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>,

Source§

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.