简体   繁体   中英

Pyplot plot image with colormap and sequence of alpha

With the imshow function from pyplot, I can plot an (M,N) size image, where each value in the (M,N) sized array is associated to a color from a colormap

import numpy as np
import matplotlib.pyplot as plt

A = np.random.rand(5,5)
plt.imshow(A, cmap='jet')
plt.colorbar()
plt.show()

I have an (2,M,N) sized array (let's call it B ) that I would like to show. B[0] gives the hue and B[1] is the alpha. Here, B[0] would be the same as A in the first example.

I would like to write something like plt.imshow(B[0], cmap='jet', alpha=B[1]) . But the value of the alpha has to be the same for the entire image, since the alpha option has to be a scalar or None

With imshow , I can also specify an (M,N,3) or an (M,N,4) array, where the value for R,G,B and alpha are given. However, I don't know how to get the RGB values from my initial (M,N) sized array A .


I tried setting the color with the HSV scheme from my values, then converting to RGB and plotting with those colors

from matplotlib import colors

B = np.random.rand(2,5,5)

hue = B[0]
sat = B[1]
val = np.ones(B[0].shape)

rgb = colors.hsv_to_rgb(np.array([hue, sat, val]).T)

plt.imshow(rgb)
plt.show()

But then, I don't know how to make a color bar from the actual values in B .


I'm going for something like this: 预期结果 where the vertical part of the colorbar shows the value in B[0] and the horizontal part of the colorbar shows the value in B[1] .

The first part is relatively straight forward:



import numpy as np
import matplotlib.pyplot as plt

A = np.random.rand(5,5)
cmap = plt.get_cmap('jet')

rgba_img = cmap(A)
rgba_img[:, :, 3] = np.random.rand(5, 5)

plt.imshow(rgba_img)
plt.colorbar()
plt.show()

Making a colorbar is more difficult. You will basically need to replace the pcolor that is used to make the colorbar with another one that looks as you want it to.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM