pylab 中的可变 alpha 混合
如何在 pylab 中控制 2D 图像的透明度?我想给出两组值(X,Y,Z,T)
,其中X,Y
是位置数组,Z
是颜色值,T
是像 imshow
这样的函数的透明度,但似乎该函数只将 alpha 作为标量。作为一个具体示例,请考虑下面尝试显示两个高斯函数的代码。该值越接近零,我希望绘图越透明。
from pylab import *
side = linspace(-1,1,100)
X,Y = meshgrid(side,side)
extent = (-1,1,-1,1)
Z1 = exp(-((X+.5)**2+Y**2))
Z2 = exp(-((X-.5)**2+(Y+.2)**2))
imshow(Z1, cmap=cm.hsv, alpha=.6, extent=extent)
imshow(Z2, cmap=cm.hsv, alpha=.6, extent=extent)
show()
注意:我不是在寻找 Z1+Z2 的图(这很简单),而是在寻找指定图像中 alpha 混合的通用方法。
How does one control the transparency over a 2D image in pylab? I'd like to give two sets of values (X,Y,Z,T)
where X,Y
are arrays of positions, Z
is the color value, and T
is the transparency to a function like imshow
but it seems that the function only takes alpha as a scalar. As a concrete example, consider the code below that attempts to display two Gaussians. The closer the value is to zero, the more transparent I'd like the plot to be.
from pylab import *
side = linspace(-1,1,100)
X,Y = meshgrid(side,side)
extent = (-1,1,-1,1)
Z1 = exp(-((X+.5)**2+Y**2))
Z2 = exp(-((X-.5)**2+(Y+.2)**2))
imshow(Z1, cmap=cm.hsv, alpha=.6, extent=extent)
imshow(Z2, cmap=cm.hsv, alpha=.6, extent=extent)
show()
Note: I am not looking for a plot of Z1+Z2 (that would be trivial) but for a general way to specify the alpha blending across an image.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以做的一件事是修改放入 imshow 的内容。第一个变量可以是您使用的灰度值,也可以是 RGB 或 RGBA 值。如果您使用 RGB/RGBA 值,则 cmap 将被忽略。例如,
将生成相同的图像,
因为
cm.hsv()
仅返回 RGBA 值。如果你看一下它返回的值,它们的 A 值(透明度)都是 1.0。因此,实现可变透明度的一种方法是这样的:您可能会找到一种更优雅的方法来做到这一点,但希望您能明白这一点。
One thing that you can do is modify what you put into imshow. The first variable can be grayscale values as you have used or it can be RGB or RGBA values. If you RGB/RGBA values then the cmap is ignored. So for instance,
will generate the same image as
because
cm.hsv()
just returns RGBA values. If you take a look at the values it returns, they all have 1.0 as the A value (the transparency). So one way to make variable transparency would be something like this:You might find a little more elegant way of doing this, but hopefully you get the idea.