[Numpy-discussion] histogram2d and decreasing bin edges

Matthias Frank mfrank at ari.uni-heidelberg.de
Tue Jan 11 14:21:40 EST 2011

Hi all,

I've noticed a change in numpy.histogram2d between (possibly very much)
older versions and the current one: The function can no longer handle
the situation where bin edges decrease instead of increasing monotonically.

The reason for this seems to be the handling of outliers histogramdd,
see the output of minimal example below.
If I understand correctly, this is the only place where histogramdd
implicitly assumes monotonically increasing bin edges. If so, this could
be fixed to work with increasing and decreasing bin edges by taking
abs(dedges[i]).min() when calculating the rounding precision. If not, it
might be more consistent, and produce a more meaningful error message,
if histogram2d asserted that bin edges increase monotonically and
otherwise raised an AttributeError as the 1-d histogram() function does
in that case (see below)


In [1]: import numpy
In [2]: numpy.__version__
Out[2]: '1.5.1'

In [3]: ascending=numpy.array([0,1])
In [4]: descending=numpy.array([1,0])
In [5]: numpy.histogram2d([0.5],[0.5],bins=(ascending,ascending))
Out[5]: (array([[ 1.]]), array([ 0.,  1.]), array([ 0.,  1.]))

In [6]: numpy.histogram2d([0.5],[0.5],bins=(descending,descending))
Warning: invalid value encountered in log10
ValueError                                Traceback (most recent call last)
/lib/python2.6/site-packages/numpy/lib/twodim_base.pyc in histogram2d(x,
y, bins, range, normed, weights)
    613         xedges = yedges = asarray(bins, float)
    614         bins = [xedges, yedges]
--> 615     hist, edges = histogramdd([x,y], bins, range, normed, weights)
    616     return hist, edges[0], edges[1]

/lib/python2.6/site-packages/numpy/lib/function_base.pyc in
histogramdd(sample, bins, range, normed, weights)
    312     for i in arange(D):
    313         # Rounding precision
--> 314         decimal = int(-log10(dedges[i].min())) +6
    315         # Find which points are on the rightmost edge.
    316         on_edge = where(around(sample[:,i], decimal) ==

ValueError: cannot convert float NaN to integer

Behavior of the 1-d histogram()

In [8]: numpy.histogram([0.5],bins=ascending)
Out[8]: (array([1]), array([0, 1]))

In [9]: numpy.histogram([0.5],bins=descending)
AttributeError                            Traceback (most recent call last)
/lib/python2.6/site-packages/numpy/lib/function_base.pyc in histogram(a,
bins, range, normed, weights)
    160         if (np.diff(bins) < 0).any():
    161             raise AttributeError(
--> 162                     'bins must increase monotonically.')
    164     # Histogram is an integer or a float array depending on the

AttributeError: bins must increase monotonically.

More information about the NumPy-Discussion mailing list