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

Skip to content

Commit 6060a40

Browse files
authored
Merge pull request #8116 from anntzer/simplify-_reshape_2D
Simplify _reshape_2D.
2 parents 43ac39b + 71fbadb commit 6060a40

File tree

1 file changed

+9
-25
lines changed

1 file changed

+9
-25
lines changed

lib/matplotlib/cbook/__init__.py

Lines changed: 9 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1983,32 +1983,16 @@ def _reshape_2D(X):
19831983
19841984
v is iterable and can be used to instantiate a 1D array.
19851985
"""
1986-
if hasattr(X, 'shape'):
1987-
# one item
1988-
if len(X.shape) == 1:
1989-
if hasattr(X[0], 'shape'):
1990-
X = list(X)
1991-
else:
1992-
X = [X, ]
1993-
1994-
# several items
1995-
elif len(X.shape) == 2:
1996-
nrows, ncols = X.shape
1997-
if nrows == 1:
1998-
X = [X]
1999-
elif ncols == 1:
2000-
X = [X.ravel()]
2001-
else:
2002-
X = [X[:, i] for i in xrange(ncols)]
2003-
else:
2004-
raise ValueError("input `X` must have 2 or fewer dimensions")
2005-
2006-
if not hasattr(X[0], '__len__'):
2007-
X = [X]
1986+
# Iterate over columns for ndarrays, over rows otherwise.
1987+
X = X.T if isinstance(X, np.ndarray) else np.asarray(X)
1988+
if X.ndim == 1 and X.dtype.type != np.object_:
1989+
# 1D array of scalars: directly return it.
1990+
return [X]
1991+
elif X.ndim in [1, 2]:
1992+
# 2D array, or 1D array of iterables: flatten them first.
1993+
return [np.reshape(x, -1) for x in X]
20081994
else:
2009-
X = [np.ravel(x) for x in X]
2010-
2011-
return X
1995+
raise ValueError("input `X` must have 2 or fewer dimensions")
20121996

20131997

20141998
def violin_stats(X, method, points=100):

0 commit comments

Comments
 (0)