matplotlib 如何设置注解框的z索引

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

我希望我最后一次点击的注解框位于顶部,但目前,它以添加的顺序显示(如果它是最后添加的,它将位于顶部)。
我使用ImportanceOfBeingEarnest对这个问题的回答中的代码作为示例。

  1. import matplotlib.pyplot as plt
  2. import numpy as np
  3. x = np.arange(20)
  4. y = np.sin(x)
  5. fig, ax = plt.subplots()
  6. line1 = ax.scatter(x[:10],y[:10],20, c="red", picker=True, marker='*')
  7. line2 = ax.scatter(x[10:20],y[10:20],20, c="green", picker=True, marker='^')
  8. ia = lambda i: plt.annotate("Annotate {}".format(i), (x[i],y[i]), visible=False)
  9. img_annotations = [ia(i) for i in range(len(x))]
  10. lce = [False]
  11. def show_ROI(event):
  12. tlce=False
  13. for annot, line in zip([img_annotations[:10],img_annotations[10:20]], [line1, line2]):
  14. if line.contains(event)[0]:
  15. lce[0]=tlce=True
  16. ind = line.contains(event)[1]["ind"]
  17. print('onpick3 scatter:', ind)
  18. ab = annot[ind[0]]
  19. ab.set_visible(True)
  20. if not tlce:
  21. for ab in img_annotations:
  22. ab.set_visible(False)
  23. lce[0] = False
  24. fig.canvas.draw_idle()
  25. fig.canvas.mpl_connect('button_press_event', show_ROI)
  26. plt.show()
kmbjn2e3

kmbjn2e31#

您应该能够将zorder设置为

  1. ab.set_zorder(10)

对于那些可见的,和一个较低的zorder为其他人。

相关问题