2017-12-15 4 views
1

凡例にイベントピッキングを作成しましたが、ドラッグ可能な凡例を同時に実装するのが難しいです。イベントピッキングを実装して、この行を削除するときにドラッグ可能な伝説が仕事をするように2つの競合に思える:self.canvas.mpl_connect('pick_event', self.onpick) イベントピッキング機能を使用している間、matplotlibのドラッグ可能な凡例

import matplotlib.pyplot as plt 
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg 
import numpy as np 

import tkinter as tk 

class App: 
    def __init__(self,master): 
     self.master = master 

     aveCR = {0:np.array([.582,1.081,1.507,1.872,2.180]),1:np.array([2.876,6.731,1.132,1.305,1.217])}  
     legend = ['A', 'AB'] 

     plotFrame = tk.Frame(master) 
     plotFrame.pack() 

     f = plt.Figure() 
     self.ax = f.add_subplot(111) 
     self.canvas = FigureCanvasTkAgg(f,master=plotFrame) 
     self.canvas.show() 
     self.canvas.get_tk_widget().pack() 
     self.canvas.mpl_connect('pick_event', self.onpick) 

     # Plot 
     lines = [0] * len(aveCR) 
     for i in range(len(aveCR)):   
      X = range(len(aveCR[i])) 
      lines[i], = self.ax.plot(X,aveCR[i],label=legend[i]) 

     # Legend 
     leg = self.ax.legend(bbox_to_anchor=(0., 1.02, 1., .102), loc=3,ncol=2, borderaxespad=0.) 
     if leg: 
      leg.draggable() 

     self.lined = dict() 
     for legline, origline in zip(leg.get_lines(), lines): 
      legline.set_picker(5) # 5 pts tolerance 
      self.lined[legline] = origline 

    def onpick(self, event): 
     # on the pick event, find the orig line corresponding to the 
     # legend proxy line, and toggle the visibility 
     legline = event.artist 
     origline = self.lined[legline] 
     vis = not origline.get_visible() 
     origline.set_visible(vis) 
     # Change the alpha on the line in the legend so we can see what lines 
     # have been toggled 
     if vis: 
      legline.set_alpha(1.0) 
     else: 
      legline.set_alpha(0.2) 
     self.canvas.draw() 

root = tk.Tk() 
root.title("hem") 
app = App(root) 
root.mainloop() 

答えて

0

あなたはドラッグ可能な凡例を使用しているため、pick_eventは伝説に発生する可能性はもちろんあります。実際には、伝説の中のいずれかの行で起こっているすべてのpick_eventもまた伝説で起こります。

ここでは、選択したアーティストが辞書の行の1つである場合にのみ、カスタムピッキングが行われるようにしたいと考えています。 event.artistself.lined辞書に含まれているかどうかを問い合わせることで、これが起こることを確認できます。

 
    def onpick(self, event): 
     if event.artist in self.lined.keys(): 
      legline = event.artist 
      origline = self.lined[legline] 
      vis = not origline.get_visible() 
      origline.set_visible(vis) 
      # Change the alpha on the line in the legend so we can see what lines 
      # have been toggled 
      if vis: 
       legline.set_alpha(1.0) 
      else: 
       legline.set_alpha(0.2) 
      self.canvas.draw_idle() 
関連する問題