Struct plotpy::Histogram

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

Generates a Histogram plot

§Example

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

fn main() -> Result<(), StrError> {
    // set values
    let values = vec![
        vec![1, 1, 1, 2, 2, 2, 2, 2, 3, 3, 4, 5, 6], // first series
        vec![-1, -1, 0, 1, 2, 3],                    // second series
        vec![5, 6, 7, 8],                            // third series
    ];

    // set labels
    let labels = ["first", "second", "third"];

    // configure and draw histogram
    let mut histogram = Histogram::new();
    histogram.set_colors(&["#9de19a", "#e7eca3", "#98a7f2"])
        .set_line_width(10.0)
        .set_stacked(true)
        .set_style("step");
    histogram.draw(&values, &labels);

    // add histogram to plot
    let mut plot = Plot::new();
    plot.add(&histogram)
        .set_frame_border(true, false, true, false)
        .grid_labels_legend("values", "count");

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

doc_histogram.svg

See also integration test in the tests directory.

Output from some integration tests:

integ_histogram_1.svg

Implementations§

source§

impl Histogram

source

pub fn new() -> Self

Creates a new Histogram object

source

pub fn draw<T, U>(&mut self, values: &Vec<Vec<T>>, labels: &[U])
where T: Display, U: Display,

Draws histogram

§Input
  • values - values
  • labels - labels
§Notes
  • The type T must be a number.
  • The type U must be a String or &str.
source

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

Sets the colors for each bar

source

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

Sets the width of the lines

source

pub fn set_style(&mut self, style: &str) -> &mut Self

Sets the type of histogram

Options:

  • bar is a traditional bar-type histogram. If multiple data are given the bars are arranged side by side.
  • barstacked is a bar-type histogram where multiple data are stacked on top of each other.
  • step generates a lineplot that is by default unfilled.
  • stepfilled generates a lineplot that is by default filled.
  • As defined in https://matplotlib.org/stable/api/_as_gen/matplotlib.pyplot.hist.html
source

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

Sets option to draw stacked histogram

source

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

Sets option to skip filling bars

source

pub fn set_number_bins(&mut self, bins: usize) -> &mut Self

Sets the number of bins

Trait Implementations§

source§

impl GraphMaker for Histogram

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

§

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.