Skip to content

[Bug]: Misleading error from hist2d #21298

@iljah

Description

@iljah

Bug summary

Took a while to track down why hist2d was failing in some of my plots. Turns out I was giving it arrays of different length but the error messages were quite misleading.

Code for reproduction

>>> from matplotlib.pyplot import hist2d
>>> hist2d((1,2,3), (1,2,3,4))

Actual outcome

/usr/lib64/python3.9/site-packages/numpy/core/_asarray.py:171: VisibleDeprecationWarning: Creating an ndarray from ragged nested sequences (which is a list-or-tuple of lists-or-tuples-or ndarrays with different lengths or shapes) is deprecated. If you meant to do this, you must specify 'dtype=object' when creating the ndarray.
return array(a, dtype, copy=False, order=order, subok=True)
Traceback (most recent call last):
File "", line 1, in
File "/usr/lib64/python3.9/site-packages/matplotlib/pyplot.py", line 2877, in hist2d
__ret = gca().hist2d(
File "/usr/lib64/python3.9/site-packages/matplotlib/init.py", line 1364, in inner
return func(ax, *map(sanitize_sequence, args), **kwargs)
File "/usr/lib64/python3.9/site-packages/matplotlib/axes/_axes.py", line 7093, in hist2d
h, xedges, yedges = np.histogram2d(x, y, bins=bins, range=range,
File "<array_function internals>", line 5, in histogram2d
File "/usr/lib64/python3.9/site-packages/numpy/lib/twodim_base.py", line 744, in histogram2d
hist, edges = histogramdd([x, y], bins, range, normed, weights, density)
File "<array_function internals>", line 5, in histogramdd
File "/usr/lib64/python3.9/site-packages/numpy/lib/histograms.py", line 1049, in histogramdd
smin, smax = _get_outer_edges(sample[:,i], range[i])
File "/usr/lib64/python3.9/site-packages/numpy/lib/histograms.py", line 322, in _get_outer_edges
if not (np.isfinite(first_edge) and np.isfinite(last_edge)):
ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()

Expected outcome

ERROR len(arg1) != len(arg2): 3 != 4
or similar

Operating system

Fedora 34

Matplotlib Version

3.4.3

Matplotlib Backend

Qt5Agg

Python version

3.9.7

Jupyter version

nan

Other libraries

No response

Installation

Linux package manager (Debian/Fedora/etc.)

Conda channel

No response

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions