2017-10-19 74 views
0

這是我的其他question關於tkinter中Matplotlib功能的跟進。我試圖開發一個程序,當我訓練模型時打開多個包含圖的窗口。這些值作爲字典存儲在aveCR中,每個鍵存儲多個數組。對於每個鍵,我將這些數組繪製在一個新窗口中,因此使用for循環打開一個新窗口(不知道這是否是好的做法!)Matplotlib/tkinter:在打開多個窗口時選擇圖例的事件

我遇到的問題是每個窗口的圖例。打開/關閉行的功能只能在最後一個窗口中使用,我希望在打開的每個新窗口中都可以使用此功能。我知道self.lined被for循環的最後一幅圖覆蓋,但我不確定是否應該將它添加到for循環。

我爲aveCR添加了下面的虛擬數字,因此代碼可以運行。任何意見在接近這個將不勝感激!

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 
     # Frame for the training values 
     buttonFrame = tk.Frame(master) 
     buttonFrame.pack() 

     self.buttonGenerate = tk.Button(master=buttonFrame, 
             text='Train', 
             command=self.train) 
     self.buttonGenerate.grid(column=2,row=0) 


    def train(self): 

     aveCR = {0:{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])}, 
      1:{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 = {0: ['A', 'AB'], 1: ['A', 'AB']} 

     for i in range(len(aveCR)): 
      t = tk.Toplevel(self.master) 
      # Frame for the plot 
      plotFrame = tk.Frame(t) 
      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[i]) 
      for j in range(len(aveCR[i])):   
       X = range(0,len(aveCR[i][j])) 
       lines[j], = self.ax.plot(X,aveCR[i][j],label=legend[i][j]) 
      leg = self.ax.legend(bbox_to_anchor=(0., 1.02, 1., .102), loc=3,ncol=2, borderaxespad=0.) 

     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

這可能主要是一個設計問題。我會建議爲每個繪圖窗口使用一個類。然後,App類可以根據需要實例化多個這些繪圖窗口,並提供各自的數據作爲參數。每個圖表窗口管理圖例和事件本身。

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

import Tkinter as tk 

class Plotwindow(): 
    def __init__(self, master, data, legend): 
     t = tk.Toplevel(master) 
     # Frame for the plot 
     plotFrame = tk.Frame(t) 
     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(data) 
     for j in range(len(data)):   
      X = range(0,len(data[j])) 
      lines[j], = self.ax.plot(X,data[j],label=legend[j]) 
     leg = self.ax.legend(bbox_to_anchor=(0., 1.02, 1., .102), loc=3,ncol=2, borderaxespad=0.) 
     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() 

class App: 
    def __init__(self,master): 
     self.master = master 
     # Frame for the training values 
     buttonFrame = tk.Frame(master) 
     buttonFrame.pack() 

     self.buttonGenerate = tk.Button(master=buttonFrame, 
             text='Train', 
             command=self.train) 
     self.buttonGenerate.grid(column=2,row=0) 

    def train(self): 

     aveCR = {0:{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])}, 
      1:{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 = {0: ['A', 'AB'], 1: ['A', 'AB']} 

     self.windows = [] 
     for i in range(len(aveCR)): 
      data = aveCR[i] 
      self.windows.append(Plotwindow(self.master,data, legend[i])) 


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

感謝您的幫助! –

相關問題