Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit e4b7975

Browse files
authored
Merge pull request #22018 from jakevdp/fix-hist-iter
BUG: fix handling of zero-dimensional arrays in cbook._reshape_2D
2 parents ec99c15 + a64c58a commit e4b7975

File tree

2 files changed

+14
-3
lines changed

2 files changed

+14
-3
lines changed

lib/matplotlib/cbook/__init__.py

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1389,9 +1389,13 @@ def _reshape_2D(X, name):
13891389
for xi in X:
13901390
# check if this is iterable, except for strings which we
13911391
# treat as singletons.
1392-
if (isinstance(xi, collections.abc.Iterable) and
1393-
not isinstance(xi, str)):
1394-
is_1d = False
1392+
if not isinstance(xi, str):
1393+
try:
1394+
iter(xi)
1395+
except TypeError:
1396+
pass
1397+
else:
1398+
is_1d = False
13951399
xi = np.asanyarray(xi)
13961400
nd = np.ndim(xi)
13971401
if nd > 1:

lib/matplotlib/tests/test_cbook.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -610,6 +610,13 @@ class Dummy:
610610
assert isinstance(xnew[1], np.ndarray) and xnew[1].shape == (1,)
611611
assert isinstance(xnew[2], np.ndarray) and xnew[2].shape == (1,)
612612

613+
# Test a list of zero-dimensional arrays
614+
x = [np.array(0), np.array(1), np.array(2)]
615+
xnew = cbook._reshape_2D(x, 'x')
616+
assert isinstance(xnew, list)
617+
assert len(xnew) == 1
618+
assert isinstance(xnew[0], np.ndarray) and xnew[0].shape == (3,)
619+
613620
# Now test with a list of lists with different lengths, which means the
614621
# array will internally be converted to a 1D object array of lists
615622
x = [[1, 2, 3], [3, 4], [2]]

0 commit comments

Comments
 (0)