From b3f0c28894f400d0345a8925c64875ec1dfcd049 Mon Sep 17 00:00:00 2001 From: Thomas A Caswell Date: Mon, 13 Nov 2017 11:23:05 -0800 Subject: [PATCH] Backport PR #9723: ENH: Catch masked array and invalid x, y to pcolormesh --- lib/matplotlib/axes/_axes.py | 17 +++++++++++++++-- lib/matplotlib/tests/test_axes.py | 6 ++++++ 2 files changed, 21 insertions(+), 2 deletions(-) diff --git a/lib/matplotlib/axes/_axes.py b/lib/matplotlib/axes/_axes.py index dc34e74ad2dd..483d16c61ff8 100644 --- a/lib/matplotlib/axes/_axes.py +++ b/lib/matplotlib/axes/_axes.py @@ -5172,7 +5172,21 @@ def _pcolorargs(funcname, *args, **kw): return X, Y, C if len(args) == 3: - X, Y, C = [np.asanyarray(a) for a in args] + # Check x and y for bad data... + C = np.asanyarray(args[2]) + X, Y = [cbook.safe_masked_invalid(a) for a in args[:2]] + if funcname == 'pcolormesh': + if np.ma.is_masked(X) or np.ma.is_masked(Y): + raise ValueError( + 'x and y arguments to pcolormesh cannot have ' + 'non-finite values or be of type ' + 'numpy.ma.core.MaskedArray with masked values') + # safe_masked_invalid() returns an ndarray for dtypes other + # than floating point. + if isinstance(X, np.ma.core.MaskedArray): + X = X.data # strip mask as downstream doesn't like it... + if isinstance(Y, np.ma.core.MaskedArray): + Y = Y.data numRows, numCols = C.shape else: raise TypeError( @@ -5567,7 +5581,6 @@ def pcolormesh(self, *args, **kwargs): # convert to one dimensional arrays C = C.ravel() coords = np.column_stack((X, Y)).astype(float, copy=False) - collection = mcoll.QuadMesh(Nx - 1, Ny - 1, coords, antialiased=antialiased, shading=shading, **kwargs) diff --git a/lib/matplotlib/tests/test_axes.py b/lib/matplotlib/tests/test_axes.py index 7e6cee08127e..99031307eb93 100644 --- a/lib/matplotlib/tests/test_axes.py +++ b/lib/matplotlib/tests/test_axes.py @@ -1150,6 +1150,12 @@ def test_pcolorargs(): ax.pcolormesh(x, y, Z[:-1, :-1], shading="gouraud") with pytest.raises(TypeError): ax.pcolormesh(X, Y, Z[:-1, :-1], shading="gouraud") + x[0] = np.NaN + with pytest.raises(ValueError): + ax.pcolormesh(x, y, Z[:-1, :-1]) + x = np.ma.array(x, mask=(x < 0)) + with pytest.raises(ValueError): + ax.pcolormesh(x, y, Z[:-1, :-1]) @image_comparison(baseline_images=['canonical'])