Title: matplotlib.collections.QuadMesh.set_array() input arg format is weird and undocumented · Issue #15388 · matplotlib/matplotlib · GitHub
Open Graph Title: matplotlib.collections.QuadMesh.set_array() input arg format is weird and undocumented · Issue #15388 · matplotlib/matplotlib
X Title: matplotlib.collections.QuadMesh.set_array() input arg format is weird and undocumented · Issue #15388 · matplotlib/matplotlib
Description: Bug report Bug summary Trying to give matplotlib.collections.QuadMesh.set_array() the same argument as was used by matplotlib.axes.Axes.pcolormesh() results in an error. Moreover, the argument format depends on the shading; an argument t...
Open Graph Description: Bug report Bug summary Trying to give matplotlib.collections.QuadMesh.set_array() the same argument as was used by matplotlib.axes.Axes.pcolormesh() results in an error. Moreover, the argument form...
X Description: Bug report Bug summary Trying to give matplotlib.collections.QuadMesh.set_array() the same argument as was used by matplotlib.axes.Axes.pcolormesh() results in an error. Moreover, the argument form...
Opengraph URL: https://github.com/matplotlib/matplotlib/issues/15388
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"matplotlib.collections.QuadMesh.set_array() input arg format is weird and undocumented","articleBody":"\u003c!--To help us understand and resolve your issue, please fill out the form to the best of your ability.--\u003e\r\n\u003c!--You can feel free to delete the sections that do not apply.--\u003e\r\n\r\n### Bug report\r\n\r\n**Bug summary**\r\n\r\nTrying to give `matplotlib.collections.QuadMesh.set_array()` the same argument as was used by `matplotlib.axes.Axes.pcolormesh()` results in an error. Moreover, the argument format depends on the shading; an argument that is correct for `shading='gouraud'` will be accepted and produce spurious graphics for `shading='flat'`. In my opinion, the user should be able to use the same argument format for the update (via `set_array` or another method if that causes backwards compatibility issues) than used for creation (via `pcolormesh`), and not have to care about the shading when setting the data.\r\n\r\nIt was raised in [a StackOverflow post from 2013](https://stackoverflow.com/questions/18797175/animation-with-pcolormesh-routine-in-matplotlib-how-do-i-initialize-the-data) and the top answer (from 2015) gives workarounds, but I did not find anything in the issue tracker (here). It should at least be documented, [the current doc](https://matplotlib.org/3.1.1/api/collections_api.html?highlight=collections%20quadmesh%20set_array#matplotlib.collections.QuadMesh.set_array) only says \"Set the image array from numpy array A\".\r\n\r\n\u003c!--A short 1-2 sentences that succinctly describes the bug--\u003e\r\n\r\n**Code for reproduction**\r\n\r\n\u003c!--A minimum code snippet required to reproduce the bug.\r\nPlease make sure to minimize the number of dependencies required, and provide\r\nany necessary plotted data.\r\nAvoid using threads, as Matplotlib is (explicitly) not thread-safe.--\u003e\r\n\r\n```python\r\nimport numpy as np\r\nimport matplotlib.pyplot as plt\r\n\r\n\r\ndef f(X, Y, t=0):\r\n return np.sin(X+t*np.pi)*np.sin(Y)\r\n\r\nxmin, xmax = 0.0, 4*np.pi\r\nymin, ymax = 0.0, 5*np.pi\r\nNx, Ny = 50, 40\r\nx, y = np.linspace(xmin, xmax, num=Nx), np.linspace(ymin, ymax, num=Ny)\r\nX, Y = np.meshgrid(x, y, indexing='ij')\r\nzstart = f(X, Y)\r\n\r\nrun_number = np.random.rand()\r\n\r\n# Intialize figure stuff\r\nfig = plt.figure()\r\naxs = [\r\n plt.subplot(221, title='control flat'),\r\n plt.subplot(222, title='test flat'),\r\n plt.subplot(223, title='control gouraud'),\r\n plt.subplot(224, title='test gouraud'),\r\n ]\r\n\r\ndef drawcmesh(X, Y, z, ax, shading):\r\n ax.set_xlim(left=xmin, right=xmax)\r\n ax.set_ylim(bottom=ymin, top=ymax)\r\n cmesh = ax.pcolormesh(X, Y, z,\r\n shading=shading, vmin=-1.5, vmax=1.5, cmap='hot')\r\n return cmesh\r\n\r\n\r\n\r\nzstart = f(X, Y)\r\nperm_mesh = [\r\n drawcmesh(X, Y, zstart, axs[1], shading='flat'),\r\n drawcmesh(X, Y, zstart, axs[3], shading='gouraud')\r\n ]\r\ndrawcmesh(X, Y, zstart, axs[0], shading='flat')\r\ndrawcmesh(X, Y, zstart, axs[2], shading='gouraud')\r\n\r\ndef update(t):\r\n z = f(X, Y, t)\r\n\r\n for i in range(4):\r\n ax = axs[i]\r\n if i\u003c2:\r\n shading='flat'\r\n else:\r\n shading='gouraud'\r\n\r\n if i%2 == 0:\r\n # Control case: redraw by hand\r\n axs[i].clear()\r\n drawcmesh(X, Y, z, axs[i], shading=shading)\r\n axs[i].set_title('control {}'.format(shading))\r\n\r\n if i==1:\r\n # Flat shading\r\n\r\n # # The following would copy the syntax of plt.pcolormesh() but it\r\n # # results in an error\r\n # perm_mesh[0].set_array(z)\r\n\r\n # The following does not error out but the plot is later incorrect\r\n perm_mesh[0].set_array(z.ravel())\r\n\r\n # # The following works\r\n # perm_mesh[0].set_array(z[:-1, :-1].ravel())\r\n\r\n if i==3:\r\n # Gouraud shading\r\n\r\n # # The following would copy the syntax of plt.pcolormesh() but it\r\n # # results in an error\r\n # perm_mesh[1].set_array(z)\r\n\r\n # The following works\r\n perm_mesh[1].set_array(z.ravel())\r\n\r\n # # The following results in an error at draw time:\r\n\r\n \r\n # perm_mesh[1].set_array(z[:-1, :-1].ravel())\r\n\r\n# # Uncomment this to check that the initial drawing for the test case is correct\r\n# plt.show(block=True)\r\n\r\nplt.show(block=False)\r\nfor t in np.linspace(0.0, 4.0, num=50):\r\n update(t)\r\n fig.canvas.draw()\r\n fig.canvas.flush_events()\r\n```\r\n\r\n**Results**\r\nCf commented code under `# Flat shading` and `# Gouraud shading` in the source for the various possibilities.\r\n\r\nSpurious graphics for flat shading:\r\n\r\n\u003cimg width=\"561\" alt=\"test-shadings\" src=\"https://user-images.githubusercontent.com/28686753/66308548-b0194600-e907-11e9-97e6-ad61452022d2.PNG\"\u003e\r\n\r\n\r\n\r\n\r\nError given by gouraud shading when dimensions do not match:\r\n\r\n\r\n```\r\n\u003ccurrent file\u003e, line 93, in \u003cmodule\u003e\r\n fig.canvas.draw()\r\nFile \"\u003canaconda-site-packages\u003e\\matplotlib\\backends\\backend_agg.py\", line 388, in draw\r\n self.figure.draw(self.renderer)\r\nFile \"\u003canaconda-site-packages\u003e\\matplotlib\\artist.py\", line 38, in draw_wrapper\r\n return draw(artist, renderer, *args, **kwargs)\r\nFile \"\u003canaconda-site-packages\u003e\\matplotlib\\figure.py\", line 1709, in draw\r\n renderer, self, artists, self.suppressComposite)\r\nFile \"\u003canaconda-site-packages\u003e\\matplotlib\\image.py\", line 135, in _draw_list_compositing_images\r\n a.draw(renderer)\r\nFile \"\u003canaconda-site-packages\u003e\\matplotlib\\artist.py\", line 38, in draw_wrapper\r\n return draw(artist, renderer, *args, **kwargs)\r\nFile \"\u003canaconda-site-packages\u003e\\matplotlib\\axes\\_base.py\", line 2647, in draw\r\n mimage._draw_list_compositing_images(renderer, self, artists)\r\nFile \"\u003canaconda-site-packages\u003e\\matplotlib\\image.py\", line 135, in _draw_list_compositing_images\r\n a.draw(renderer)\r\nFile \"\u003canaconda-site-packages\u003e\\matplotlib\\artist.py\", line 38, in draw_wrapper\r\n return draw(artist, renderer, *args, **kwargs)\r\nFile \"\u003canaconda-site-packages\u003e\\matplotlib\\collections.py\", line 2037, in draw\r\n self._meshWidth, self._meshHeight, coordinates)\r\nFile \"\u003canaconda-site-packages\u003e\\matplotlib\\collections.py\", line 1985, in convert_mesh_to_triangles\r\n c = self.get_facecolor().reshape((meshHeight + 1, meshWidth + 1, 4))\r\nValueError: cannot reshape array of size 7644 into shape (50,40,4)\r\n```\r\n\r\n**Matplotlib version**\r\n\u003c!--Please specify your platform and versions of the relevant libraries you are using:--\u003e\r\n * Operating system: Win10\r\n * Matplotlib version: 3.1.1 (from conda)\r\n * Matplotlib backend (`print(matplotlib.get_backend())`): Qt5Agg\r\n * Python version: 3.7.4\r\n","author":{"url":"https://github.com/Tigraan","@type":"Person","name":"Tigraan"},"datePublished":"2019-10-07T11:31:38.000Z","interactionStatistic":{"@type":"InteractionCounter","interactionType":"https://schema.org/CommentAction","userInteractionCount":4},"url":"https://github.com/15388/matplotlib/issues/15388"}
| 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:68cebd5a-3570-2e4b-cea1-ca8ffc685540 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | A2E0:172D3F:263588A:33E13BE:6A548030 |
| html-safe-nonce | cd585517c39084359f55606a1ac0ccf53f4e0655b31310f78ad3d631802f0fcd |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJBMkUwOjE3MkQzRjoyNjM1ODhBOjMzRTEzQkU6NkE1NDgwMzAiLCJ2aXNpdG9yX2lkIjoiNTMzNDI1NTE1MTgzMzQ0ODQ5NiIsInJlZ2lvbl9lZGdlIjoiaWFkIiwicmVnaW9uX3JlbmRlciI6ImlhZCJ9 |
| visitor-hmac | bcc0fb4840603c64d74368d597d1733d243e70436061aea7eb7e2d3cb9715dfc |
| hovercard-subject-tag | issue:503393496 |
| 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/15388/issue_layout |
| twitter:image | https://opengraph.githubassets.com/d00302a47402546ccff228077e607bae42df8a4f9f7aba466d42309c337181d8/matplotlib/matplotlib/issues/15388 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/d00302a47402546ccff228077e607bae42df8a4f9f7aba466d42309c337181d8/matplotlib/matplotlib/issues/15388 |
| og:image:alt | Bug report Bug summary Trying to give matplotlib.collections.QuadMesh.set_array() the same argument as was used by matplotlib.axes.Axes.pcolormesh() results in an error. Moreover, the argument form... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | Tigraan |
| 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 | 6d28624802c2f7d9500239dd6870ed7031b7353b |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
Links:
Viewport: width=device-width