Tkinter嵌入式Matplotlib显示问题

时间:2014-12-12 03:04:37

标签: python matplotlib tkinter python-3.4

我正在使用径向基础神经网络进行曲线拟合。在跟随this guide之后,我成功地将我的情节嵌入到tkinter画布中 然而,我的问题是当我运行我的代码时,除非我展开或收缩GUI窗口,否则绘图不会显示在画布中。我在这里搜索了相关的问题,但我找不到一个具体解决这个问题。我使用的是python 3.4.2。

from tkinter import *
import math
import numpy as np
from numpy import *
import scipy
import scipy.linalg
import matplotlib
matplotlib.use('TkAgg')
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg, NavigationToolbar2TkAgg
from matplotlib.figure import Figure


class RadialBasisNetwork:

    def  __init__(self):

        window = Tk()
        window.title("RBF Curve Fitting")
        self.f = Figure(figsize=(4,3), dpi=100)
        self.canvas = FigureCanvasTkAgg(self.f, master=window)
        self.canvas.show()
        self.canvas.get_tk_widget().pack(side=LEFT, fill=BOTH, expand=1)
        self.canvas._tkcanvas.pack(side=LEFT, fill=BOTH, expand=1)
        frame0 = Frame(window)
        frame0.pack(fill=BOTH, expand=1)
        title = Label(frame0, text = "RBF Network Controller", font = "Times 12 bold")
        title.grid(row = 1, column = 1)

    def Plot(self):

        x = np.linspace(x_0, x_1, num = steps) # generate domain of x
        plot_title = "model of " + model    
        a = self.f.add_subplot(111)
        a.plot(x,ypred, 'r--') # plot x against predicted y
        a.plot(x,y)            # plot x against actual y (target)
        a.legend(["Fit", "Target"], loc = "best", frameon = False, labelspacing = 0)
        a.set_title(plot_title) 
        a.set_ylabel('Y axis')
        a.set_xlabel('X axis')

RadialBasisNetwork()

0 个答案:

没有答案