我希望我最后一次点击的注解框位于顶部,但目前,它以添加的顺序显示(如果它是最后添加的,它将位于顶部)。
我使用ImportanceOfBeingEarnest对这个问题的回答中的代码作为示例。
import matplotlib.pyplot as plt
import numpy as np
x = np.arange(20)
y = np.sin(x)
fig, ax = plt.subplots()
line1 = ax.scatter(x[:10],y[:10],20, c="red", picker=True, marker='*')
line2 = ax.scatter(x[10:20],y[10:20],20, c="green", picker=True, marker='^')
ia = lambda i: plt.annotate("Annotate {}".format(i), (x[i],y[i]), visible=False)
img_annotations = [ia(i) for i in range(len(x))]
lce = [False]
def show_ROI(event):
tlce=False
for annot, line in zip([img_annotations[:10],img_annotations[10:20]], [line1, line2]):
if line.contains(event)[0]:
lce[0]=tlce=True
ind = line.contains(event)[1]["ind"]
print('onpick3 scatter:', ind)
ab = annot[ind[0]]
ab.set_visible(True)
if not tlce:
for ab in img_annotations:
ab.set_visible(False)
lce[0] = False
fig.canvas.draw_idle()
fig.canvas.mpl_connect('button_press_event', show_ROI)
plt.show()
1条答案
按热度按时间kmbjn2e31#
您应该能够将zorder设置为
对于那些可见的,和一个较低的zorder为其他人。