plotters icon indicating copy to clipboard operation
plotters copied to clipboard

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

Open yygrechka opened this issue 1 year ago • 4 comments

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 avatar Oct 26 '24 17:10 yygrechka

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

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

yygrechka avatar Oct 26 '24 18:10 yygrechka

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.

yygrechka avatar Oct 26 '24 18:10 yygrechka

@yygrechka, Could you add a filter? instead of chart.draw_series(points1.iter().zip(points2.iter()).map(|(x,y)| Circle::new((*x,*y), 3, RED.filled()))); do chart.draw_series(points1.iter().zip(points2.iter()).filter(|(x,y)| **y < 1f64).map(|(x,y)| Circle::new((*x,*y), 3, RED.filled())));

mchant avatar Dec 01 '24 03:12 mchant

@mchant Adding a filter works easily for points, but is more complicated for lines, see e.g. #429

jbncode avatar Dec 01 '24 19:12 jbncode