Skip to content

[Bug]: pcolor writing to read-only input mask #26230

Closed
@Rylie-W

Description

@Rylie-W

Bug summary

When the parameter X or Y is a masked array with a read-only mask, pcolor fails with ValueError: array is read-only

Code for reproduction

import matplotlib.pyplot as plt
import numpy as np

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)

mask = np.broadcast_to([True, False] * 5, Z.shape)
masked_X = np.ma.array(X, mask=mask)
masked_Y = np.ma.array(Y, mask=mask)
masked_Z = np.ma.array(Z, mask=mask)

plt.pcolormesh(masked_X, masked_Y, masked_Z)

Actual outcome

Traceback (most recent call last):
File "", line 1, in
File "/Library/Python/3.9/lib/python/site-packages/matplotlib/pyplot.py", line 2773, in pcolormesh
__ret = gca().pcolormesh(
File "/Library/Python/3.9/lib/python/site-packages/matplotlib/init.py", line 1442, in inner
return func(ax, *map(sanitize_sequence, args), **kwargs)
File "/Library/Python/3.9/lib/python/site-packages/matplotlib/axes/_axes.py", line 6220, in pcolormesh
X, Y, C, shading = self._pcolorargs('pcolormesh', *args,
File "/Library/Python/3.9/lib/python/site-packages/matplotlib/axes/_axes.py", line 5713, in _pcolorargs
X, Y = [cbook.safe_masked_invalid(a) for a in [X, Y]]
File "/Library/Python/3.9/lib/python/site-packages/matplotlib/axes/_axes.py", line 5713, in
X, Y = [cbook.safe_masked_invalid(a) for a in [X, Y]]
File "/Library/Python/3.9/lib/python/site-packages/matplotlib/cbook/init.py", line 715, in safe_masked_invalid
xm = np.ma.masked_invalid(x, copy=False)
File "/Library/Python/3.9/lib/python/site-packages/numpy/ma/core.py", line 2360, in masked_invalid
res = masked_where(~(np.isfinite(a)), a, copy=copy)
File "/Library/Python/3.9/lib/python/site-packages/numpy/ma/core.py", line 1942, in masked_where
result.mask = _shrink_mask(cond)
File "/Library/Python/3.9/lib/python/site-packages/numpy/ma/core.py", line 3516, in mask
self.setmask(value)
File "/Library/Python/3.9/lib/python/site-packages/numpy/ma/core.py", line 3462, in setmask
current_mask.flat = mask
ValueError: array is read-only

Expected outcome

No error

Additional information

The error still exists because I missed the following code in fixing #26093:

X, Y = [cbook.safe_masked_invalid(a) for a in [X, Y]]
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')

And I will fix it as well if allowed.

Operating system

OS/X

Matplotlib Version

3.7.1

Matplotlib Backend

MacOSX

Python version

3.9

Jupyter version

No response

Installation

None

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions