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(())
}
§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(())
}
§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(())
}
§More examples
See also integration test in the tests directory.
Implementations§
Source§impl Barplot
impl Barplot
Sourcepub fn draw_with_str<'a, T, U>(&mut self, x: &[&str], y: &'a T)
pub fn draw_with_str<'a, T, U>(&mut self, x: &[&str], y: &'a T)
Draws the bar plot with strings
Sourcepub fn set_colors(&mut self, colors: &[&str]) -> &mut Self
pub fn set_colors(&mut self, colors: &[&str]) -> &mut Self
Sets the colors for each bar
Sourcepub fn set_bottom(&mut self, bottom: &[f64]) -> &mut Self
pub fn set_bottom(&mut self, bottom: &[f64]) -> &mut Self
Sets the vertical offset to stack bars
Sourcepub fn set_with_text(&mut self, position: &str) -> &mut Self
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
Sourcepub fn set_horizontal(&mut self, flag: bool) -> &mut Self
pub fn set_horizontal(&mut self, flag: bool) -> &mut Self
Enables drawing horizontal bars
Sourcepub fn set_errors(&mut self, errors: &[f64]) -> &mut Self
pub fn set_errors(&mut self, errors: &[f64]) -> &mut Self
Enables error indicators
Trait Implementations§
Source§impl GraphMaker for Barplot
impl GraphMaker for Barplot
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 Barplot
impl RefUnwindSafe for Barplot
impl Send for Barplot
impl Sync for Barplot
impl Unpin for Barplot
impl UnwindSafe for Barplot
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