在下面的代码中,我绘制了一条线,然后在其上方绘制了一个不透明补丁 (alpha=1)。我希望被补丁覆盖的线部分被隐藏,但看起来好像线是在补丁之后绘制的。如何更改,使线条不显示出来?
![plot heart](https://i.stack.imgur.com/xr5sd.png)
该代码改编自这个 matplotlib 示例
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()