gpt4 book ai didi

python - 带有 alpha 的 matplotlib Circle patch 产生边缘和面色的重叠

转载 作者:太空狗 更新时间:2023-10-30 01:24:59 24 4
gpt4 key购买 nike

总的来说,我对 matplotlib 和 python 还比较陌生,我想做的是相当基础的。但是,即使经过相当长的谷歌搜索时间,我也找不到解决方案:

问题是:

我想画一个边框和脸颜色不同的圆,即设置 edgecolorfacecolor 不同。我还想要一个 alpha channel ,即 alpha=0.5。现在,虽然所有这些工作正常,但生成的圆圈没有单一的边框颜色,而是绘制了 2 个边框。一种是外层颜色,我为 edgecolor 指定了颜色,另一种颜色我假设是 edgecolor 和 facecolor 的组合。

这是我的代码:

from matplotlib import pyplot as plt
point = (1.0, 1.0)
c = plt.Circle(point, 1, facecolor='green', edgecolor='orange', linewidth=15.0, alpha=0.5)
fig, ax = plt.subplots()
ax.add_artist(c)
plt.show()

这是一个例子:

circle with 2 border colors

好吧,这可能是一件小事,但这第二个边界让我抓狂!

我做错了什么吗?事情就是这样吗?任何帮助将不胜感激。

最佳答案

在某种程度上,是的,这就是事实。关键是边缘实际上是画在圆的边缘上的。这意味着边宽的一半画在圆的表面上,另一半画在外面。如果现在你设置 alpha<1.0正如您得出的正确结论,您会看到面色和边色重叠。

但是,您可以去掉那个“额外边框”。以下是两种操作方法,哪一种最适合您取决于您​​具体想要做什么。

第一个建议:

恕我直言,最简单的方法是只为 facecolor 设置 alpha 。这可以通过为 facecolor 设置 alpha channel 来完成。直接省略 alpha Circle 调用中的参数.您可以使用 colorConverter 设置 alpha channel :

from matplotlib.colors import colorConverter
import matplotlib.pyplot as plt

fig, ax = plt.subplots(figsize=(8, 8))
ax.axis('equal')
ax.set_xlim(0,1)
ax.set_ylim(0,1)
# Here you set alpha for the faceolor
fc = colorConverter.to_rgba('green', alpha=0.5)
point = (1.0, 1.0)
r = 1.0
lw = 15.0
ec = 'orange'
# NOTE: do not set alpha when calling Circle!
c = plt.Circle(point, r,fc=fc, ec=ec, lw=lw)
ax.add_artist(c)
plt.show()

enter image description here

第二条建议

一个更详细的选项是在仅绘制面部后用白色边缘“删除”圆的边缘,然后仅绘制边缘。使用这种方法,两种颜色都与 alpha channel 一起出现。但请注意,在这种情况下,位于边缘“下方”的任何对象都将被边缘完全遮盖:

import matplotlib.pyplot as plt
from matplotlib.collections import PatchCollection

point = (1.0, 1.0)
r = 1.0
alpha = 0.5
lw = 15.0
fc = 'green'
ec = 'orange'
# First only draw the facecolor
c_face = plt.Circle(point, r, alpha=alpha, fc=fc, lw=0.0)
# Draw a non-transparent white edge to wipe the facecolor where they overlap
c_wipe = plt.Circle(point, r, alpha=1.0, ec='white', fc='none', lw=lw)
# Now draw only the edge
c_edge = plt.Circle(point, r, alpha=alpha, fc='none', ec=ec, lw=lw)
circle_patch = PatchCollection([c_face, c_wipe, c_edge], match_original=True)
fig, ax = plt.subplots(figsize=(8, 8))
ax.axis('equal')
ax.set_xlim(0,1)
ax.set_ylim(0,1)
ax.add_artist(circle_patch)
plt.show()

enter image description here


Here is a gist根据第二个建议处理这个问题。只需下载 mod_patch.py归档,然后开始。

使用方法如下:

import matplotlib.pyplot as plt
from mod_patch import get_Patch
fig, ax = plt.subplots(figsize=(8,8))
c = get_Patch(plt.Circle, (0,0.5), 0.5, facecolor='green', edgecolor='orange', alpha=0.5, lw=15)
ax.add_artist(c)
r = get_Patch(plt.Rectangle, (0.5,0), 0.5, 0.5, facecolor='green', edgecolor='orange', alpha=0.5, lw=15)
ax.add_artist(r)
plt.show()

enter image description here

为了完整起见,这里定义get_Patch :

from matplotlib.collections import PatchCollection


def get_Patch(a_Patch, *args, **kwargs):
background_color = kwargs.pop(
'bgc',
kwargs.pop('background_color', 'white')
)
alpha = kwargs.get('alpha', 1.0)
patches = []
lw = kwargs.get('lw', kwargs.get('linewidth', 0.0))
if alpha < 1.0 and lw:
color = kwargs.get('c', kwargs.get('color', None))
fc = kwargs.get('facecolor', kwargs.get('fc', None))
ec = kwargs.get('edgecolor', kwargs.get('ec', None))
face_kwargs = dict(kwargs)
face_kwargs['fc'] = fc if fc is not None else color
face_kwargs['lw'] = 0.0
p_face = a_Patch(*args, **face_kwargs)
patches.append(p_face)
wipe_kwargs = dict(kwargs)
wipe_kwargs['fc'] = 'none'
wipe_kwargs['ec'] = background_color
wipe_kwargs['alpha'] = 1.0
p_wipe = a_Patch(*args, **wipe_kwargs)
patches.append(p_wipe)
edge_kwargs = dict(kwargs)
edge_kwargs['fc'] = 'none'
edge_kwargs['ec'] = ec if ec is not None else color
p_edge = a_Patch(*args, **edge_kwargs)
patches.append(p_edge)
else:
p_simple = a_Patch(*args, **kwargs)
patches.append(p_simple)
return PatchCollection(patches, match_original=True)

关于python - 带有 alpha 的 matplotlib Circle patch 产生边缘和面色的重叠,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53803531/

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