Skip to content

Commit fd8b6e0

Browse files
jklymaktacaswell
authored andcommitted
Merge pull request matplotlib#17289 from QuLogic/np119
Prepare for ragged array warnings in NumPy 1.19 Conflicts: lib/matplotlib/axes/_axes.py - implicitly backported changes to wording in error messages
1 parent 889c73f commit fd8b6e0

File tree

4 files changed

+62
-24
lines changed

4 files changed

+62
-24
lines changed

lib/matplotlib/axes/_axes.py

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3704,9 +3704,9 @@ def boxplot(self, x, notch=None, sym=None, vert=None, whis=None,
37043704
stats['med'] = med
37053705

37063706
if conf_intervals is not None:
3707-
if np.shape(conf_intervals)[0] != len(bxpstats):
3708-
err_mess = 'conf_intervals length not compatible with x'
3709-
raise ValueError(err_mess)
3707+
if len(conf_intervals) != len(bxpstats):
3708+
raise ValueError(
3709+
"'conf_intervals' and 'x' have different lengths")
37103710
else:
37113711
for stats, ci in zip(bxpstats, conf_intervals):
37123712
if ci is not None:
@@ -6574,8 +6574,6 @@ def hist(self, x, bins=None, range=None, density=False, weights=None,
65746574
if histtype == 'barstacked' and not stacked:
65756575
stacked = True
65766576

6577-
# basic input validation
6578-
input_empty = np.size(x) == 0
65796577
# Massage 'x' for processing.
65806578
x = cbook._reshape_2D(x, 'x')
65816579
nx = len(x) # number of datasets
@@ -6600,10 +6598,13 @@ def hist(self, x, bins=None, range=None, density=False, weights=None,
66006598
if len(w) != nx:
66016599
raise ValueError('weights should have the same shape as x')
66026600

6601+
input_empty = True
66036602
for xi, wi in zip(x, w):
6604-
if wi is not None and len(wi) != len(xi):
6605-
raise ValueError(
6606-
'weights should have the same shape as x')
6603+
len_xi = len(xi)
6604+
if wi is not None and len(wi) != len_xi:
6605+
raise ValueError('weights should have the same shape as x')
6606+
if len_xi:
6607+
input_empty = False
66076608

66086609
if color is None:
66096610
color = [self._get_lines.get_next_color() for i in range(nx)]

lib/matplotlib/cbook/__init__.py

Lines changed: 50 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1032,7 +1032,12 @@ def _combine_masks(*args):
10321032
else:
10331033
if isinstance(x, np.ma.MaskedArray) and x.ndim > 1:
10341034
raise ValueError("Masked arrays must be 1-D")
1035-
x = np.asanyarray(x)
1035+
try:
1036+
x = np.asanyarray(x)
1037+
except (np.VisibleDeprecationWarning, ValueError):
1038+
# NumPy 1.19 raises a warning about ragged arrays, but we want
1039+
# to accept basically anything here.
1040+
x = np.asanyarray(x, dtype=object)
10361041
if x.ndim == 1:
10371042
x = safe_masked_invalid(x)
10381043
seqlist[i] = True
@@ -1366,24 +1371,48 @@ def _reshape_2D(X, name):
13661371
Use Fortran ordering to convert ndarrays and lists of iterables to lists of
13671372
1D arrays.
13681373
1369-
Lists of iterables are converted by applying `np.asarray` to each of their
1370-
elements. 1D ndarrays are returned in a singleton list containing them.
1371-
2D ndarrays are converted to the list of their *columns*.
1374+
Lists of iterables are converted by applying `np.asanyarray` to each of
1375+
their elements. 1D ndarrays are returned in a singleton list containing
1376+
them. 2D ndarrays are converted to the list of their *columns*.
13721377
13731378
*name* is used to generate the error message for invalid inputs.
13741379
"""
1375-
# Iterate over columns for ndarrays, over rows otherwise.
1376-
X = np.atleast_1d(X.T if isinstance(X, np.ndarray) else np.asarray(X))
1380+
# Iterate over columns for ndarrays.
1381+
if isinstance(X, np.ndarray):
1382+
X = X.T
1383+
1384+
if len(X) == 0:
1385+
return [[]]
1386+
elif X.ndim == 1 and np.ndim(X[0]) == 0:
1387+
# 1D array of scalars: directly return it.
1388+
return [X]
1389+
elif X.ndim in [1, 2]:
1390+
# 2D array, or 1D array of iterables: flatten them first.
1391+
return [np.reshape(x, -1) for x in X]
1392+
else:
1393+
raise ValueError(f'{name} must have 2 or fewer dimensions')
1394+
1395+
# Iterate over list of iterables.
13771396
if len(X) == 0:
13781397
return [[]]
1379-
elif X.ndim == 1 and np.ndim(X[0]) == 0:
1398+
1399+
result = []
1400+
is_1d = True
1401+
for xi in X:
1402+
xi = np.asanyarray(xi)
1403+
nd = np.ndim(xi)
1404+
if nd > 1:
1405+
raise ValueError(f'{name} must have 2 or fewer dimensions')
1406+
elif nd == 1 and len(xi) != 1:
1407+
is_1d = False
1408+
result.append(xi.reshape(-1))
1409+
1410+
if is_1d:
13801411
# 1D array of scalars: directly return it.
1381-
return [X]
1382-
elif X.ndim in [1, 2]:
1383-
# 2D array, or 1D array of iterables: flatten them first.
1384-
return [np.reshape(x, -1) for x in X]
1412+
return [np.reshape(result, -1)]
13851413
else:
1386-
raise ValueError("{} must have 2 or fewer dimensions".format(name))
1414+
# 2D array, or 1D array of iterables: use flattened version.
1415+
return result
13871416

13881417

13891418
def violin_stats(X, method, points=100, quantiles=None):
@@ -1448,10 +1477,10 @@ def violin_stats(X, method, points=100, quantiles=None):
14481477
quantiles = _reshape_2D(quantiles, "quantiles")
14491478
# Else, mock quantiles if is none or empty
14501479
else:
1451-
quantiles = [[]] * np.shape(X)[0]
1480+
quantiles = [[]] * len(X)
14521481

14531482
# quantiles should has the same size as dataset
1454-
if np.shape(X)[:1] != np.shape(quantiles)[:1]:
1483+
if len(X) != len(quantiles):
14551484
raise ValueError("List of violinplot statistics and quantiles values"
14561485
" must have the same length")
14571486

@@ -1626,8 +1655,15 @@ def index_of(y):
16261655
try:
16271656
return y.index.values, y.values
16281657
except AttributeError:
1658+
pass
1659+
try:
16291660
y = _check_1d(y)
1661+
except (np.VisibleDeprecationWarning, ValueError):
1662+
# NumPy 1.19 will warn on ragged input, and we can't actually use it.
1663+
pass
1664+
else:
16301665
return np.arange(y.shape[0], dtype=float), y
1666+
raise ValueError('Input could not be cast to an at-least-1D NumPy array')
16311667

16321668

16331669
def safe_first_element(obj):

lib/matplotlib/tests/test_backend_svg.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -189,7 +189,8 @@ def include(gid, obj):
189189
elif obj.axes is None:
190190
return False
191191
if isinstance(obj, plt.Line2D):
192-
if np.array(obj.get_data()).shape == (2, 1):
192+
xdata, ydata = obj.get_data()
193+
if len(xdata) == len(ydata) == 1:
193194
return False
194195
elif not hasattr(obj, "axes") or obj.axes is None:
195196
return False

lib/mpl_toolkits/mplot3d/art3d.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -301,7 +301,7 @@ def set_segments(self, segments):
301301
"""
302302
Set 3D segments.
303303
"""
304-
self._segments3d = np.asanyarray(segments)
304+
self._segments3d = segments
305305
LineCollection.set_segments(self, [])
306306

307307
def do_3d_projection(self, renderer):

0 commit comments

Comments
 (0)