Skip to content

Histogram index out of range exception when values contain upper boundary #1348

@jw-suh

Description

@jw-suh

Bug Report

Issue:
Creating a histogram with any value that is equal to the max value will throw 'System.IndexOutOfRangeException', if there are more values than bins. This happens easily when using the overload that takes the binSize and not the bin count.

This is the offending code, taken from ScottPlot.Statistics.Common.Histogram

double[] hist = new double[binCount];

...

if (values[i] == max)
{
    hist[values.Length - 1] += 1;
    continue;
}

Even with only a few values this can get out of bounds easily, as long as the min and max properties reduce the binCount to less than the value count.

Reproducing:
Use the following code to easily reproduce the exception.

 var values = new double[] { 10, 20, 20, 10, 20, 10, 20, 10, 10, 20, 10, 10, 20, 15, 10, 30 };
 var (counts, binEdges) = ScottPlot.Statistics.Common.Histogram(values, 15, 30, 1, false);

System Details

  • ScottPlot Version: 4.1.25
  • Operating System: Windows 10 Pro 21H1
  • Application Type: console
  • .NET Version: .NET core 3.1 LTS

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