diff options
author | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-05-18 02:49:50 +0000 |
---|---|---|
committer | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-05-18 02:49:50 +0000 |
commit | 9835e2ae736235810b4ea1c162ca5e65c547e770 (patch) | |
tree | 3fcebf40ed70e581d776a8a4c65923e8ec20e026 /vendor/plotters/examples/normal-dist.rs | |
parent | Releasing progress-linux version 1.70.0+dfsg2-1~progress7.99u1. (diff) | |
download | rustc-9835e2ae736235810b4ea1c162ca5e65c547e770.tar.xz rustc-9835e2ae736235810b4ea1c162ca5e65c547e770.zip |
Merging upstream version 1.71.1+dfsg1.
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'vendor/plotters/examples/normal-dist.rs')
-rw-r--r-- | vendor/plotters/examples/normal-dist.rs | 66 |
1 files changed, 66 insertions, 0 deletions
diff --git a/vendor/plotters/examples/normal-dist.rs b/vendor/plotters/examples/normal-dist.rs new file mode 100644 index 000000000..be4878630 --- /dev/null +++ b/vendor/plotters/examples/normal-dist.rs @@ -0,0 +1,66 @@ +use plotters::prelude::*; + +use rand::SeedableRng; +use rand_distr::{Distribution, Normal}; +use rand_xorshift::XorShiftRng; + +const OUT_FILE_NAME: &'static str = "plotters-doc-data/normal-dist.png"; +fn main() -> Result<(), Box<dyn std::error::Error>> { + let root = BitMapBackend::new(OUT_FILE_NAME, (1024, 768)).into_drawing_area(); + + root.fill(&WHITE)?; + + let sd = 0.13; + + let random_points: Vec<(f64, f64)> = { + let norm_dist = Normal::new(0.5, sd).unwrap(); + let mut x_rand = XorShiftRng::from_seed(*b"MyFragileSeed123"); + let mut y_rand = XorShiftRng::from_seed(*b"MyFragileSeed321"); + let x_iter = norm_dist.sample_iter(&mut x_rand); + let y_iter = norm_dist.sample_iter(&mut y_rand); + x_iter.zip(y_iter).take(5000).collect() + }; + + let areas = root.split_by_breakpoints([944], [80]); + + let mut x_hist_ctx = ChartBuilder::on(&areas[0]) + .y_label_area_size(40) + .build_cartesian_2d((0.0..1.0).step(0.01).use_round().into_segmented(), 0..250)?; + let mut y_hist_ctx = ChartBuilder::on(&areas[3]) + .x_label_area_size(40) + .build_cartesian_2d(0..250, (0.0..1.0).step(0.01).use_round())?; + let mut scatter_ctx = ChartBuilder::on(&areas[2]) + .x_label_area_size(40) + .y_label_area_size(40) + .build_cartesian_2d(0f64..1f64, 0f64..1f64)?; + scatter_ctx + .configure_mesh() + .disable_x_mesh() + .disable_y_mesh() + .draw()?; + scatter_ctx.draw_series( + random_points + .iter() + .map(|(x, y)| Circle::new((*x, *y), 2, GREEN.filled())), + )?; + let x_hist = Histogram::vertical(&x_hist_ctx) + .style(GREEN.filled()) + .margin(0) + .data(random_points.iter().map(|(x, _)| (*x, 1))); + let y_hist = Histogram::horizontal(&y_hist_ctx) + .style(GREEN.filled()) + .margin(0) + .data(random_points.iter().map(|(_, y)| (*y, 1))); + x_hist_ctx.draw_series(x_hist)?; + y_hist_ctx.draw_series(y_hist)?; + + // To avoid the IO failure being ignored silently, we manually call the present function + root.present().expect("Unable to write result to file, please make sure 'plotters-doc-data' dir exists under current dir"); + println!("Result has been saved to {}", OUT_FILE_NAME); + + Ok(()) +} +#[test] +fn entry_point() { + main().unwrap() +} |