gpt4 book ai didi

python - 如何将绘制的线保持在补丁下?

转载 作者:行者123 更新时间:2023-12-05 03:46:17 25 4
gpt4 key购买 nike

在下面的代码中,我画了一条线,然后在它上面画了一个不透明的补丁 (alpha=1)。我希望被补丁覆盖的那部分线被隐藏,但看起来好像这条线是在补丁之后绘制的。怎么改,让线不透?

plot heart

代码改编自this matplotlib example

import matplotlib.path as mpath
import matplotlib.patches as mpatches
import matplotlib.pyplot as plt


fig, ax = plt.subplots()

Path = mpath.Path
path_data = [
(Path.MOVETO, (1.58, -2.57)),
(Path.CURVE4, (0.35, -1.1)),
(Path.CURVE4, (-1.75, 2.0)),
(Path.CURVE4, (0.375, 2.0)),
(Path.LINETO, (0.85, 1.15)),
(Path.CURVE4, (2.2, 3.2)),
(Path.CURVE4, (3, 0.05)),
(Path.CURVE4, (2.0, -0.5)),
(Path.CLOSEPOLY, (1.58, -2.57)),
]
codes, verts = zip(*path_data)


path = mpath.Path(verts, codes)
# plot control points and connecting lines
x, y = zip(*path.vertices)
y2 = [_y-1 for _y in y]
line, = ax.plot(x, y2, 'go-')


patch = mpatches.PathPatch(path, facecolor='r', alpha=1)
ax.add_patch(patch)

ax.grid()
ax.axis('equal')
plt.show()

最佳答案

您可以指定补丁的 z 顺序(绿线有 zorder 2,所以大于 2 的都可以):

patch = mpatches.PathPatch(path, facecolor='r', alpha=1, zorder=5)

enter image description here

关于python - 如何将绘制的线保持在补丁下?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65381655/

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