Skip to content

Commit 708cb66

Browse files
oscargusmeeseeksmachine
authored andcommitted
Backport PR #26223: Fix: pcolormesh writing to read-only input mask
1 parent 4d199b0 commit 708cb66

File tree

2 files changed

+19
-2
lines changed

2 files changed

+19
-2
lines changed

lib/matplotlib/axes/_axes.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5699,7 +5699,7 @@ def _pcolorargs(self, funcname, *args, shading='auto', **kwargs):
56995699
else:
57005700
X, Y = np.meshgrid(np.arange(ncols + 1), np.arange(nrows + 1))
57015701
shading = 'flat'
5702-
C = cbook.safe_masked_invalid(C)
5702+
C = cbook.safe_masked_invalid(C, copy=True)
57035703
return X, Y, C, shading
57045704

57055705
if len(args) == 3:
@@ -5788,7 +5788,7 @@ def _interp_grid(X):
57885788
Y = _interp_grid(Y.T).T
57895789
shading = 'flat'
57905790

5791-
C = cbook.safe_masked_invalid(C)
5791+
C = cbook.safe_masked_invalid(C, copy=True)
57925792
return X, Y, C, shading
57935793

57945794
@_preprocess_data()

lib/matplotlib/tests/test_axes.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1418,6 +1418,23 @@ def test_pcolorargs():
14181418
ax.pcolormesh(X, Y, Z, shading='auto')
14191419

14201420

1421+
def test_pcolorargs_with_read_only():
1422+
x = np.arange(6).reshape(2, 3)
1423+
xmask = np.broadcast_to([False, True, False], x.shape) # read-only array
1424+
assert xmask.flags.writeable is False
1425+
masked_x = np.ma.array(x, mask=xmask)
1426+
plt.pcolormesh(masked_x)
1427+
1428+
x = np.linspace(0, 1, 10)
1429+
y = np.linspace(0, 1, 10)
1430+
X, Y = np.meshgrid(x, y)
1431+
Z = np.sin(2 * np.pi * X) * np.cos(2 * np.pi * Y)
1432+
Zmask = np.broadcast_to([True, False] * 5, Z.shape)
1433+
assert Zmask.flags.writeable is False
1434+
masked_Z = np.ma.array(Z, mask=Zmask)
1435+
plt.pcolormesh(X, Y, masked_Z)
1436+
1437+
14211438
@check_figures_equal(extensions=["png"])
14221439
def test_pcolornearest(fig_test, fig_ref):
14231440
ax = fig_test.subplots()

0 commit comments

Comments
 (0)