Skip to content

Commit ac018af

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 2e57d5b commit ac018af

File tree

1 file changed

+35
-11
lines changed

1 file changed

+35
-11
lines changed

lib/matplotlib/cbook/__init__.py

Lines changed: 35 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1316,24 +1316,48 @@ def _reshape_2D(X, name):
13161316
Use Fortran ordering to convert ndarrays and lists of iterables to lists of
13171317
1D arrays.
13181318
1319-
Lists of iterables are converted by applying `np.asarray` to each of their
1320-
elements. 1D ndarrays are returned in a singleton list containing them.
1321-
2D ndarrays are converted to the list of their *columns*.
1319+
Lists of iterables are converted by applying `np.asanyarray` to each of
1320+
their elements. 1D ndarrays are returned in a singleton list containing
1321+
them. 2D ndarrays are converted to the list of their *columns*.
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(f'{name} must have 2 or fewer dimensions')
1339+
1340+
# Iterate over list of iterables.
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.asanyarray(xi)
1348+
nd = np.ndim(xi)
1349+
if nd > 1:
1350+
raise ValueError(f'{name} must have 2 or fewer dimensions')
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)