Skip to content

Histogram function can't handle all-same data #2463

@Xerxes004

Description

@Xerxes004

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.

[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:

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:

  1. Create an array of all zeros called data
  2. Attempt to create a Histogram like: ScottPlot.Statistics.Common.Histogram(data, min: 0, max: 0, binSize: 1)
  3. Crash to desktop

System Details

  • ScottPlot Version: 4.1.61
  • Operating System: Windows 10
  • Application Type: WPF
  • .NET Version: .NET 7

Metadata

Metadata

Assignees

No one assigned

    Labels

    BUGunexpected behavior

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions