Skip to content

Commit

Permalink
use dynamic height range for plotting example chart
Browse files Browse the repository at this point in the history
  • Loading branch information
HyperCodec committed Jun 13, 2024
1 parent 470c1c9 commit 0c63895
Showing 1 changed file with 24 additions and 17 deletions.
41 changes: 24 additions & 17 deletions examples/plot.rs
Original file line number Diff line number Diff line change
Expand Up @@ -119,21 +119,6 @@ fn main() -> Result<(), Box<dyn Error>> {

println!("Training complete, collecting data and building chart...");

let root = SVGBackend::new(OUTPUT_FILE_NAME, (640, 480)).into_drawing_area();
root.fill(&WHITE)?;

let mut chart = ChartBuilder::on(&root)
.caption(
"agent fitness values per generation",
("sans-serif", 50).into_font(),
)
.margin(5)
.x_label_area_size(30)
.y_label_area_size(30)
.build_cartesian_2d(0usize..GENS, 0f32..1000.0)?;

chart.configure_mesh().draw()?;

let data: Vec<_> = Arc::into_inner(performance_stats)
.unwrap()
.into_inner()
Expand All @@ -142,9 +127,16 @@ fn main() -> Result<(), Box<dyn Error>> {
.enumerate()
.collect();

let highs = data
let highs: Vec<_> = data
.iter()
.map(|(i, PerformanceStats { high, .. })| (*i, *high))
.collect();

let highest_overall = highs
.iter()
.map(|(i, PerformanceStats { high, .. })| (*i, *high));
.map(|(_, h)| h)
.max_by(|&a, &b| a.partial_cmp(b).unwrap())
.unwrap();

let medians = data
.iter()
Expand All @@ -154,6 +146,21 @@ fn main() -> Result<(), Box<dyn Error>> {
.iter()
.map(|(i, PerformanceStats { low, .. })| (*i, *low));

let root = SVGBackend::new(OUTPUT_FILE_NAME, (640, 480)).into_drawing_area();
root.fill(&WHITE)?;

let mut chart = ChartBuilder::on(&root)
.caption(
"agent fitness values per generation",
("sans-serif", 50).into_font(),
)
.margin(5)
.x_label_area_size(30)
.y_label_area_size(30)
.build_cartesian_2d(0usize..GENS, 0f32..*highest_overall)?;

chart.configure_mesh().draw()?;

chart
.draw_series(LineSeries::new(highs, &GREEN))?
.label("high");
Expand Down

0 comments on commit 0c63895

Please sign in to comment.