Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit ae723ba

Browse files
authored
Merge pull request #26223 from Rylie-W/copy-read-only-input-in-safe_masked_invalid
Fix: pcolormesh writing to read-only input mask
2 parents b71260a + 95d39d3 commit ae723ba

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
@@ -5764,7 +5764,7 @@ def _pcolorargs(self, funcname, *args, shading='auto', **kwargs):
57645764
else:
57655765
X, Y = np.meshgrid(np.arange(ncols + 1), np.arange(nrows + 1))
57665766
shading = 'flat'
5767-
C = cbook.safe_masked_invalid(C)
5767+
C = cbook.safe_masked_invalid(C, copy=True)
57685768
return X, Y, C, shading
57695769

57705770
if len(args) == 3:
@@ -5853,7 +5853,7 @@ def _interp_grid(X):
58535853
Y = _interp_grid(Y.T).T
58545854
shading = 'flat'
58555855

5856-
C = cbook.safe_masked_invalid(C)
5856+
C = cbook.safe_masked_invalid(C, copy=True)
58575857
return X, Y, C, shading
58585858

58595859
@_preprocess_data()

lib/matplotlib/tests/test_axes.py

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

14981498

1499+
def test_pcolorargs_with_read_only():
1500+
x = np.arange(6).reshape(2, 3)
1501+
xmask = np.broadcast_to([False, True, False], x.shape) # read-only array
1502+
assert xmask.flags.writeable is False
1503+
masked_x = np.ma.array(x, mask=xmask)
1504+
plt.pcolormesh(masked_x)
1505+
1506+
x = np.linspace(0, 1, 10)
1507+
y = np.linspace(0, 1, 10)
1508+
X, Y = np.meshgrid(x, y)
1509+
Z = np.sin(2 * np.pi * X) * np.cos(2 * np.pi * Y)
1510+
Zmask = np.broadcast_to([True, False] * 5, Z.shape)
1511+
assert Zmask.flags.writeable is False
1512+
masked_Z = np.ma.array(Z, mask=Zmask)
1513+
plt.pcolormesh(X, Y, masked_Z)
1514+
1515+
14991516
@check_figures_equal(extensions=["png"])
15001517
def test_pcolornearest(fig_test, fig_ref):
15011518
ax = fig_test.subplots()

0 commit comments

Comments
 (0)