diff --git a/lib/matplotlib/cbook/__init__.py b/lib/matplotlib/cbook/__init__.py index a7b11f5d1d91..5e3bc1955bb4 100644 --- a/lib/matplotlib/cbook/__init__.py +++ b/lib/matplotlib/cbook/__init__.py @@ -1416,6 +1416,8 @@ 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 len(X) == 0: + return [[]] if X.ndim == 1 and not isinstance(X[0], collections.abc.Iterable): # 1D array of scalars: directly return it. return [X] diff --git a/lib/matplotlib/tests/test_cbook.py b/lib/matplotlib/tests/test_cbook.py index 431e51c27fc1..5b6f593e7375 100644 --- a/lib/matplotlib/tests/test_cbook.py +++ b/lib/matplotlib/tests/test_cbook.py @@ -495,7 +495,11 @@ def test_flatiter(): def test_reshape2d(): class dummy(): pass + xnew = cbook._reshape_2D([], 'x') + assert np.shape(xnew) == (1, 0) + x = [dummy() for j in range(5)] + xnew = cbook._reshape_2D(x, 'x') assert np.shape(xnew) == (1, 5)