pub struct Barplot { /* private fields */ }Expand description
Generates a Barplot plot
§Examples
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_1.svg")?;
Ok(())
}See also integration test in the tests directory.
Implementations§
source§impl Barplot
impl Barplot
sourcepub fn draw_with_str<'a, S, T, U>(&mut self, x: &[S], y: &'a T)
pub fn draw_with_str<'a, S, T, U>(&mut self, x: &[S], y: &'a T)
Draws the bar plot with strings
§Notes
- The type
Sof the input array must be a string. - The type
Uof the input array must be a number.
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
pub fn set_horizontal(&mut self, flag: bool) -> &mut Self
pub fn set_x_errors(&mut self, errors: &[f64]) -> &mut Self
pub fn set_extra(&mut self, extra: &str) -> &mut Self
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