2017-07-12 51 views
0

我希望我最後一次點擊的註釋框成爲最上面的註釋框,但是目前它按照它添加的順序顯示(如果它是最後添加的,它會在最上面)。如何設置註釋框(matplotlib)的z-index

我使用這個代碼來自ImportanceOfBeingEarnest對this問題的回答作爲例子。

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

您應該能夠設置爲ZORDER

ab.set_zorder(10) 

對於那些可見的,並且較低的ZORDER換了別人。