本文介绍了将 alpha 添加到现有的 matplotlib 颜色图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想叠加几个 hexbin 图,但使用内置颜色图只能看到最后一个.我不想从头构建颜色图.如果事先不知道颜色图的内部结构,如何将线性 alpha 添加到颜色图中?
I'd like to make an overlay of several hexbin plots, but with builtin colormaps only the last one is visible. I don't want to construct a colormap de novo. How one would add linear alpha to the colormap without knowing the inner structure of the colormap beforehand?
推荐答案
我不太确定这是否属于不知道颜色图的内部结构",但也许这样的事情可以添加线性 alpha到现有的颜色图?
I'm not quite sure if this qualifies within "not knowing the inner structure of the colormap", but perhaps something like this would work to add a linear alpha to an existing colormap?
import numpy as np
import matplotlib.pylab as pl
from matplotlib.colors import ListedColormap
# Random data
data1 = np.random.random((4,4))
# Choose colormap
cmap = pl.cm.RdBu
# Get the colormap colors
my_cmap = cmap(np.arange(cmap.N))
# Set alpha
my_cmap[:,-1] = np.linspace(0, 1, cmap.N)
# Create new colormap
my_cmap = ListedColormap(my_cmap)
pl.figure()
pl.subplot(121)
pl.pcolormesh(data1, cmap=pl.cm.RdBu)
pl.colorbar()
pl.subplot(122)
pl.pcolormesh(data1, cmap=my_cmap)
pl.colorbar()
这篇关于将 alpha 添加到现有的 matplotlib 颜色图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!