8000 Merge pull request #9723 from jklymak/fixpcolorcopy · matplotlib/matplotlib@1df45a4 · GitHub
[go: up one dir, main page]

Skip to content

Commit 1df45a4

Browse files
authored
Merge pull request #9723 from jklymak/fixpcolorcopy
ENH: Catch masked array and invalid x, y to pcolormesh
2 parents eedb1b0 + 44d455a commit 1df45a4

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
@@ -5182,7 +5182,21 @@ def _pcolorargs(funcname, *args, **kw):
51825182
return X, Y, C
51835183

51845184
if len(args) == 3:
5185-
X, Y, C = [np.asanyarray(a) for a in args]
5185+
# Check x and y for bad data...
5186+
C = np.asanyarray(args[2])
5187+
X, Y = [cbook.safe_masked_invalid(a) for a in args[:2]]
5188+
if funcname == 'pcolormesh':
5189+
if np.ma.is_masked(X) or np.ma.is_masked(Y):
5190+
raise ValueError(
5191+
'x and y arguments to pcolormesh cannot have '
5192+
'non-finite values or be of type '
5193+
'numpy.ma.core.MaskedArray with masked values')
5194+
# safe_masked_invalid() returns an ndarray for dtypes other
5195+
# than floating point.
5196+
if isinstance(X, np.ma.core.MaskedArray):
5197+
X = X.data # strip mask as downstream doesn't like it...
5198+
if isinstance(Y, np.ma.core.MaskedArray):
5199+
Y = Y.data
51865200
numRows, numCols = C.shape
51875201
else:
51885202
raise TypeError(
@@ -5577,7 +5591,6 @@ def pcolormesh(self, *args, **kwargs):
55775591
# convert to one dimensional arrays
55785592
C = C.ravel()
55795593
coords = np.column_stack((X, Y)).astype(float, copy=False)
5580-
55815594
collection = mcoll.QuadMesh(Nx - 1, Ny - 1, coords,
55825595
antialiased=antialiased, shading=shading,
55835596
**kwargs)

lib/matplotlib/tests/test_axes.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1190,6 +1190,12 @@ def test_pcolorargs():
11901190
ax.pcolormesh(x, y, Z[:-1, :-1], shading="gouraud")
11911191
with pytest.raises(TypeError):
11921192
ax.pcolormesh(X, Y, Z[:-1, :-1], shading="gouraud")
1193+
x[0] = np.NaN
1194+
with pytest.raises(ValueError):
1195+
ax.pcolormesh(x, y, Z[:-1, :-1])
1196+
x = np.ma.array(x, mask=(x < 0))
1197+
with pytest.raises(ValueError):
1198+
ax.pcolormesh(x, y, Z[:-1, :-1])
11931199

11941200

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

0 commit comments

Comments
 (0)
0