-
Notifications
You must be signed in to change notification settings - Fork 981
Closed
Labels
BUGunexpected behaviorunexpected behavior
Description
Bug Report
Issue: Given an array of data where all the values are the same, the ScottPlot.Statistics.Common.Histogram function throws a System.IndexOutOfRangeException. This happens because the min and max of this data are the same, which causes the bin count to be 0.
ScottPlot/src/ScottPlot4/ScottPlot/Statistics/Common.cs
Lines 327 to 333 in 3b67060
| [Obsolete("This method is obsolete. Consider using ScottPlot.Statistics.Histogram as demonstrated by the ScottPlot Cookbook.")] | |
| public static (double[] hist, double[] binEdges) Histogram(double[] values, double min, double max, double binSize, bool density = false) | |
| { | |
| int binCount = (int)Math.Ceiling((max - min) / binSize); | |
| max = min + binCount * binSize; | |
| return Histogram(values, binCount, density, min, max); | |
| } |
Similarly, the new Histogram class has an incorrect assumption about min and max:
ScottPlot/src/Shared/ScottPlot/Statistics/Histogram.cs
Lines 78 to 82 in 3b67060
| public Histogram(double min, double max, int binCount, bool addOutliersToEdgeBins = false, bool addFinalBin = true) | |
| { | |
| if (min >= max) | |
| throw new ArgumentException($"{nameof(max)} must be greater than {nameof(min)}"); | |
Histograms of all-the-same data may be useless, but are still valid. The code should be updated to handle min and max being the same value.
Reproducing:
- Create an array of all zeros called
data - Attempt to create a Histogram like:
ScottPlot.Statistics.Common.Histogram(data, min: 0, max: 0, binSize: 1) - Crash to desktop
System Details
- ScottPlot Version: 4.1.61
- Operating System: Windows 10
- Application Type: WPF
- .NET Version: .NET 7
Reactions are currently unavailable
Metadata
Metadata
Assignees
Labels
BUGunexpected behaviorunexpected behavior