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

Skip to content

FIX: allow reshape 2-D to return a bare 1-d list #11921

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Sep 9, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion lib/matplotlib/cbook/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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]:
Expand Down
21 changes: 21 additions & 0 deletions lib/matplotlib/tests/test_cbook.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)