matplotlib 如何将绘制的线保留在面片下

dgtucam1  于 2023-10-24  发布在  其他
关注(0)|答案(1)|浏览(130)

在下面的代码中,我画了一条线,然后在它上面画了一个不透明的补丁(alpha=1)。我希望被补丁覆盖的那部分线被隐藏起来,但它看起来就像是在补丁之后画的线。如何改变它,使线不显示出来?

代码改编自这个matplotlib示例

  1. import matplotlib.path as mpath
  2. import matplotlib.patches as mpatches
  3. import matplotlib.pyplot as plt
  4. fig, ax = plt.subplots()
  5. Path = mpath.Path
  6. path_data = [
  7. (Path.MOVETO, (1.58, -2.57)),
  8. (Path.CURVE4, (0.35, -1.1)),
  9. (Path.CURVE4, (-1.75, 2.0)),
  10. (Path.CURVE4, (0.375, 2.0)),
  11. (Path.LINETO, (0.85, 1.15)),
  12. (Path.CURVE4, (2.2, 3.2)),
  13. (Path.CURVE4, (3, 0.05)),
  14. (Path.CURVE4, (2.0, -0.5)),
  15. (Path.CLOSEPOLY, (1.58, -2.57)),
  16. ]
  17. codes, verts = zip(*path_data)
  18. path = mpath.Path(verts, codes)
  19. # plot control points and connecting lines
  20. x, y = zip(*path.vertices)
  21. y2 = [_y-1 for _y in y]
  22. line, = ax.plot(x, y2, 'go-')
  23. patch = mpatches.PathPatch(path, facecolor='r', alpha=1)
  24. ax.add_patch(patch)
  25. ax.grid()
  26. ax.axis('equal')
  27. plt.show()
xyhw6mcr

xyhw6mcr1#

您可以指定补丁的z顺序(绿色行的zorder 2,因此任何> 2都可以):

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

相关问题