Title: issue with transparency in imshow · Issue #16730 · matplotlib/matplotlib · GitHub
Open Graph Title: issue with transparency in imshow · Issue #16730 · matplotlib/matplotlib
X Title: issue with transparency in imshow · Issue #16730 · matplotlib/matplotlib
Description: Bug report Hi, I am trying to understand the following example on how to provide an array-like alpha in imshow. https://matplotlib.org/3.2.0/gallery/images_contours_and_fields/image_transparency_blend.html However, the exact same script ...
Open Graph Description: Bug report Hi, I am trying to understand the following example on how to provide an array-like alpha in imshow. https://matplotlib.org/3.2.0/gallery/images_contours_and_fields/image_transparency_bl...
X Description: Bug report Hi, I am trying to understand the following example on how to provide an array-like alpha in imshow. https://matplotlib.org/3.2.0/gallery/images_contours_and_fields/image_transparency_bl...
Opengraph URL: https://github.com/matplotlib/matplotlib/issues/16730
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"issue with transparency in imshow","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\nHi, I am trying to understand the following example on how to provide an array-like alpha in imshow.\r\n\r\nhttps://matplotlib.org/3.2.0/gallery/images_contours_and_fields/image_transparency_blend.html\r\n\r\nHowever, the exact same script gives me following errors. What is the source of this bug?\r\n\r\n**Bug summary**\r\n\r\nValueError: operands could not be broadcast together with shapes (218,218) (100,100)\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```\r\n\r\ndef normal_pdf(x, mean, var):\r\n return np.exp(-(x - mean)**2 / (2*var))\r\n\r\n# Generate the space in which the blobs will live\r\nxmin, xmax, ymin, ymax = (0, 100, 0, 100)\r\nn_bins = 100\r\nxx = np.linspace(xmin, xmax, n_bins)\r\nyy = np.linspace(ymin, ymax, n_bins)\r\n\r\n# Generate the blobs. The range of the values is roughly -.0002 to .0002\r\nmeans_high = [20, 50]\r\nmeans_low = [50, 60]\r\nvar = [150, 200]\r\n\r\ngauss_x_high = normal_pdf(xx, means_high[0], var[0])\r\ngauss_y_high = normal_pdf(yy, means_high[1], var[0])\r\n\r\ngauss_x_low = normal_pdf(xx, means_low[0], var[1])\r\ngauss_y_low = normal_pdf(yy, means_low[1], var[1])\r\n\r\nweights = (np.outer(gauss_y_high, gauss_x_high) - np.outer(gauss_y_low, gauss_x_low))\r\n\r\n# We'll also create a grey background into which the pixels will fade\r\n#greys = np.full((*weights.shape, 3), 70, dtype=np.uint8)\r\n\r\n# First we'll plot these blobs using ``imshow`` without transparency.\r\nvmax = np.abs(weights).max()\r\nimshow_kwargs = {\r\n 'vmax': vmax,\r\n 'vmin': -vmax,\r\n 'cmap': 'RdYlBu',\r\n 'extent': (xmin, xmax, ymin, ymax),\r\n}\r\n\r\nfig, ax = plt.subplots()\r\n#ax.imshow(greys)\r\nax.imshow(weights, **imshow_kwargs)\r\nax.set_axis_off()\r\n\r\n# Create an alpha channel of linearly increasing values moving to the right.\r\nalphas = np.ones(weights.shape)\r\nalphas[:, 30:] = np.linspace(1, 0, 70)\r\n\r\n# Create the figure and image\r\n# Note that the absolute values may be slightly different\r\nfig, ax = plt.subplots()\r\n#ax.imshow(greys)\r\nax.imshow(weights, alpha=alphas, **imshow_kwargs)\r\nax.set_axis_off()\r\n```\r\n\r\n**Actual outcome**\r\n\r\n\u003c!--The output produced by the above code, which may be a screenshot, console output, etc.--\u003e\r\n\r\n```\r\n# If applicable, paste the console output here\r\n#\r\n#\r\n```\r\n```\r\nError in callback \u003cfunction post_execute at 0x7f4972d48578\u003e (for post_execute):\r\n---------------------------------------------------------------------------\r\nValueError Traceback (most recent call last)\r\n/xxx/anaconda2/lib/python2.7/site-packages/matplotlib/pyplot.pyc in post_execute()\r\n 147 def post_execute():\r\n 148 if matplotlib.is_interactive():\r\n--\u003e 149 draw_all()\r\n 150 \r\n 151 # IPython \u003e= 2\r\n\r\n/xxx/anaconda2/lib/python2.7/site-packages/matplotlib/_pylab_helpers.pyc in draw_all(cls, force)\r\n 134 for f_mgr in cls.get_all_fig_managers():\r\n 135 if force or f_mgr.canvas.figure.stale:\r\n--\u003e 136 f_mgr.canvas.draw_idle()\r\n 137 \r\n 138 atexit.register(Gcf.destroy_all)\r\n\r\n/xxx/anaconda2/lib/python2.7/site-packages/matplotlib/backend_bases.pyc in draw_idle(self, *args, **kwargs)\r\n 2053 if not self._is_idle_drawing:\r\n 2054 with self._idle_draw_cntx():\r\n-\u003e 2055 self.draw(*args, **kwargs)\r\n 2056 \r\n 2057 def draw_cursor(self, event):\r\n\r\n/xxx/anaconda2/lib/python2.7/site-packages/matplotlib/backends/backend_agg.pyc in draw(self)\r\n 435 # if toolbar:\r\n 436 # toolbar.set_cursor(cursors.WAIT)\r\n--\u003e 437 self.figure.draw(self.renderer)\r\n 438 # A GUI class may be need to update a window using this draw, so\r\n 439 # don't forget to call the superclass.\r\n\r\n/xxx/anaconda2/lib/python2.7/site-packages/matplotlib/artist.pyc in draw_wrapper(artist, renderer, *args, **kwargs)\r\n 53 renderer.start_filter()\r\n 54 \r\n---\u003e 55 return draw(artist, renderer, *args, **kwargs)\r\n 56 finally:\r\n 57 if artist.get_agg_filter() is not None:\r\n\r\n/xxx/anaconda2/lib/python2.7/site-packages/matplotlib/figure.pyc in draw(self, renderer)\r\n 1491 \r\n 1492 mimage._draw_list_compositing_images(\r\n-\u003e 1493 renderer, self, artists, self.suppressComposite)\r\n 1494 \r\n 1495 renderer.close_group('figure')\r\n\r\n/xxx/anaconda2/lib/python2.7/site-packages/matplotlib/image.pyc in _draw_list_compositing_images(renderer, parent, artists, suppress_composite)\r\n 139 if not_composite or not has_images:\r\n 140 for a in artists:\r\n--\u003e 141 a.draw(renderer)\r\n 142 else:\r\n 143 # Composite any adjacent images together\r\n\r\n/xxx/anaconda2/lib/python2.7/site-packages/matplotlib/artist.pyc in draw_wrapper(artist, renderer, *args, **kwargs)\r\n 53 renderer.start_filter()\r\n 54 \r\n---\u003e 55 return draw(artist, renderer, *args, **kwargs)\r\n 56 finally:\r\n 57 if artist.get_agg_filter() is not None:\r\n\r\n/xxx/anaconda2/lib/python2.7/site-packages/matplotlib/axes/_base.pyc in draw(self, renderer, inframe)\r\n 2633 renderer.stop_rasterizing()\r\n 2634 \r\n-\u003e 2635 mimage._draw_list_compositing_images(renderer, self, artists)\r\n 2636 \r\n 2637 renderer.close_group('axes')\r\n\r\n/xxx/anaconda2/lib/python2.7/site-packages/matplotlib/image.pyc in _draw_list_compositing_images(renderer, parent, artists, suppress_composite)\r\n 139 if not_composite or not has_images:\r\n 140 for a in artists:\r\n--\u003e 141 a.draw(renderer)\r\n 142 else:\r\n 143 # Composite any adjacent images together\r\n\r\n/xxx/anaconda2/lib/python2.7/site-packages/matplotlib/artist.pyc in draw_wrapper(artist, renderer, *args, **kwargs)\r\n 53 renderer.start_filter()\r\n 54 \r\n---\u003e 55 return draw(artist, renderer, *args, **kwargs)\r\n 56 finally:\r\n 57 if artist.get_agg_filter() is not None:\r\n\r\n/xxx/anaconda2/lib/python2.7/site-packages/matplotlib/image.pyc in draw(self, renderer, *args, **kwargs)\r\n 592 else:\r\n 593 im, l, b, trans = self.make_image(\r\n--\u003e 594 renderer, renderer.get_image_magnification())\r\n 595 if im is not None:\r\n 596 renderer.draw_image(gc, l, b, im)\r\n\r\n/xxx/anaconda2/lib/python2.7/site-packages/matplotlib/image.pyc in make_image(self, renderer, magnification, unsampled)\r\n 840 return self._make_image(\r\n 841 self._A, bbox, transformed_bbox, self.axes.bbox, magnification,\r\n--\u003e 842 unsampled=unsampled)\r\n 843 \r\n 844 def _check_unsampled_image(self, renderer):\r\n\r\n/xxx/anaconda2/lib/python2.7/site-packages/matplotlib/image.pyc in _make_image(self, A, in_bbox, out_bbox, clip_bbox, magnification, unsampled, round_to_pixel_border)\r\n 520 alpha_channel = output[:, :, 3]\r\n 521 alpha_channel[:] = np.asarray(\r\n--\u003e 522 np.asarray(alpha_channel, np.float32) * out_alpha * alpha,\r\n 523 np.uint8)\r\n 524 \r\n\r\nValueError: operands could not be broadcast together with shapes (218,218) (100,100) \r\n\r\n---------------------------------------------------------------------------\r\nValueError Traceback (most recent call last)\r\n/xxx/anaconda2/lib/python2.7/site-packages/IPython/core/formatters.pyc in __call__(self, obj)\r\n 332 pass\r\n 333 else:\r\n--\u003e 334 return printer(obj)\r\n 335 # Finally look for special method names\r\n 336 method = get_real_method(obj, self.print_method)\r\n\r\n/xxx/anaconda2/lib/python2.7/site-packages/IPython/core/pylabtools.pyc in \u003clambda\u003e(fig)\r\n 245 \r\n 246 if 'png' in formats:\r\n--\u003e 247 png_formatter.for_type(Figure, lambda fig: print_figure(fig, 'png', **kwargs))\r\n 248 if 'retina' in formats or 'png2x' in formats:\r\n 249 png_formatter.for_type(Figure, lambda fig: retina_figure(fig, **kwargs))\r\n\r\n/xxx/anaconda2/lib/python2.7/site-packages/IPython/core/pylabtools.pyc in print_figure(fig, fmt, bbox_inches, **kwargs)\r\n 129 \r\n 130 bytes_io = BytesIO()\r\n--\u003e 131 fig.canvas.print_figure(bytes_io, **kw)\r\n 132 data = bytes_io.getvalue()\r\n 133 if fmt == 'svg':\r\n\r\n/xxx/anaconda2/lib/python2.7/site-packages/matplotlib/backend_bases.pyc in print_figure(self, filename, dpi, facecolor, edgecolor, orientation, format, **kwargs)\r\n 2210 orientation=orientation,\r\n 2211 dryrun=True,\r\n-\u003e 2212 **kwargs)\r\n 2213 renderer = self.figure._cachedRenderer\r\n 2214 bbox_inches = self.figure.get_tightbbox(renderer)\r\n\r\n/xxx/anaconda2/lib/python2.7/site-packages/matplotlib/backends/backend_agg.pyc in print_png(self, filename_or_obj, *args, **kwargs)\r\n 515 \r\n 516 def print_png(self, filename_or_obj, *args, **kwargs):\r\n--\u003e 517 FigureCanvasAgg.draw(self)\r\n 518 renderer = self.get_renderer()\r\n 519 original_dpi = renderer.dpi\r\n\r\n/xxx/anaconda2/lib/python2.7/site-packages/matplotlib/backends/backend_agg.pyc in draw(self)\r\n 435 # if toolbar:\r\n 436 # toolbar.set_cursor(cursors.WAIT)\r\n--\u003e 437 self.figure.draw(self.renderer)\r\n 438 # A GUI class may be need to update a window using this draw, so\r\n 439 # don't forget to call the superclass.\r\n\r\n/xxx/anaconda2/lib/python2.7/site-packages/matplotlib/artist.pyc in draw_wrapper(artist, renderer, *args, **kwargs)\r\n 53 renderer.start_filter()\r\n 54 \r\n---\u003e 55 return draw(artist, renderer, *args, **kwargs)\r\n 56 finally:\r\n 57 if artist.get_agg_filter() is not None:\r\n\r\n/xxx/anaconda2/lib/python2.7/site-packages/matplotlib/figure.pyc in draw(self, renderer)\r\n 1491 \r\n 1492 mimage._draw_list_compositing_images(\r\n-\u003e 1493 renderer, self, artists, self.suppressComposite)\r\n 1494 \r\n 1495 renderer.close_group('figure')\r\n\r\n/xxx/anaconda2/lib/python2.7/site-packages/matplotlib/image.pyc in _draw_list_compositing_images(renderer, parent, artists, suppress_composite)\r\n 139 if not_composite or not has_images:\r\n 140 for a in artists:\r\n--\u003e 141 a.draw(renderer)\r\n 142 else:\r\n 143 # Composite any adjacent images together\r\n\r\n/xxx/anaconda2/lib/python2.7/site-packages/matplotlib/artist.pyc in draw_wrapper(artist, renderer, *args, **kwargs)\r\n 53 renderer.start_filter()\r\n 54 \r\n---\u003e 55 return draw(artist, renderer, *args, **kwargs)\r\n 56 finally:\r\n 57 if artist.get_agg_filter() is not None:\r\n\r\n/xxx/anaconda2/lib/python2.7/site-packages/matplotlib/axes/_base.pyc in draw(self, renderer, inframe)\r\n 2633 renderer.stop_rasterizing()\r\n 2634 \r\n-\u003e 2635 mimage._draw_list_compositing_images(renderer, self, artists)\r\n 2636 \r\n 2637 renderer.close_group('axes')\r\n\r\n/xxx/anaconda2/lib/python2.7/site-packages/matplotlib/image.pyc in _draw_list_compositing_images(renderer, parent, artists, suppress_composite)\r\n 139 if not_composite or not has_images:\r\n 140 for a in artists:\r\n--\u003e 141 a.draw(renderer)\r\n 142 else:\r\n 143 # Composite any adjacent images together\r\n\r\n/xxx/anaconda2/lib/python2.7/site-packages/matplotlib/artist.pyc in draw_wrapper(artist, renderer, *args, **kwargs)\r\n 53 renderer.start_filter()\r\n 54 \r\n---\u003e 55 return draw(artist, renderer, *args, **kwargs)\r\n 56 finally:\r\n 57 if artist.get_agg_filter() is not None:\r\n\r\n/xxx/anaconda2/lib/python2.7/site-packages/matplotlib/image.pyc in draw(self, renderer, *args, **kwargs)\r\n 592 else:\r\n 593 im, l, b, trans = self.make_image(\r\n--\u003e 594 renderer, renderer.get_image_magnification())\r\n 595 if im is not None:\r\n 596 renderer.draw_image(gc, l, b, im)\r\n\r\n/xxx/anaconda2/lib/python2.7/site-packages/matplotlib/image.pyc in make_image(self, renderer, magnification, unsampled)\r\n 840 return self._make_image(\r\n 841 self._A, bbox, transformed_bbox, self.axes.bbox, magnification,\r\n--\u003e 842 unsampled=unsampled)\r\n 843 \r\n 844 def _check_unsampled_image(self, renderer):\r\n\r\n/xxx/anaconda2/lib/python2.7/site-packages/matplotlib/image.pyc in _make_image(self, A, in_bbox, out_bbox, clip_bbox, magnification, unsampled, round_to_pixel_border)\r\n 520 alpha_channel = output[:, :, 3]\r\n 521 alpha_channel[:] = np.asarray(\r\n--\u003e 522 np.asarray(alpha_channel, np.float32) * out_alpha * alpha,\r\n 523 np.uint8)\r\n 524 \r\n\r\nValueError: operands could not be broadcast together with shapes (218,218) (100,100)\r\n```\r\n\r\n**Expected outcome**\r\n\r\n\u003c!--A description of the expected outcome from the code snippet--\u003e\r\n\u003c!--If this used to work in an earlier version of Matplotlib, please note the version it used to work on--\u003e\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: \r\n * Matplotlib version: 2.2.3\r\n * Matplotlib backend (`print(matplotlib.get_backend())`): TkAgg\r\n * Python version: 2.7.16\r\n * Jupyter notebook version (if applicable): 5.7.8\r\n * Other libraries: Numpy version: 1.16.5\r\n\r\n\u003c!--Please tell us how you installed matplotlib and python e.g., from source, pip, conda--\u003e\r\n\u003c!--If you installed from conda, please specify which channel you used if not the default--\u003e\r\n\r\n","author":{"url":"https://github.com/pink2289","@type":"Person","name":"pink2289"},"datePublished":"2020-03-11T10:54:17.000Z","interactionStatistic":{"@type":"InteractionCounter","interactionType":"https://schema.org/CommentAction","userInteractionCount":5},"url":"https://github.com/16730/matplotlib/issues/16730"}
| 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:c7ffcd98-0074-07cb-2f90-c3f9267fac7c |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | D300:15606D:281883:3625EC:6A5189F1 |
| html-safe-nonce | 9e10680c50ea669a074111bdc2905d1e6696996a6975be3f986f6654f261f811 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJEMzAwOjE1NjA2RDoyODE4ODM6MzYyNUVDOjZBNTE4OUYxIiwidmlzaXRvcl9pZCI6IjgxMDc0Mzk1MDUzNDkzODA1OTQiLCJyZWdpb25fZWRnZSI6ImlhZCIsInJlZ2lvbl9yZW5kZXIiOiJpYWQifQ== |
| visitor-hmac | 3c80706a8eec4d833b265ec632bbd7063090d3ff95c99430a3dd521f6ba56d62 |
| hovercard-subject-tag | issue:579175193 |
| 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/16730/issue_layout |
| twitter:image | https://opengraph.githubassets.com/5f728d497f6bfb1536ed906e03f726c699184b08b67d0e0fce41e702d47e27dc/matplotlib/matplotlib/issues/16730 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/5f728d497f6bfb1536ed906e03f726c699184b08b67d0e0fce41e702d47e27dc/matplotlib/matplotlib/issues/16730 |
| og:image:alt | Bug report Hi, I am trying to understand the following example on how to provide an array-like alpha in imshow. https://matplotlib.org/3.2.0/gallery/images_contours_and_fields/image_transparency_bl... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | pink2289 |
| 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 | cb77a7b8b06cd86c243be33fd8e7922d70a9d243 |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
Links:
Viewport: width=device-width