From 4435838654e60c63031d77629259f8503e8d09d6 Mon Sep 17 00:00:00 2001 From: Yi Wei Date: Sat, 1 Jul 2023 14:44:39 +0200 Subject: [PATCH] FIX: pcolor writing to read-only input mask --- lib/matplotlib/axes/_axes.py | 2 +- lib/matplotlib/tests/test_axes.py | 12 +++++++++--- 2 files changed, 10 insertions(+), 4 deletions(-) diff --git a/lib/matplotlib/axes/_axes.py b/lib/matplotlib/axes/_axes.py index cbfa5d644e2a..4eff4ec1b5d2 100644 --- a/lib/matplotlib/axes/_axes.py +++ b/lib/matplotlib/axes/_axes.py @@ -5773,7 +5773,7 @@ def _pcolorargs(self, funcname, *args, shading='auto', **kwargs): # unit conversion allows e.g. datetime objects as axis values X, Y = args[:2] X, Y = self._process_unit_info([("x", X), ("y", Y)], kwargs) - X, Y = [cbook.safe_masked_invalid(a) for a in [X, Y]] + X, Y = [cbook.safe_masked_invalid(a, copy=True) for a in [X, Y]] if funcname == 'pcolormesh': if np.ma.is_masked(X) or np.ma.is_masked(Y): diff --git a/lib/matplotlib/tests/test_axes.py b/lib/matplotlib/tests/test_axes.py index 1caf681f50bf..30c7e34c937b 100644 --- a/lib/matplotlib/tests/test_axes.py +++ b/lib/matplotlib/tests/test_axes.py @@ -1507,11 +1507,17 @@ def test_pcolorargs_with_read_only(): y = np.linspace(0, 1, 10) X, Y = np.meshgrid(x, y) Z = np.sin(2 * np.pi * X) * np.cos(2 * np.pi * Y) - Zmask = np.broadcast_to([True, False] * 5, Z.shape) - assert Zmask.flags.writeable is False - masked_Z = np.ma.array(Z, mask=Zmask) + mask = np.broadcast_to([True, False] * 5, Z.shape) + assert mask.flags.writeable is False + masked_Z = np.ma.array(Z, mask=mask) plt.pcolormesh(X, Y, masked_Z) + masked_X = np.ma.array(X, mask=mask) + masked_Y = np.ma.array(Y, mask=mask) + with pytest.warns(UserWarning, + match='are not monotonically increasing or decreasing'): + plt.pcolor(masked_X, masked_Y, masked_Z) + @check_figures_equal(extensions=["png"]) def test_pcolornearest(fig_test, fig_ref):