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

Skip to content

Commit 04bc071

Browse files
authored
Merge pull request #9769 from matplotlib/auto-backport-of-pr-9723
Backport PR #9723 on branch v2.1.x
2 parents 080d0f8 + b3f0c28 commit 04bc071

File tree

2 files changed

+21
-2
lines changed

2 files changed

+21
-2
lines changed

lib/matplotlib/axes/_axes.py

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5172,7 +5172,21 @@ def _pcolorargs(funcname, *args, **kw):
51725172
return X, Y, C
51735173

51745174
if len(args) == 3:
5175-
X, Y, C = [np.asanyarray(a) for a in args]
5175+
# Check x and y for bad data...
5176+
C = np.asanyarray(args[2])
5177+
X, Y = [cbook.safe_masked_invalid(a) for a in args[:2]]
5178+
if funcname == 'pcolormesh':
5179+
if np.ma.is_masked(X) or np.ma.is_masked(Y):
5180+
raise ValueError(
5181+
'x and y arguments to pcolormesh cannot have '
5182+
'non-finite values or be of type '
5183+
'numpy.ma.core.MaskedArray with masked values')
5184+
# safe_masked_invalid() returns an ndarray for dtypes other
5185+
# than floating point.
5186+
if isinstance(X, np.ma.core.MaskedArray):
5187+
X = X.data # strip mask as downstream doesn't like it...
5188+
if isinstance(Y, np.ma.core.MaskedArray):
5189+
Y = Y.data
51765190
numRows, numCols = C.shape
51775191
else:
51785192
raise TypeError(
@@ -5567,7 +5581,6 @@ def pcolormesh(self, *args, **kwargs):
55675581
# convert to one dimensional arrays
55685582
C = C.ravel()
55695583
coords = np.column_stack((X, Y)).astype(float, copy=False)
5570-
55715584
collection = mcoll.QuadMesh(Nx - 1, Ny - 1, coords,
55725585
antialiased=antialiased, shading=shading,
55735586
**kwargs)

lib/matplotlib/tests/test_axes.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1150,6 +1150,12 @@ def test_pcolorargs():
11501150
ax.pcolormesh(x, y, Z[:-1, :-1], shading="gouraud")
11511151
with pytest.raises(TypeError):
11521152
ax.pcolormesh(X, Y, Z[:-1, :-1], shading="gouraud")
1153+
x[0] = np.NaN
1154+
with pytest.raises(ValueError):
1155+
ax.pcolormesh(x, y, Z[:-1, :-1])
1156+
x = np.ma.array(x, mask=(x < 0))
1157+
with pytest.raises(ValueError):
1158+
ax.pcolormesh(x, y, Z[:-1, :-1])
11531159

11541160

11551161
@image_comparison(baseline_images=['canonical'])

0 commit comments

Comments
 (0)