plotters-rs / plotters

A rust drawing library for high quality data plotting for both WASM and native, statically and realtimely 🦀 📈🚀
https://plotters-rs.github.io/home/
MIT License
3.91k stars 280 forks source link

[BUG] Surprising plotting behavior when point is out of range. #656

Open yygrechka opened 1 month ago

yygrechka commented 1 month ago

Describe the bug I've been playing around with the plotters library and even creating my own utility crate using it, and I was surprised to see that when a point is out of range of the plotting area, it is drawn somewhere close to the edge of the plotting area (the expected behavior would be that it is not drawn at all). I would say that this causes the plot to be "incorrect" and thus qualifies as a "BUG". Screenshot from 2024-10-26 11-48-55

To Reproduce In a jupyter notebook:

1st cell:

:dep plotters = { version = "0.3.7", defualt_features=false, features=["evcxr", "all_series"]}
use plotters::prelude::*;

2nd cell:

let points1 = vec![0.2, 0.4, 0.6, 0.8];
let points2 = vec![0.2, 0.9, 1.6, 2.3];

evcxr_figure((640, 480), |root| {
    // The following code will create a chart context

    let (up, down) = root.split_vertically(240);
    let mut chart = ChartBuilder::on(&up)
        .caption("(Incorrect) Should be linear; points do not fit into chart and are forced into it", ("Arial", 20).into_font())
        .x_label_area_size(40)
        .y_label_area_size(40)
        .build_cartesian_2d(0f64..1f64, 0f64..1f64)?;

    chart.configure_mesh()
        .disable_x_mesh()
        .disable_y_mesh()
        .draw()?;

    chart.draw_series(points1.iter().zip(points2.iter()).map(|(x,y)| Circle::new((*x,*y), 3, RED.filled())));

    let mut chart = ChartBuilder::on(&down)
        .caption("(Correct) Linear relationship; points fit into chart", ("Arial", 20).into_font())
        .x_label_area_size(40)
        .y_label_area_size(40)
        .build_cartesian_2d(0f64..1f64, 0f64..3f64)?;

    chart.configure_mesh()
        .disable_x_mesh()
        .disable_y_mesh()
        .draw()?;

    chart.draw_series(points1.iter().zip(points2.iter()).map(|(x,y)| Circle::new((*x,*y), 3, RED.filled())));

    Ok(())
}).style("width:60%")

Version Information plotters version 0.3.7

yygrechka commented 1 month ago

Seems very related to: https://github.com/plotters-rs/plotters/issues/622

and https://github.com/khonsulabs/cushy/issues/179

yygrechka commented 1 month ago

Attempted the following import:

:dep plotters = { git = "https://github.com/plotters-rs/plotters", branch = "master", defualt_features=false, features=["evcxr", "all_series"] } 
use plotters::prelude::*;

Got the same behavior.