Skip to content

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

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Rylie-W opened this issue Jul 1, 2023 · 1 comment · Fixed by #26232
Closed

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

Rylie-W opened this issue Jul 1, 2023 · 1 comment · Fixed by #26232
Milestone

Comments

@Rylie-W
Copy link

Rylie-W commented Jul 1, 2023

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

@rcomer
Copy link
Member

rcomer commented Jul 1, 2023

Thanks @Rylie-W good spot! If you want to open a PR for that, that would be great.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants