2017-12-15 10 views
1

범례에서 이벤트 픽킹을 만들었지 만 드래그 가능한 범례를 동시에 구현하는 데 어려움이 있습니다. 두 사람은 이벤트 따기 구현이 줄을 제거 할 때 드래그 전설이 작업을 수행으로 충돌하는 것 : 당신은 드래그 전설을 사용하기 때문에하는 pick_event 범례에서 발생 가능성은 물론 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도 전설에서 일어난다.

이제 픽업 된 아티스트가 사전의 줄 중 하나 인 경우 사용자 지정 선택 만 수행 할 수 있습니다. 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()