在matplotlib中覆盖imshow图 [英] Overlay imshow plots in matplotlib

查看:171
本文介绍了在matplotlib中覆盖imshow图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在同一张imshow图上比较两组不同的数据,以便于查看差异. 我的第一个本能是使颜色图中的颜色透明(尤其是较低的值),但我无法使其工作:

I would like to compare two different sets of data on the same imshow plot to make it easy to see the differences. My first instinct is to make the colors in the colormap transparent (the lower values especially) but I haven't been able to get this to work:

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()

没有错误,但是第二个图的白色和黑色没有显示任何透明度.我还尝试了colorConverter方法在正常的plt.plot情况下设置颜色,尽管显示了正确的颜色,但该颜色也没有变为透明.

There is no error but the white and black of the second plot do not show any transparency. I also tried the colorConverter method to set the color in a normal plt.plot situation and the color also did not become transparent though the correct color was displayed.

我们将不胜感激关于如何叠加/比较imshow图的任何其他建议

Any additional advice on how to overlay/compare imshow plots would be much appreciated

推荐答案

您可以在imshow命令中设置alpha自变量.

You can set the alpha argument in your imshow command.

在您的示例中,img3 = plt.imshow(zvals2, interpolation='nearest', cmap=cmap2, origin='lower', alpha=0.6)

感谢您的澄清. 这是您可以做什么的描述:

Thanks for the clarification. Here is a description of what you can do:

  • 首先,选择一个 matplotlib颜色图对象(在您的情况下,是白色和黑色) ,您可以使用二进制"颜色图).或者,如果您想要的颜色图还不存在,则按照您的方式创建自己的颜色图.
  • 然后初始化该颜色图对象:这将导致它在内部创建一个名为"_lut"的数组,该数组保存rgba值.
  • 然后,您可以根据想要实现的值填充alpha值(在您的示例中,创建一个从0到0.8的数组)
  • 然后您可以使用此颜色图
  • First, choose a matplotlib colormap object (in your case, for white and black, you can take the 'binary' colormap). Or create your own colormap as you did, if the colormap you want doesn't already exist.
  • Then initialize this colormap object: this will cause it to internally create an array called "_lut" which holds rgba values.
  • Then, you can fill the alpha values according to what you want to achieve (in your example, create an array from 0 to 0.8)
  • You can then use this colormap

以下是使用您的代码的示例:

Below is an example using your code:

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()

这篇关于在matplotlib中覆盖imshow图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆