如何实现这样的python matplotlib heatmap colorbar?
plt.imshow(a,aspect='auto', cmap=plt.cm.gist_rainbow_r)
plt.colorbar()
发布于 2018-07-31 04:29:56
这个来自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)
https://stackoverflow.com/questions/51601272
复制相似问题