diff --git a/lib/matplotlib/axes/_axes.py b/lib/matplotlib/axes/_axes.py index 58017b66ff3b..8848ef233824 100644 --- a/lib/matplotlib/axes/_axes.py +++ b/lib/matplotlib/axes/_axes.py @@ -5699,7 +5699,7 @@ def _pcolorargs(self, funcname, *args, shading='auto', **kwargs): else: X, Y = np.meshgrid(np.arange(ncols + 1), np.arange(nrows + 1)) shading = 'flat' - C = cbook.safe_masked_invalid(C) + C = cbook.safe_masked_invalid(C, copy=True) return X, Y, C, shading if len(args) == 3: @@ -5788,7 +5788,7 @@ def _interp_grid(X): Y = _interp_grid(Y.T).T shading = 'flat' - C = cbook.safe_masked_invalid(C) + C = cbook.safe_masked_invalid(C, copy=True) return X, Y, C, shading @_preprocess_data() diff --git a/lib/matplotlib/tests/test_axes.py b/lib/matplotlib/tests/test_axes.py index 44fbbb02b6a7..dcc88f48788e 100644 --- a/lib/matplotlib/tests/test_axes.py +++ b/lib/matplotlib/tests/test_axes.py @@ -1418,6 +1418,23 @@ def test_pcolorargs(): ax.pcolormesh(X, Y, Z, shading='auto') +def test_pcolorargs_with_read_only(): + x = np.arange(6).reshape(2, 3) + xmask = np.broadcast_to([False, True, False], x.shape) # read-only array + assert xmask.flags.writeable is False + masked_x = np.ma.array(x, mask=xmask) + plt.pcolormesh(masked_x) + + x = np.linspace(0, 1, 10) + 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) + plt.pcolormesh(X, Y, masked_Z) + + @check_figures_equal(extensions=["png"]) def test_pcolornearest(fig_test, fig_ref): ax = fig_test.subplots()