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.81k stars 276 forks source link

[Bug] Top axis label dropped incorrectly #186

Closed arctic-alpaca closed 2 years ago

arctic-alpaca commented 4 years ago

Hello, I couldn't find a way to draw the top most label on the y axis. The .with_key_points method seems to ignore the top most label. When using

.build_cartesian_2d(
    (0u32..10u32).into_segmented(),
    (0u32..10u32).with_key_points(vec![0, 1, 2, 3, 4, 6, 7, 8, 9, 10]),
)

in the histogram example , 10 still doesn't get printed on the y axis. Is this a current limitation of the library or did I miss something?

38 commented 4 years ago

Hi, thanks for reporting this. I've looked into it, I feel this is a bug which caused by a 1px offset of plotting area. This is most likely link to issue #165

The reason why this is happening is Plotters actually checks if the label is out of change of the subplot

https://github.com/38/plotters/blob/8e120a12ad97cd8b204f311592e87a589243f273/src/chart/context.rs#L331-L335

But at this point, the relative position is -1 (which should be 0) that causes Plotters drop the top label - but previously I haven't realize this is actually a bug.

Meanwhile a temporary workaround would be instead of using 0..10, use 0..11 with the same key point vec.

Thanks again for reporting this!

38 commented 2 years ago

Hi there, just verified this is fixed by the commit I pushed. Close the issue for now.

wangjiawen2013 commented 7 months ago

I tried this method and find that it worked for integer coord, but didn't work for f64 coord. The error occured: image

The following is the code I used:

evcxr_figure((640, 240), |root| {
    let mut chart = ChartBuilder::on(&root).x_label_area_size(40).y_label_area_size(40)
   .build_cartesian_2d(
       (0.0..100.0).with_key_points(vec![-5.0, 0.0, 10.0,20.0,50.0,90.0]),   // <= This line will make the plot shows 4 tick marks at 1, 20, 50, 90
       0..10
    ).unwrap();
    chart.configure_mesh()
        .x_desc("X")
        .y_desc("Y")
        .y_labels(10)
        .light_line_style(&TRANSPARENT)
        .label_style(("sans-serif", 15, &BLACK).into_text_style(&root))  // εζ ‡ζ ‡η­Ύε±žζ€§
        //.x_label_formatter(&|v| format!("{:.1}", v))
        //.y_label_formatter(&|v| format!("{:.1}", v))
        .disable_x_mesh()
        .draw().unwrap();
    Ok(())
}).style("width:60%")