Ana*_*ake 21 python numpy matplotlib
我想在同一个imshow图上比较两组不同的数据,以便于查看差异.我的第一直觉是使colormap中的颜色透明(特别是较低的值),但我无法使其工作:
from matplotlib.colors import colorConverter
import matplotlib.pyplot as plt
import numpy as np
# create dummy data
zvals = np.random.rand(100,100)*10-5
zvals2 = np.random.rand(100,100)*10-5
# generate the transparent colors
color1 = colorConverter.to_rgba('white',alpha=0.0)
color2 = colorConverter.to_rgba('black',alpha=0.8)
# make the colormaps
cmap1 = mpl.colors.LinearSegmentedColormap.from_list('my_cmap',['green','blue'],256)
cmap2 = mpl.colors.LinearSegmentedColormap.from_list('my_cmap2',[color1,color2],256)
img2 = plt.imshow(zvals,interpolation='nearest',cmap=cmap1,origin='lower')
img3 = plt.imshow(zvals2,interpolation='nearest',cmap=cmap2,origin='lower')
plt.show()
Run Code Online (Sandbox Code Playgroud)
没有错误,但第二个图的白色和黑色没有显示任何透明度.我还尝试使用colorConverter方法在正常的plt.plot情况下设置颜色,尽管显示了正确的颜色,但颜色也没有变得透明.
关于如何叠加/比较imshow图的任何其他建议将非常感激
gca*_*tes 30
您可以alpha在imshow命令中设置参数.
在你的例子中, img3 = plt.imshow(zvals2, interpolation='nearest', cmap=cmap2, origin='lower', alpha=0.6)
谢谢你的澄清.以下是您可以执行的操作的说明:
以下是使用您的代码的示例:
from matplotlib.colors import colorConverter
import matplotlib.pyplot as plt
import numpy as np
import matplotlib as mpl
# create dummy data
zvals = np.ones((100,100))# np.random.rand(100,100)*10-5
zvals2 = np.random.rand(100,100)*10-5
# generate the colors for your colormap
color1 = colorConverter.to_rgba('white')
color2 = colorConverter.to_rgba('black')
# make the colormaps
cmap1 = mpl.colors.LinearSegmentedColormap.from_list('my_cmap',['green','blue'],256)
cmap2 = mpl.colors.LinearSegmentedColormap.from_list('my_cmap2',[color1,color2],256)
cmap2._init() # create the _lut array, with rgba values
# create your alpha array and fill the colormap with them.
# here it is progressive, but you can create whathever you want
alphas = np.linspace(0, 0.8, cmap2.N+3)
cmap2._lut[:,-1] = alphas
img2 = plt.imshow(zvals, interpolation='nearest', cmap=cmap1, origin='lower')
img3 = plt.imshow(zvals2, interpolation='nearest', cmap=cmap2, origin='lower')
plt.show()
Run Code Online (Sandbox Code Playgroud)
