Title: Imshow breaks if given a unyt_array input · Issue #18077 · matplotlib/matplotlib · GitHub
Open Graph Title: Imshow breaks if given a unyt_array input · Issue #18077 · matplotlib/matplotlib
X Title: Imshow breaks if given a unyt_array input · Issue #18077 · matplotlib/matplotlib
Description: Bug report Bug summary matplotlib.pyplot.imshow doesn't seem to play nicely with unyt_arrays like other plotting functions do. Description This problem was originally reported on unyt (yt-project/unyt#161) According to unyt's documentati...
Open Graph Description: Bug report Bug summary matplotlib.pyplot.imshow doesn't seem to play nicely with unyt_arrays like other plotting functions do. Description This problem was originally reported on unyt (yt-project/u...
X Description: Bug report Bug summary matplotlib.pyplot.imshow doesn't seem to play nicely with unyt_arrays like other plotting functions do. Description This problem was originally reported on unyt (yt-proje...
Opengraph URL: https://github.com/matplotlib/matplotlib/issues/18077
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Imshow breaks if given a unyt_array input","articleBody":"### Bug report\r\n\r\n**Bug summary**\r\n`matplotlib.pyplot.imshow` doesn't seem to play nicely with `unyt_arrays` like other plotting functions do.\r\n\r\n\r\n\r\n\r\n\r\n### Description\r\n\r\nThis problem was originally reported on unyt (https://github.com/yt-project/unyt/issues/161)\r\nAccording to unyt's documentation matplotlib is \"unyt_aware\", but it seems that some plotting functions are not well behaved.\r\n\r\n\r\n### What I Did\r\n**Code for reproduction**\r\n```python\r\nimport unyt\r\nimport numpy as np\r\nfrom matplotlib import pyplot as plt\r\n\r\narr = unyt.unyt_array(np.ones((16, 16)), \"g/cm**3\")\r\nplt.pcolormesh(arr) # works\r\nplt.contourf(arr) # works\r\nplt.imshow(arr) # breaks\r\n\u003e\u003e UnitOperationError: The \u003cufunc 'add'\u003e operator for unyt_arrays with units \"g/cm**3\" \r\n(dimensions \"(mass)/(length)**3\") and \"dimensionless\" (dimensions \"1\") is not well defined.\r\n```\r\n\r\n**Actual outcome**\r\n\r\nFull trace\r\n\r\n```\r\nUnitOperationError Traceback (most recent call last)\r\n\u003cipython-input-9-7192cbd0f805\u003e in \u003cmodule\u003e\r\n----\u003e 1 plt.savefig(\"hello\")\r\n\r\n~/miniconda3/envs/mpl_latest/lib/python3.8/site-packages/matplotlib/pyplot.py in savefig(*args, **kwargs)\r\n 840 def savefig(*args, **kwargs):\r\n 841 fig = gcf()\r\n--\u003e 842 res = fig.savefig(*args, **kwargs)\r\n 843 fig.canvas.draw_idle() # need this if 'transparent=True' to reset colors\r\n 844 return res\r\n\r\n~/miniconda3/envs/mpl_latest/lib/python3.8/site-packages/matplotlib/figure.py in savefig(self, fname, transparent, **kwargs)\r\n 2309 patch.set_edgecolor('none')\r\n 2310\r\n-\u003e 2311 self.canvas.print_figure(fname, **kwargs)\r\n 2312\r\n 2313 if transparent:\r\n\r\n~/miniconda3/envs/mpl_latest/lib/python3.8/site-packages/matplotlib/backend_bases.py in print_figure(self, filename, dpi, facecolor, edgecolor, orientation, format, bbox_inches, pad_inches, bbox_extra_artists, backend, **kwargs)\r\n 2208\r\n 2209 try:\r\n-\u003e 2210 result = print_method(\r\n 2211 filename,\r\n 2212 dpi=dpi,\r\n\r\n~/miniconda3/envs/mpl_latest/lib/python3.8/site-packages/matplotlib/backend_bases.py in wrapper(*args, **kwargs)\r\n 1637 kwargs.pop(arg)\r\n 1638\r\n-\u003e 1639 return func(*args, **kwargs)\r\n 1640\r\n 1641 return wrapper\r\n\r\n~/miniconda3/envs/mpl_latest/lib/python3.8/site-packages/matplotlib/backends/backend_agg.py in print_png(self, filename_or_obj, metadata, pil_kwargs, *args)\r\n 507 *metadata*, including the default 'Software' key.\r\n 508 \"\"\"\r\n--\u003e 509 FigureCanvasAgg.draw(self)\r\n 510 mpl.image.imsave(\r\n 511 filename_or_obj, self.buffer_rgba(), format=\"png\", origin=\"upper\",\r\n\r\n~/miniconda3/envs/mpl_latest/lib/python3.8/site-packages/matplotlib/backends/backend_agg.py in draw(self)\r\n 405 (self.toolbar._wait_cursor_for_draw_cm() if self.toolbar\r\n 406 else nullcontext()):\r\n--\u003e 407 self.figure.draw(self.renderer)\r\n 408 # A GUI class may be need to update a window using this draw, so\r\n 409 # don't forget to call the superclass.\r\n\r\n~/miniconda3/envs/mpl_latest/lib/python3.8/site-packages/matplotlib/artist.py in draw_wrapper(artist, renderer, *args, **kwargs)\r\n 39 renderer.start_filter()\r\n 40\r\n---\u003e 41 return draw(artist, renderer, *args, **kwargs)\r\n 42 finally:\r\n 43 if artist.get_agg_filter() is not None:\r\n\r\n~/miniconda3/envs/mpl_latest/lib/python3.8/site-packages/matplotlib/figure.py in draw(self, renderer)\r\n 1861\r\n 1862 self.patch.draw(renderer)\r\n-\u003e 1863 mimage._draw_list_compositing_images(\r\n 1864 renderer, self, artists, self.suppressComposite)\r\n 1865\r\n\r\n~/miniconda3/envs/mpl_latest/lib/python3.8/site-packages/matplotlib/image.py in _draw_list_compositing_images(renderer, parent, artists, suppress_composite)\r\n 130 if not_composite or not has_images:\r\n 131 for a in artists:\r\n--\u003e 132 a.draw(renderer)\r\n 133 else:\r\n 134 # Composite any adjacent images together\r\n\r\n~/miniconda3/envs/mpl_latest/lib/python3.8/site-packages/matplotlib/artist.py in draw_wrapper(artist, renderer, *args, **kwargs)\r\n 39 renderer.start_filter()\r\n 40\r\n---\u003e 41 return draw(artist, renderer, *args, **kwargs)\r\n 42 finally:\r\n 43 if artist.get_agg_filter() is not None:\r\n\r\n~/miniconda3/envs/mpl_latest/lib/python3.8/site-packages/matplotlib/cbook/deprecation.py in wrapper(*inner_args, **inner_kwargs)\r\n 409 else deprecation_addendum,\r\n 410 **kwargs)\r\n--\u003e 411 return func(*inner_args, **inner_kwargs)\r\n 412\r\n 413 return wrapper\r\n\r\n~/miniconda3/envs/mpl_latest/lib/python3.8/site-packages/matplotlib/axes/_base.py in draw(self, renderer, inframe)\r\n 2746 renderer.stop_rasterizing()\r\n 2747\r\n-\u003e 2748 mimage._draw_list_compositing_images(renderer, self, artists)\r\n 2749\r\n 2750 renderer.close_group('axes')\r\n\r\n~/miniconda3/envs/mpl_latest/lib/python3.8/site-packages/matplotlib/image.py in _draw_list_compositing_images(renderer, parent, artists, suppress_composite)\r\n 130 if not_composite or not has_images:\r\n 131 for a in artists:\r\n--\u003e 132 a.draw(renderer)\r\n 133 else:\r\n 134 # Composite any adjacent images together\r\n\r\n~/miniconda3/envs/mpl_latest/lib/python3.8/site-packages/matplotlib/artist.py in draw_wrapper(artist, renderer, *args, **kwargs)\r\n 39 renderer.start_filter()\r\n 40\r\n---\u003e 41 return draw(artist, renderer, *args, **kwargs)\r\n 42 finally:\r\n 43 if artist.get_agg_filter() is not None:\r\n\r\n~/miniconda3/envs/mpl_latest/lib/python3.8/site-packages/matplotlib/image.py in draw(self, renderer, *args, **kwargs)\r\n 636 renderer.draw_image(gc, l, b, im, trans)\r\n 637 else:\r\n--\u003e 638 im, l, b, trans = self.make_image(\r\n 639 renderer, renderer.get_image_magnification())\r\n 640 if im is not None:\r\n\r\n~/miniconda3/envs/mpl_latest/lib/python3.8/site-packages/matplotlib/image.py in make_image(self, renderer, magnification, unsampled)\r\n 921 clip = ((self.get_clip_box() or self.axes.bbox) if self.get_clip_on()\r\n 922 else self.figure.bbox)\r\n--\u003e 923 return self._make_image(self._A, bbox, transformed_bbox, clip,\r\n 924 magnification, unsampled=unsampled)\r\n 925\r\n\r\n~/miniconda3/envs/mpl_latest/lib/python3.8/site-packages/matplotlib/image.py in _make_image(self, A, in_bbox, out_bbox, clip_bbox, magnification, unsampled, round_to_pixel_border)\r\n 446 self.norm.autoscale_None(A)\r\n 447 dv = np.float64(self.norm.vmax) - np.float64(self.norm.vmin)\r\n--\u003e 448 vmid = self.norm.vmin + dv / 2\r\n 449 fact = 1e7 if scaled_dtype == np.float64 else 1e4\r\n 450 newmin = vmid - dv * fact\r\n\r\n~/miniconda3/envs/mpl_latest/lib/python3.8/site-packages/numpy/ma/core.py in __add__(self, other)\r\n 4136 if self._delegate_binop(other):\r\n 4137 return NotImplemented\r\n-\u003e 4138 return add(self, other)\r\n 4139\r\n 4140 def __radd__(self, other):\r\n\r\n~/miniconda3/envs/mpl_latest/lib/python3.8/site-packages/numpy/ma/core.py in __call__(self, a, b, *args, **kwargs)\r\n 1019 with np.errstate():\r\n 1020 np.seterr(divide='ignore', invalid='ignore')\r\n-\u003e 1021 result = self.f(da, db, *args, **kwargs)\r\n 1022 # Get the mask for the result\r\n 1023 (ma, mb) = (getmask(a), getmask(b))\r\n\r\n~/miniconda3/envs/mpl_latest/lib/python3.8/site-packages/unyt/array.py in __array_ufunc__(self, ufunc, method, *inputs, **kwargs)\r\n 1737 raise UnitOperationError(ufunc, u0, u1)\r\n 1738 else:\r\n-\u003e 1739 raise UnitOperationError(ufunc, u0, u1)\r\n 1740 conv, offset = u1.get_conversion_factor(u0, inp1.dtype)\r\n 1741 new_dtype = np.dtype(\"f\" + str(inp1.dtype.itemsize))\r\n\r\nUnitOperationError: The \u003cufunc 'add'\u003e operator for unyt_arrays with units \"g/cm**3\" (dimensions \"(mass)/(length)**3\") and \"dimensionless\" (dimensions \"1\") is not well defined\r\n```\r\n\r\n**Expected outcome**\r\nA usable figure :)\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: MacOS\r\n * Matplotlib version: 3.3.0\r\n * Matplotlib backend (`print(matplotlib.get_backend())`): MacOSX\r\n * Python version: 3.8.3\r\n * Jupyter version (if applicable):\r\n * Other libraries: IPython 7.16.1\r\n\r\nmpl installed via cona (Conda-forge channel)\r\n\r\n","author":{"url":"https://github.com/neutrinoceros","@type":"Person","name":"neutrinoceros"},"datePublished":"2020-07-26T15:15:55.000Z","interactionStatistic":{"@type":"InteractionCounter","interactionType":"https://schema.org/CommentAction","userInteractionCount":6},"url":"https://github.com/18077/matplotlib/issues/18077"}
| 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:04817472-7bf4-7665-476d-2c6ffd5e81aa |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | D2A4:381414:561EF8:73C6E7:6A534487 |
| html-safe-nonce | f85cd7cdebc67f5b9e77cf068d0004798c95d5935409dd98be66bcce0c98a700 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJEMkE0OjM4MTQxNDo1NjFFRjg6NzNDNkU3OjZBNTM0NDg3IiwidmlzaXRvcl9pZCI6IjE2NzQwMzI0OTExNjQ1NTAyNzkiLCJyZWdpb25fZWRnZSI6ImlhZCIsInJlZ2lvbl9yZW5kZXIiOiJpYWQifQ== |
| visitor-hmac | 95cca988f710d62fdade5c590444e8602adcb74caa2cfe9acc2bb9e657d345a6 |
| hovercard-subject-tag | issue:665800695 |
| 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/18077/issue_layout |
| twitter:image | https://opengraph.githubassets.com/8a635308e72982a2ce37d8b76fdba39d66b91461851219c651ed29a0584f1fcd/matplotlib/matplotlib/issues/18077 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/8a635308e72982a2ce37d8b76fdba39d66b91461851219c651ed29a0584f1fcd/matplotlib/matplotlib/issues/18077 |
| og:image:alt | Bug report Bug summary matplotlib.pyplot.imshow doesn't seem to play nicely with unyt_arrays like other plotting functions do. Description This problem was originally reported on unyt (yt-project/u... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | neutrinoceros |
| 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