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

Skip to content

Commit e842b17

Browse files
committed
Rewrite _reshape_2D to not use ragged ndarrays.
This is a raising a deprecation warning in NumPy 1.19, may go away some time later, and is not strictly necessary for the implementation.
1 parent d0c86a3 commit e842b17

File tree

1 file changed

+32
-8
lines changed

1 file changed

+32
-8
lines changed

lib/matplotlib/cbook/__init__.py

Lines changed: 32 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1322,18 +1322,42 @@ def _reshape_2D(X, name):
13221322
13231323
*name* is used to generate the error message for invalid inputs.
13241324
"""
1325-
# Iterate over columns for ndarrays, over rows otherwise.
1326-
X = np.atleast_1d(X.T if isinstance(X, np.ndarray) else np.asarray(X))
1325+
# Iterate over columns for ndarrays.
1326+
if isinstance(X, np.ndarray):
1327+
X = X.T
1328+
1329+
if len(X) == 0:
1330+
return [[]]
1331+
elif X.ndim == 1 and np.ndim(X[0]) == 0:
1332+
# 1D array of scalars: directly return it.
1333+
return [X]
1334+
elif X.ndim in [1, 2]:
1335+
# 2D array, or 1D array of iterables: flatten them first.
1336+
return [np.reshape(x, -1) for x in X]
1337+
else:
1338+
raise ValueError("{} must have 2 or fewer dimensions".format(name))
1339+
1340+
# Iterate over rows for non-ndarrays.
13271341
if len(X) == 0:
13281342
return [[]]
1329-
elif X.ndim == 1 and np.ndim(X[0]) == 0:
1343+
1344+
result = []
1345+
is_1d = True
1346+
for xi in X:
1347+
xi = np.asarray(xi)
1348+
nd = np.ndim(xi)
1349+
if nd > 1:
1350+
raise ValueError("{} must have 2 or fewer dimensions".format(name))
1351+
elif nd == 1 and len(xi) != 1:
1352+
is_1d = False
1353+
result.append(xi.reshape(-1))
1354+
1355+
if is_1d:
13301356
# 1D array of scalars: directly return it.
1331-
return [X]
1332-
elif X.ndim in [1, 2]:
1333-
# 2D array, or 1D array of iterables: flatten them first.
1334-
return [np.reshape(x, -1) for x in X]
1357+
return [np.reshape(result, -1)]
13351358
else:
1336-
raise ValueError("{} must have 2 or fewer dimensions".format(name))
1359+
# 2D array, or 1D array of iterables: use flattened version.
1360+
return result
13371361

13381362

13391363
def violin_stats(X, method, points=100, quantiles=None):

0 commit comments

Comments
 (0)