Liris
Liris

Reputation: 1511

Setting alpha as an array in plt.imshow() makes the plot crashe

When trying to run this little piece of code inspired from the matplotlib documentation Blend transparency with color in 2-D images:

import numpy as np
import matplotlib.pyplot as plt

test = np.random.random((300, 300))

plt.figure()
plt.imshow(test, alpha = test/test.max())
plt.show()

I end up with :

<matplotlib.image.AxesImage at 0x7f8bfe8cc610>Traceback (most recent call last):
  File "/Users/darkvador/opt/anaconda3/lib/python3.7/site-packages/matplotlib/backends/backend_qt5.py", line 508, in _draw_idle
    self.draw()
  File "/Users/darkvador/opt/anaconda3/lib/python3.7/site-packages/matplotlib/backends/backend_agg.py", line 388, in draw
    self.figure.draw(self.renderer)
  File "/Users/darkvador/opt/anaconda3/lib/python3.7/site-packages/matplotlib/artist.py", line 38, in draw_wrapper
    return draw(artist, renderer, *args, **kwargs)
  File "/Users/darkvador/opt/anaconda3/lib/python3.7/site-packages/matplotlib/figure.py", line 1709, in draw
    renderer, self, artists, self.suppressComposite)
  File "/Users/darkvador/opt/anaconda3/lib/python3.7/site-packages/matplotlib/image.py", line 135, in _draw_list_compositing_images
    a.draw(renderer)
  File "/Users/darkvador/opt/anaconda3/lib/python3.7/site-packages/matplotlib/artist.py", line 38, in draw_wrapper
    return draw(artist, renderer, *args, **kwargs)
  File "/Users/darkvador/opt/anaconda3/lib/python3.7/site-packages/matplotlib/axes/_base.py", line 2647, in draw
    mimage._draw_list_compositing_images(renderer, self, artists)
  File "/Users/darkvador/opt/anaconda3/lib/python3.7/site-packages/matplotlib/image.py", line 135, in _draw_list_compositing_images
    a.draw(renderer)
  File "/Users/darkvador/opt/anaconda3/lib/python3.7/site-packages/matplotlib/artist.py", line 38, in draw_wrapper
    return draw(artist, renderer, *args, **kwargs)
  File "/Users/darkvador/opt/anaconda3/lib/python3.7/site-packages/matplotlib/image.py", line 619, in draw
    renderer, renderer.get_image_magnification())
  File "/Users/darkvador/opt/anaconda3/lib/python3.7/site-packages/matplotlib/image.py", line 881, in make_image
    unsampled=unsampled)
  File "/Users/darkvador/opt/anaconda3/lib/python3.7/site-packages/matplotlib/image.py", line 530, in _make_image
    np.asarray(alpha_channel, np.float32) * out_alpha * alpha,
ValueError: operands could not be broadcast together with shapes (740,740) (300,300)

I'm under macos-catalina, with python:

Python 3.7.7 (default, Mar 26 2020, 10:32:53) 
[Clang 4.0.1 (tags/RELEASE_401/final)] :: Anaconda, Inc. on darwin 

Note that the error does not occur if:

Upvotes: 0

Views: 2269

Answers (2)

L&#233;onard
L&#233;onard

Reputation: 2630

From the matplotlib 3.1.3 documentation, the alpha keyword argument must be a scalar value.

You should update to Matplotlib 3.3.1, because giving an array for the alpha parameters must have been implemented in between these two versions.

Upvotes: 3

L&#233;onard
L&#233;onard

Reputation: 2630

Your alpha array alpha/alpha.max() does not match the shape of the actual image array test. From the test code you provided, we can create an alpha array and successfully display it as an alpha-varying image with the code below. So you have to make sure the alpha array has the exact same shape as the test array.

import numpy as np
import matplotlib.pyplot as plt

test = np.random.random((300, 300))
alpha = test / test.max()
print(alpha)

plt.figure()
plt.imshow(test, alpha = alpha)
plt.show()

producing the image below.

imshow with alpha

Upvotes: 2

Related Questions