这个例子来自matplotlib画廊,展示了一些制作自定义颜色映射的不同方法,包括透明度: https://matplotlib.org/examples/pylab_examples/custom_cmap.html
在您的情况下,看起来您想要一个修改过的gist_rainbow颜色映射。 您可以通过修改α通道来实现此目的,方法如下:
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.colors import LinearSegmentedColormap
# get colormap
ncolors = 256
color_array = plt.get_cmap('gist_rainbow')(range(ncolors))
# change alpha values
color_array[:,-1] = np.linspace(1.0,0.0,ncolors)
# create a colormap object
map_object = LinearSegmentedColormap.from_list(name='rainbow_alpha',colors=color_array)
# register this new colormap with matplotlib
plt.register_cmap(cmap=map_object)
# show some example data
f,ax = plt.subplots()
h = ax.imshow(np.random.rand(100,100),cmap='rainbow_alpha')
plt.colorbar(mappable=h)
np.linspace(1.0, 0.0, ncolors) -> np.linspace(0.0, 1.0, ncolors)
进行还原是必要的,正如 @李宏强 在评论中建议的那样,这样才能使其正常工作。 - vdi
gist_rainbow_r
,而是rainbow_r
。请参考此链接以获取更多详细信息:https://matplotlib.org/examples/color/colormaps_reference.html。 如果这正是你所需要的,请告诉我。 - Sheldore