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

Skip to content

Commit 57708cb

Browse files
dstansbymeeseeksmachine
authored andcommitted
Backport PR #17821: FIX: Keep lists of lists of one scalar each 2D in _reshape_2D
1 parent 835348c commit 57708cb

File tree

2 files changed

+10
-2
lines changed

2 files changed

+10
-2
lines changed

lib/matplotlib/cbook/__init__.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1363,12 +1363,12 @@ def _reshape_2D(X, name):
13631363
result = []
13641364
is_1d = True
13651365
for xi in X:
1366+
if isinstance(xi, collections.abc.Iterable):
1367+
is_1d = False
13661368
xi = np.asanyarray(xi)
13671369
nd = np.ndim(xi)
13681370
if nd > 1:
13691371
raise ValueError(f'{name} must have 2 or fewer dimensions')
1370-
elif nd == 1 and len(xi) != 1:
1371-
is_1d = False
13721372
result.append(xi.reshape(-1))
13731373

13741374
if is_1d:

lib/matplotlib/tests/test_cbook.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -502,6 +502,14 @@ class dummy:
502502
xnew = cbook._reshape_2D(x, 'x')
503503
assert np.shape(xnew) == (5, 3)
504504

505+
# Test a list of lists which are all of length 1
506+
x = [[1], [2], [3]]
507+
xnew = cbook._reshape_2D(x, 'x')
508+
assert isinstance(xnew, list)
509+
assert isinstance(xnew[0], np.ndarray) and xnew[0].shape == (1,)
510+
assert isinstance(xnew[1], np.ndarray) and xnew[1].shape == (1,)
511+
assert isinstance(xnew[2], np.ndarray) and xnew[2].shape == (1,)
512+
505513
# Now test with a list of lists with different lengths, which means the
506514
# array will internally be converted to a 1D object array of lists
507515
x = [[1, 2, 3], [3, 4], [2]]

0 commit comments

Comments
 (0)