Tkinter matplotlib图作为弹出窗口

时间:2014-05-22 09:00:07

标签: python matplotlib tkinter

我有一个应用程序,应该在校准一些数据后显示残留错误。主窗口是matplotlib画布,显示原始和校准数据(定义如下):

# Begins the actual Application 
class App():
    def __init__(self,master):
        self.fig = plt.Figure()
        self.canvas = FigureCanvasTkAgg(self.fig, master = master)
        self.toolbar = NavigationToolbar2TkAgg(self.canvas, root)
        self.canvas.get_tk_widget().pack()
        self.canvas.draw()

我希望在新窗口中显示剩余错误,但到目前为止,我只是成功将新绘图附加到主窗口(见下图),我不知道为什么或如何从这里开始

enter image description here

我当前的错误图代码如下(此函数是Class App()的一部分:

####################
# WORK IN PROGRESS #
####################
def displayError(self,errors):
    """ This function displays a graph to show the error
    after calibration.
    """
    self.fig = plt.Figure()
    self.canvas = FigureCanvasTkAgg(self.fig, master = root)
    self.canvas.show()
    self.canvas.get_tk_widget().pack()
    x_array = []
    y_array = []
    labels = []
    for counter,i in enumerate(errors):
        x_array.append(counter)
        labels.append(i[0])
        y_array.append(i[2])
    self.axes = self.fig.add_subplot(111)
    self.axes.scatter(x_array,y_array)
        self.axes.tick_params(axis='x',which='both',bottom='off',top='off',labelbottom='off')
    self.axes.set_ylabel('Mass error (ppm)')
    self.axes.hlines(0,x_array[1]-0.5,x_array[-1]+0.5)
    self.axes.set_xlim(x_array[1]-0.5,x_array[-1]+0.5)
    for counter,i in enumerate(errors):
        self.axes.annotate(i[1],(x_array[counter]+0.1,y_array[counter]+0.1))
        self.axes.vlines(x_array[counter],0,y_array[counter],linestyles='dashed')
    #self.canvas.draw()
####################
# WORK IN PROGRESS #
####################

1 个答案:

答案 0 :(得分:2)

我认为这是因为您的FigureCanvasTkAgg都具有相同的master。你能为第二个情节产生一个新的tk.Toplevel吗?即它会是这样的:

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

import tkinter as tk
root = tk.Tk()

x = np.arange(0, 10, 1)
y = np.arange(0, 10, 1)

fig1 = plt.Figure()
canvas1 = FigureCanvasTkAgg(fig1, master=root)
canvas1.show()
canvas1.get_tk_widget().pack(side=tk.TOP, fill=tk.BOTH, expand=1.0)
ax = fig1.add_subplot(111)
ax.plot(x,y)
canvas1.draw()

root2 = tk.Toplevel()
fig2 = plt.Figure()
canvas2 = FigureCanvasTkAgg(fig2, master=root2)
canvas2.show()
canvas2.get_tk_widget().pack(side=tk.TOP, fill=tk.BOTH, expand=1.0)
ax = fig2.add_subplot(111)
ax.plot(x,2*y)
canvas2.draw()

所以第二个窗口是'popup'。