Skip to content

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

Open
@yygrechka

Description

@yygrechka

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions