2016-06-15 9 views
0

私はPythonとTkinterでプロジェクトをやっています。私はデータの配列をプロットすることができますし、私はマウスでクリックすると注釈をプロットに追加する機能も実装しましたが、今追加したすべての注釈のリストが必要です。それを持つ方法はありますか? これは注釈を追加するために、私の関数である:あなたがMatplotLib軸ごとにすべての注釈を取得

def onclick(self, event): 

    clicked = [] 
    key = event.key 
    x = event.xdata 
    y = event.ydata 

    x_d = min(range(len(self.x_data)), key=lambda i: abs(self.x_data[i] - x)) 
    local_coord = self.x_data[x_d - 6:x_d + 6] 
    x_1 = max(local_coord) 
    indx = np.where(self.x_data == x_1)[0][0] 
    y_1 = self.y_data[indx] 



    if key == "v": 
     self.ax.annotate("{0}nm".format(int(x_1)), size=25, 
         bbox=dict(boxstyle="round",fc="0.8"), 
         xy=(x_1, y_1), xycoords='data', 
         xytext=(x_1, y_1+50), textcoords='data', 
         arrowprops=dict(arrowstyle="-|>", 
             connectionstyle="bar,fraction=0", 
             )) 

    self.canvas.draw() 

答えて

2

すべてax子供をループ可能性があり、子供がタイプmatplotlib.text.Annotationであるかどうかを確認:

for child in ax.get_children(): 
    if isinstance(child, matplotlib.text.Annotation): 
     print("bingo") # and do something 

それとも、あなたはリストをしたい場合:

annotations = [child for child in ax.get_children() if isinstance(child, matplotlib.text.Annotation)] 
+0

ありがとう、私が探していたものです –

関連する問題