Title: [Bug]: pcolormesh writing to input mask · Issue #26093 · matplotlib/matplotlib · GitHub
Open Graph Title: [Bug]: pcolormesh writing to input mask · Issue #26093 · matplotlib/matplotlib
X Title: [Bug]: pcolormesh writing to input mask · Issue #26093 · matplotlib/matplotlib
Description: Bug summary When pcolormesh receives a masked array, it seems to be writing back to the mask. Since numpy 1.24 this now causes pcolormesh to fail if the mask is read-only. Code for reproduction import matplotlib.pyplot as plt import nump...
Open Graph Description: Bug summary When pcolormesh receives a masked array, it seems to be writing back to the mask. Since numpy 1.24 this now causes pcolormesh to fail if the mask is read-only. Code for reproduction imp...
X Description: Bug summary When pcolormesh receives a masked array, it seems to be writing back to the mask. Since numpy 1.24 this now causes pcolormesh to fail if the mask is read-only. Code for reproduction imp...
Opengraph URL: https://github.com/matplotlib/matplotlib/issues/26093
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"[Bug]: pcolormesh writing to input mask","articleBody":"### Bug summary\n\nWhen `pcolormesh` receives a masked array, it seems to be writing back to the mask. Since numpy 1.24 this now causes `pcolormesh` to fail if the mask is read-only.\n\n### Code for reproduction\n\n```python\nimport matplotlib.pyplot as plt\r\nimport numpy as np\r\n\r\ndata = np.arange(6).reshape(2, 3)\r\nmask = np.broadcast_to([False, True, False], data.shape) # read-only array\r\n\r\nmasked_data = np.ma.array(data, mask=mask)\r\n\r\nplt.pcolormesh(masked_data)\n```\n\n\n### Actual outcome\n\n```\r\nTraceback (most recent call last):\r\n File \"pcolormesh_read_only_mask.py\", line 9, in \u003cmodule\u003e\r\n plt.pcolormesh(masked_data)\r\n File \"[conda-env-path]/lib/python3.11/site-packages/matplotlib/pyplot.py\", line 2773, in pcolormesh\r\n __ret = gca().pcolormesh(\r\n ^^^^^^^^^^^^^^^^^\r\n File \"[conda-env-path]/lib/python3.11/site-packages/matplotlib/__init__.py\", line 1442, in inner\r\n return func(ax, *map(sanitize_sequence, args), **kwargs)\r\n ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^\r\n File \"[conda-env-path]/lib/python3.11/site-packages/matplotlib/axes/_axes.py\", line 6220, in pcolormesh\r\n X, Y, C, shading = self._pcolorargs('pcolormesh', *args,\r\n ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^\r\n File \"[conda-env-path]/lib/python3.11/site-packages/matplotlib/axes/_axes.py\", line 5704, in _pcolorargs\r\n C = cbook.safe_masked_invalid(C)\r\n ^^^^^^^^^^^^^^^^^^^^^^^^^^^^\r\n File \"[conda-env-path]/lib/python3.11/site-packages/matplotlib/cbook/__init__.py\", line 715, in safe_masked_invalid\r\n xm = np.ma.masked_invalid(x, copy=False)\r\n ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^\r\n File \"[conda-env-path]/lib/python3.11/site-packages/numpy/ma/core.py\", line 2360, in masked_invalid\r\n res = masked_where(~(np.isfinite(a)), a, copy=copy)\r\n ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^\r\n File \"[conda-env-path]/lib/python3.11/site-packages/numpy/ma/core.py\", line 1942, in masked_where\r\n result.mask = _shrink_mask(cond)\r\n ^^^^^^^^^^^\r\n File \"[conda-env-path]/lib/python3.11/site-packages/numpy/ma/core.py\", line 3516, in mask\r\n self.__setmask__(value)\r\n File \"[conda-env-path]/lib/python3.11/site-packages/numpy/ma/core.py\", line 3462, in __setmask__\r\n current_mask.flat = mask\r\n ^^^^^^^^^^^^^^^^^\r\nValueError: array is read-only\r\n```\n\n### Expected outcome\n\nNo error\n\n### Additional information\n\nThe code above runs fine with numpy v1.23, although the output from `broadcast_to` was already read-only at that version. From numpy release notes, this looks like the likely reason for the change:\r\nhttps://numpy.org/doc/stable/release/1.24.0-notes.html#masked-invalid-now-modifies-the-mask-in-place\r\n\r\nAside from the new error, if a user passes a masked array that has nans or infs at the unmasked points, we are modifying their input array with the call to `masked_invalid`.\r\n\r\nI guess we just need to take a copy somewhere?\n\n### Operating system\n\nRHEL7\n\n### Matplotlib Version\n\n3.7.1\n\n### Matplotlib Backend\n\nQtAgg\n\n### Python version\n\n3.11.3\n\n### Jupyter version\n\nN/A\n\n### Installation\n\nconda","author":{"url":"https://github.com/rcomer","@type":"Person","name":"rcomer"},"datePublished":"2023-06-08T09:28:32.000Z","interactionStatistic":{"@type":"InteractionCounter","interactionType":"https://schema.org/CommentAction","userInteractionCount":2},"url":"https://github.com/26093/matplotlib/issues/26093"}
| route-pattern | /_view_fragments/issues/show/:user_id/:repository/:id/issue_layout(.:format) |
| route-controller | voltron_issues_fragments |
| route-action | issue_layout |
| fetch-nonce | v2:ab19599a-82a9-d4ef-8701-b16209988f53 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | CCD8:272982:1B235B2:24A0A2E:6A536E3A |
| html-safe-nonce | cfcd20a56e0ba16a00b41d66433b2a49d4d451ea7098e929cb73062f0cb006e5 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJDQ0Q4OjI3Mjk4MjoxQjIzNUIyOjI0QTBBMkU6NkE1MzZFM0EiLCJ2aXNpdG9yX2lkIjoiNzg2NjY0NDQ3NDU5MTY2OTgxOCIsInJlZ2lvbl9lZGdlIjoiaWFkIiwicmVnaW9uX3JlbmRlciI6ImlhZCJ9 |
| visitor-hmac | bb2d7bdff99b650b3d60dd0c4a85048695aceef67723ef25ad62f7460096b5c8 |
| hovercard-subject-tag | issue:1747475998 |
| github-keyboard-shortcuts | repository,issues,copilot |
| google-site-verification | Apib7-x98H0j5cPqHWwSMm6dNU4GmODRoqxLiDzdx9I |
| octolytics-url | https://collector.github.com/github/collect |
| analytics-location | / |
| fb:app_id | 1401488693436528 |
| apple-itunes-app | app-id=1477376905, app-argument=https://github.com/_view_fragments/issues/show/matplotlib/matplotlib/26093/issue_layout |
| twitter:image | https://opengraph.githubassets.com/ae46aaf0c461e1275d573b557d718c1b4a0b68cf64bb6c9b644319836cf9f39c/matplotlib/matplotlib/issues/26093 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/ae46aaf0c461e1275d573b557d718c1b4a0b68cf64bb6c9b644319836cf9f39c/matplotlib/matplotlib/issues/26093 |
| og:image:alt | Bug summary When pcolormesh receives a masked array, it seems to be writing back to the mask. Since numpy 1.24 this now causes pcolormesh to fail if the mask is read-only. Code for reproduction imp... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | rcomer |
| hostname | github.com |
| expected-hostname | github.com |
| None | b9a586c06a05a7a86fc7e3f4dbd03e42f6869085879aa184aa6369456dbd50fb |
| turbo-cache-control | no-preview |
| go-import | github.com/matplotlib/matplotlib git https://github.com/matplotlib/matplotlib.git |
| octolytics-dimension-user_id | 215947 |
| octolytics-dimension-user_login | matplotlib |
| octolytics-dimension-repository_id | 1385122 |
| octolytics-dimension-repository_nwo | matplotlib/matplotlib |
| octolytics-dimension-repository_public | true |
| octolytics-dimension-repository_is_fork | false |
| octolytics-dimension-repository_network_root_id | 1385122 |
| octolytics-dimension-repository_network_root_nwo | matplotlib/matplotlib |
| turbo-body-classes | logged-out env-production page-responsive |
| disable-turbo | false |
| browser-stats-url | https://api.github.com/_private/browser/stats |
| browser-errors-url | https://api.github.com/_private/browser/errors |
| release | 07a982c1d40157c619b364352b704c3ce66bb332 |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
Links:
Viewport: width=device-width