plotters
plotters copied to clipboard
Automatically bin the data for the histogram
Would be nice to have an option to tell the histogram to automatically split the data in a number of bins.
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:
- The names
bins
andrange
is similar to matplotlib.hist(). Is there a better design that I can use here? - I am using a new struct named
HistogramBuilder
. This is becauseHistogram
alone cannot handle the case where the user sets thedata
before setting therange
orbins
. - 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)
- Bins defaults to 10 and ranges defults to
- Try to keep backwards compatibility:
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.