gpt4 book ai didi

python - 更改 facecolor "alpha"值会产生不需要的边缘(matplotlib pcolormesh)

转载 作者:太空宇宙 更新时间:2023-11-03 15:41:55 25 4
gpt4 key购买 nike

我需要使用 pcolormesh 引入一个非常量 alpha 值(imshow 是先验的而不是可能的替代品,因为我需要对轴使用对数刻度——因此沿每个坐标的间距不规则)。

正在关注 this post ,我试图改变面孔的后验阿尔法值。但是,在结果中,我无法摆脱出现的边缘。

这是一个最小的例子,我在其中绘制了一个 2D 高斯凹凸(只有很少的点),alpha 从左下角到右上角递增:

from matplotlib import pyplot as plt
import numpy as np

# start with coordinates, corresponding meshgrid to compute the "shading" value and
# extended coordinate array for pcolormesh (center mesh)
xx = np.linspace(-4,4,7)
xmesh, ymesh = np.meshgrid(xx,xx)
xplot = np.pad(0.5*(xx[1:]+xx[:-1]),1,'reflect',reflect_type="odd") # center & extend
yy = np.exp(-xx[None,:]**2-xx[:,None]**2) # data to plot

# plot the data
fig = plt.figure()
hpc = plt.pcolormesh(xplot, xplot, yy, shading="flat", edgecolor=None)
plt.gca().set_aspect(1)

# change alpha of the faces: lower-left to upper-right gradient
fig.canvas.draw() # this generate face color array
colors = hpc.get_facecolor()
grad = ( (xmesh.ravel()+ymesh.ravel())/2. - xx.min() ) / ( xx.max()-xx.min() )
colors[:,3] = grad.ravel() # change alpha
hpc.set_facecolor(colors) # update face colors
fig.canvas.draw() # make the modification appears

结果如下所示:2D 高斯凹凸(点数很少),alpha 从左下角到右上角递增: The result looks like this: 2D gaussian bump (with very few points), with alpha increasing from the lower left to the upper right corner

是否有可能摆脱这些边缘?我的问题是我什至不知道它来自哪里...我尝试添加 hpc.set_antialiased(True)hpc.set_rasterized(True),明确地添加使用 hpc.set_facecolor(face) 设置边缘,将线宽调整到非常小的值——这些都不起作用。

非常感谢您的帮助

最佳答案

问题是这些正方形有一点点重叠,而且它们有些透明(您将它们的 alpha 值设置为 != 1)——所以在重叠处,它们的透明度低于应有的水平,而且它看起来像一条线。

您可以通过使方 block 不透明来修复它,但颜色就像它们具有规定的透明度一样,背景为白色:

def alpha_to_white(color):
white = np.array([1,1,1])
alpha = color[-1]
color = color[:-1]
return alpha*color + (1 - alpha)*white

colors = np.array([alpha_to_white(color) for color in colors])

enter image description here

关于python - 更改 facecolor "alpha"值会产生不需要的边缘(matplotlib pcolormesh),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52100747/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com