8000 ENH: Catch masked array and invalid x, y to pcolor/mesh · matplotlib/matplotlib@4733921 · GitHub
[go: up one dir, main page]

Skip to content

Commit 4733921

Browse files
committed
ENH: Catch masked array and invalid x, y to pcolor/mesh
1 parent df1af2f commit 4733921

File tree

2 files changed

+22
-1
lines changed

2 files changed

+22
-1
lines changed

lib/matplotlib/axes/_axes.py

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5182,7 +5182,22 @@ def _pcolorargs(funcname, *args, **kw):
51825182
return X, Y, C
51835183

51845184
if len(args) == 3:
5185+
# Check x and y for bad data...
51855186
X, Y, C = [np.asanyarray(a) for a in args]
5187+
for a in X, Y:
5188+
a = cbook.safe_masked_invalid(a)
5189+
if np.ma.is_masked(a):
5190+
raise TypeError(
5191+
'x and y arguments to %s cannot have '
5192+
'non-finite values and cannot be type '
5193+
'numpy.ma.core.MaskedArray with masked values'
5194+
% funcname)
5195+
# if it didn't fail the above it is masked but has no
5196+
# masked values, so strip the mask:
5197+
if type(X) is np.ma.core.MaskedArray:
5198+
X = X.data
5199+
if type(Y) is np.ma.core.MaskedArray:
5200+
Y = Y.data
51865201
numRows, numCols = C.shape
51875202
else:
51885203
raise TypeError(
@@ -5577,7 +5592,6 @@ def pcolormesh(self, *args, **kwargs):
55775592
# convert to one dimensional arrays
55785593
C = C.ravel()
55795594
coords = np.column_stack((X, Y)).astype(float, copy=False)
5580-
55815595
collection = mcoll.QuadMesh(Nx - 1, Ny - 1, coords,
55825596
antialiased=antialiased, shading=shading,
55835597
**kwargs)

lib/matplotlib/tests/test_axes.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1190,6 +1190,13 @@ 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+
with pytest.raises(TypeError):
1194+
xx = x
1195+
xx[0] = np.NaN
1196+
ax.pcolormesh(xx, y, Z[:-1, :-1])
1197+
with pytest.raises(TypeError):
1198+
xx = np.ma.masked(x, mask=(x < 0))
1199+
ax.pcolormesh(xx, y, Z[:-1, :-1])
11931200

11941201

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

0 commit comments

Comments
 (0)
0