plotters icon indicating copy to clipboard operation
plotters copied to clipboard

Automatically bin the data for the histogram

Open lu-zero opened this issue 4 years ago • 1 comments

Would be nice to have an option to tell the histogram to automatically split the data in a number of bins.

lu-zero avatar Jan 27 '21 15:01 lu-zero

I am interested to work on this. I was wondering if there is a specific recommendation on how to do this? I was thinking something along these lines:

// We can use float data values because it will automatically bin them
let data = [1.2, 1.4, 2.0, 2.1, 1.1, 3.6, 3.6, 2.2, 2.2, 1.1, 1.6];
// New struct named `HistogramBuilder`
HistogramBuilder::vertical(&chart_context)
  .style(BLUE.filled())
  .margin(10)
  // New function to set the number of bins you want
  .bins(10)
  // New function for setting the beginning of the first bin and the end of the last bin
  .range(0..3)
  .data(data.map(|x| (x, 1)))
  // New function to create a `Histogram` out of `HistogramBuilder`
  .build()

There are some design decisions that I made here:

  1. The names bins and range is similar to matplotlib.hist(). Is there a better design that I can use here?
  2. I am using a new struct named HistogramBuilder. This is because Histogram alone cannot handle the case where the user sets the data before setting the range or bins.
  3. What happens if you don't set the bins and range:
    • Try to keep backwards compatibility:
      • Bins and range not set: the "bin width" is the minimum distance between the sorted base values. (This is almost backwards compatible because in the past the "bin width" was always 1 with integer data values).
      • Set bins or range: Bins defaults to 10 and ranges defults to data_min..data_max.
    • Use defaults similar to matplotlib.hist().
      • Bins defaults to 10 and ranges defults to data_min..data_max. (This will change how the old Histograms were drawn, so if someone upgrades the crate, they will see different histograms than before)

I am new to Rust and I also have little experience in open-source development, so any other recommendation of how it might look like is really appreciated.

AliMMehr avatar Oct 22 '23 18:10 AliMMehr