Skip to content

BUG: zero-width histogram bins if the data values are in a small range close to numeric precision #27142

@timhoffm

Description

@timhoffm

Describe the issue:

np.histogram can produce zero-width bins if the data values are in a small range close to numeric precision. Noted in matplotlib/matplotlib#28685

I'm aware that there is no reasonable representation / that calculating a reasonable representation close to the numeric precision is not possible. But would it be an option to check the bin-width and error out instead of returning nonsense histogram binning?

Reproduce the code example:

In [1]: import numpy as np

In [2]: a = np.array([1, 1+2e-16] * 10)

In [3]: counts, lims = np.histogram(a, bins=10)

In [4]: counts
Out[4]: array([ 0, 10,  0,  0,  0,  0,  0,  0,  0, 10])

In [5]: lims
Out[5]: array([1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1.])

In [6]: np.diff(lims)
Out[6]: 
array([0.00000000e+00, 0.00000000e+00, 0.00000000e+00, 0.00000000e+00,
       0.00000000e+00, 2.22044605e-16, 0.00000000e+00, 0.00000000e+00,
       0.00000000e+00, 0.00000000e+00])

Error message:

No response

Python and NumPy Versions:

numpy 1.26.4
python 3.12.2

Runtime Environment:

No response

Context for the issue:

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions