python matplotlib热图颜色条从透明

李宏强*_*李宏强 5 python matplotlib heatmap colorbar

如何像这样实现python matplotlib heatmap colorbar?

在此处输入图片说明

plt.imshow(a,aspect='auto', cmap=plt.cm.gist_rainbow_r)
plt.colorbar()
Run Code Online (Sandbox Code Playgroud)

dtw*_*ard 15

matplotlib 库中的此示例显示了制作自定义颜色图的一些不同方法,包括透明度:https : //matplotlib.org/examples/pylab_examples/custom_cmap.html

在您的情况下,您似乎想要 gist_rainbow 颜色图的修改版本。您可以通过如下修改 alpha 通道来实现此目的:

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)
Run Code Online (Sandbox Code Playgroud)

  • 谢谢!!!!我已经完成了这项工作############ 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_r')(range(ncolors)) # 改变 alpha 值 color_array[:,-1] = np.linspace(0.0,1.0,ncolors) (3认同)