use plotters::prelude::*;
use plotters_statistical::style::{fill_style, palette_color};
use plotters_statistical::{BoxPlotSeries, BoxStyle};
fn main() -> Result<(), Box<dyn std::error::Error>> {
let root = SVGBackend::new("box_plot.svg", (800, 500)).into_drawing_area();
root.fill(&WHITE)?;
let groups = vec![
(1.0f64, vec![4.0, 5.0, 5.5, 6.0, 6.5, 7.0, 7.5, 8.0]),
(2.0f64, vec![2.0, 3.0, 3.2, 3.5, 4.0, 4.5, 5.0, 5.2]),
(3.0f64, vec![5.0, 6.0, 6.5, 7.0, 7.5, 8.0, 9.0, 18.0]),
];
let mut chart = ChartBuilder::on(&root)
.caption("Box plot (3 groups)", ("sans-serif", 24))
.margin(20)
.set_label_area_size(LabelAreaPosition::Left, 45)
.set_label_area_size(LabelAreaPosition::Bottom, 45)
.build_cartesian_2d(0.5f64..3.5f64, 0f64..20f64)?;
chart
.configure_mesh()
.x_desc("group")
.y_desc("value")
.draw()?;
let style = BoxStyle {
box_fill: fill_style(&palette_color(3).mix(0.4)),
..BoxStyle::default()
};
chart.draw_series(BoxPlotSeries::from_samples(groups)?.width(48).style(style))?;
root.present()?;
println!("wrote box_plot.svg");
Ok(())
}