Matplotlib / tkinter:打开多个窗口时在图例上拾取事件

时间:2017-10-19 19:46:07

标签: python matplotlib tkinter

这是我对tkinter中Matplotlib功能的其他question的跟进。我正在尝试开发一个程序,在我训练模型时打开包含绘图的多个窗口。这些值作为字典存储在aveCR中,每个键存储多个数组。对于每个键,我在一个新窗口中绘制数组,因此使用for循环打开一个新窗口(不确定这是否是一个好习惯!)

我遇到的问题是每个窗口的图例。打开/关闭线的功能仅在最终窗口中可用,我希望在每个打开的新窗口上都可以使用此功能。我知道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()

1 个答案:

答案 0 :(得分: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()