From ec18892d56f69fa9fa67ec2ba40f426152f2b2bd Mon Sep 17 00:00:00 2001 From: Jody Klymak Date: Thu, 23 Aug 2018 11:20:41 -0700 Subject: [PATCH 1/2] FIX: allow reshape 2-D to return a bare 1-d list --- lib/matplotlib/cbook/__init__.py | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/lib/matplotlib/cbook/__init__.py b/lib/matplotlib/cbook/__init__.py index 2c5661253873..1d73f8445dc8 100644 --- a/lib/matplotlib/cbook/__init__.py +++ b/lib/matplotlib/cbook/__init__.py @@ -1397,8 +1397,11 @@ def _reshape_2D(X, name): # 1D array of scalars: directly return it. return [X] elif X.ndim in [1, 2]: - # 2D array, or 1D array of iterables: flatten them first. - return [np.reshape(x, -1) for x in X] + if hasattr(X[0], '__len__'): + # 2D array, or 1D array of iterables: flatten them first. + return [np.reshape(x, -1) for x in X] + else: + return [X] else: raise ValueError("{} must have 2 or fewer dimensions".format(name)) From 9eac832db3b5e281b98d679577fbcff8e93d109f Mon Sep 17 00:00:00 2001 From: Jody Klymak Date: Wed, 29 Aug 2018 16:34:55 -0700 Subject: [PATCH 2/2] FIX: allow reshape 2-D to return a bare 1-d list --- lib/matplotlib/cbook/__init__.py | 9 +++------ lib/matplotlib/tests/test_cbook.py | 21 +++++++++++++++++++++ 2 files changed, 24 insertions(+), 6 deletions(-) diff --git a/lib/matplotlib/cbook/__init__.py b/lib/matplotlib/cbook/__init__.py index 1d73f8445dc8..88b35e171677 100644 --- a/lib/matplotlib/cbook/__init__.py +++ b/lib/matplotlib/cbook/__init__.py @@ -1393,15 +1393,12 @@ def _reshape_2D(X, name): """ # Iterate over columns for ndarrays, over rows otherwise. X = np.atleast_1d(X.T if isinstance(X, np.ndarray) else np.asarray(X)) - if X.ndim == 1 and X.dtype.type != np.object_: + if X.ndim == 1 and not isinstance(X[0], collections.abc.Iterable): # 1D array of scalars: directly return it. return [X] elif X.ndim in [1, 2]: - if hasattr(X[0], '__len__'): - # 2D array, or 1D array of iterables: flatten them first. - return [np.reshape(x, -1) for x in X] - else: - return [X] + # 2D array, or 1D array of iterables: flatten them first. + return [np.reshape(x, -1) for x in X] else: raise ValueError("{} must have 2 or fewer dimensions".format(name)) diff --git a/lib/matplotlib/tests/test_cbook.py b/lib/matplotlib/tests/test_cbook.py index 745844fb6a1c..8253b8a08da2 100644 --- a/lib/matplotlib/tests/test_cbook.py +++ b/lib/matplotlib/tests/test_cbook.py @@ -482,3 +482,24 @@ def test_flatiter(): assert 0 == next(it) assert 1 == next(it) + + +def test_reshape2d(): + class dummy(): + pass + x = [dummy() for j in range(5)] + xnew = cbook._reshape_2D(x, 'x') + assert np.shape(xnew) == (1, 5) + + x = np.arange(5) + xnew = cbook._reshape_2D(x, 'x') + assert np.shape(xnew) == (1, 5) + + x = [[dummy() for j in range(5)] for i in range(3)] + xnew = cbook._reshape_2D(x, 'x') + assert np.shape(xnew) == (3, 5) + + # this is strange behaviour, but... + x = np.random.rand(3, 5) + xnew = cbook._reshape_2D(x, 'x') + assert np.shape(xnew) == (5, 3)