diff --git a/lib/matplotlib/cbook/__init__.py b/lib/matplotlib/cbook/__init__.py index 2c5661253873..88b35e171677 100644 --- a/lib/matplotlib/cbook/__init__.py +++ b/lib/matplotlib/cbook/__init__.py @@ -1393,7 +1393,7 @@ 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]: 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)