8000 FIX: allow reshape 2-D to return a bare 1-d list · matplotlib/matplotlib@9eac832 · GitHub
[go: up one dir, main page]

Skip to content

Commit 9eac832

Browse files
committed
FIX: allow reshape 2-D to return a bare 1-d list
1 parent ec18892 commit 9eac832

File tree

2 files changed

+24
-6
lines changed

2 files changed

+24
-6
lines changed

lib/matplotlib/cbook/__init__.py

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1393,15 +1393,12 @@ def _reshape_2D(X, name):
13931393
"""
13941394
# Iterate over columns for ndarrays, over rows otherwise.
13951395
X = np.atleast_1d(X.T if isinstance(X, np.ndarray) else np.asarray(X))
1396-
if X.ndim == 1 and X.dtype.type != np.object_:
1396+
if X.ndim == 1 and not isinstance(X[0], collections.abc.Iterable):
13971397
# 1D array of scalars: directly return it.
13981398
return [X]
13991399
elif X.ndim in [1, 2]:
1400-
if hasattr(X[0], '__len__'):
1401-
# 2D array, or 1D array of iterables: flatten them first.
1402-
return [np.reshape(x, -1) for x in X]
1403-
else:
1404-
return [X]
1400+
# 2D array, or 1D array of iterables: flatten them first.
1401+
return [np.reshape(x, -1) for x in X]
14051402
else:
14061403
raise ValueError("{} must have 2 or fewer dimensions".format(name))
14071404

lib/matplotlib/tests/test_cbook.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -482,3 +482,24 @@ def test_flatiter():
482482

483483
assert 0 == next(it)
484484
assert 1 == next(it)
485+
486+
487+
def test_reshape2d():
488+
class dummy():
489+
pass
490+
x = [dummy() for j in range(5)]
491 876F +
xnew = cbook._reshape_2D(x, 'x')
492+
assert np.shape(xnew) == (1, 5)
493+
494+
x = np.arange(5)
495+
xnew = cbook._reshape_2D(x, 'x')
496+
assert np.shape(xnew) == (1, 5)
497+
498+
x = [[dummy() for j in range(5)] for i in range(3)]
499+
xnew = cbook._reshape_2D(x, 'x')
500+
assert np.shape(xnew) == (3, 5)
501+
502+
# this is strange behaviour, but...
503+
x = np.random.rand(3, 5)
504+
xnew = cbook._reshape_2D(x, 'x')
505+
assert np.shape(xnew) == (5, 3)

0 commit comments

Comments
 (0)
0