0

我有 matplotlib 不清除内存的问题(我假设)。我试过在谷歌上四处张望,但无济于事。我希望每次关闭 matplotlib 图形(由“PLOT”按钮调用)后清除内存。提前致谢。

################
#IMPORT MODULES#
################

from Tkinter import *
import matplotlib.pyplot as plt

#####
#GUI#
#####

class Application(Frame):
    #read entry values and estimate the plot(s)
    def run_brussel(self):
        #read entry values
        t_start = float(self.t_start_entry.get())
        t_end = float(self.t_end_entry.get())
        t_diff = float(self.t_diff_entry.get())
        k1 = float(self.k1_entry.get())
        k2 = float(self.k2_entry.get())
        k3 = float(self.k3_entry.get())
        k4 = float(self.k4_entry.get())
        A = float(self.A_entry.get())
        B = float(self.B_entry.get())
        X = float(self.X_entry.get())
        Y = float(self.Y_entry.get())

        mode = self.mode.get()

        #estimate the values of the Brusselator X and Y functions
        brussel = brussel_func(t_start, t_end, t_diff, k1, k2, k3, k4, A, B, X, Y)

        t_list = brussel[0]
        X_list = brussel[1]
        Y_list = brussel[2]

        #plot the functions
        plot_brussel(mode, t_list, X_list, Y_list)

    #define labels, entries, radio buttons and buttons
    def createWidgets(self):
        self.t_start_label = Label(self, text="Start time:")
        self.t_start_label.grid(row=0, column=0, sticky=W)
        self.t_start_entry = Entry(self)
        self.t_start_entry.delete(0, END)
        self.t_start_entry.insert(0, "0.0")
        self.t_start_entry.grid(row=0, column=1)

        self.t_end_label = Label(self, text="End time:")
        self.t_end_label.grid(row=1, column=0, sticky=W)
        self.t_end_entry = Entry(self)
        self.t_end_entry.delete(0, END)
        self.t_end_entry.insert(0, "100.0")
        self.t_end_entry.grid(row=1, column=1)

        self.t_diff_label = Label(self, text="Time precision:")
        self.t_diff_label.grid(row=2, column=0, sticky=W)
        self.t_diff_entry = Entry(self)
        self.t_diff_entry.delete(0, END)
        self.t_diff_entry.insert(0, "0.0001")
        self.t_diff_entry.grid(row=2, column=1)

        self.k1_label = Label(self, text="Constant k1 value:")
        self.k1_label.grid(row=3, column=0, sticky=W)
        self.k1_entry = Entry(self)
        self.k1_entry.delete(0, END)
        self.k1_entry.insert(0, "1.0")
        self.k1_entry.grid(row=3, column=1)

        self.k2_label = Label(self, text="Constant k2 value:")
        self.k2_label.grid(row=4, column=0, sticky=W)
        self.k2_entry = Entry(self)
        self.k2_entry.delete(0, END)
        self.k2_entry.insert(0, "1.0")
        self.k2_entry.grid(row=4, column=1)

        self.k3_label = Label(self, text="Constant k3 value:")
        self.k3_label.grid(row=5, column=0, sticky=W)
        self.k3_entry = Entry(self)
        self.k3_entry.delete(0, END)
        self.k3_entry.insert(0, "1.0")
        self.k3_entry.grid(row=5, column=1)

        self.k4_label = Label(self, text="Constant k4 value:")
        self.k4_label.grid(row=6, column=0, sticky=W)
        self.k4_entry = Entry(self)
        self.k4_entry.delete(0, END)
        self.k4_entry.insert(0, "1.0")
        self.k4_entry.grid(row=6, column=1)

        self.A_label = Label(self, text="Initial A concentration:")
        self.A_label.grid(row=7, column=0, sticky=W)
        self.A_entry = Entry(self)
        self.A_entry.delete(0, END)
        self.A_entry.insert(0, "1.0")
        self.A_entry.grid(row=7, column=1)

        self.B_label = Label(self, text="Initial B concentration:")
        self.B_label.grid(row=8, column=0, sticky=W)
        self.B_entry = Entry(self)
        self.B_entry.delete(0, END)
        self.B_entry.insert(0, "2.5")
        self.B_entry.grid(row=8, column=1)

        self.X_label = Label(self, text="Initial X concentration:")
        self.X_label.grid(row=9, column=0, sticky=W)
        self.X_entry = Entry(self)
        self.X_entry.delete(0, END)
        self.X_entry.insert(0, "0.0")
        self.X_entry.grid(row=9, column=1)

        self.Y_label = Label(self, text="Initial Y concentration:")
        self.Y_label.grid(row=10, column=0, sticky=W)
        self.Y_entry = Entry(self)
        self.Y_entry.delete(0, END)
        self.Y_entry.insert(0, "0.0")
        self.Y_entry.grid(row=10, column=1)

        self.mode = IntVar()
        self.modus_operandi_label = Label(self, text="Plot type:")
        self.modus_operandi_label.grid(row=11, column=0, sticky=W)
        self.modus_operandi_button1 = Radiobutton(self, text="X : Y", variable=self.mode, value=True).grid(row=11, column=1, sticky=W)
        self.modus_operandi_button2 = Radiobutton(self, text="X, Y : time", variable=self.mode, value=False).grid(row=12, column=1, sticky=W)

        self.plot = Button(self)
        self.plot["text"] = "PLOT",
        self.plot["command"] = self.run_brussel
        self.plot.grid(row=13, column=0, columnspan=2, sticky=N+S+W+E)

        self.QUIT = Button(self)
        self.QUIT["text"] = "QUIT"
        self.QUIT["fg"]   = "red"
        self.QUIT["command"] =  self.quit_pressed
        self.QUIT.grid(row=14, column=0, columnspan=2, sticky=N+S+W+E)

    #invoke the window
    def __init__(self, master=None):
        Frame.__init__(self, master)
        self.grid()
        self.createWidgets()

    #destroy the window
    def quit_pressed(self):
        quit()

######################
#FUNCTION DEFINITIONS#
######################

#Brusselator X and Y values estimation
def brussel_func(t_start, t_end, t_diff, k1, k2, k3, k4, A, B, X, Y):
    t = t_start
    t_list = []
    t_list.append(0)
    X_list = [X]
    Y_list = [Y]
    step = 0

    while t < t_end:
        X_diff = t_diff * (k1 * A - k2 * B * X_list[step] + k3 * X_list[step]**2 * Y_list[step] - k4 * X_list[step])
        Y_diff = t_diff * (k2 * B * X_list[step] - k3 * X_list[step]**2 * Y_list[step])
        X_list.append(X)
        Y_list.append(Y)
        t_list.append(t)
        t += t_diff
        X += X_diff
        Y += Y_diff
        step += 1

    return [t_list, X_list, Y_list]

#plot       
def plot_brussel(mode, t_list, X_list, Y_list):
    p = []

    #plots X : Y    
    if mode == True:
        p = plt.plot(X_list, Y_list)
        plt.xlabel('x')
        plt.ylabel('y')

    #plots X, Y : time
    else:
        p = plt.plot(t_list, X_list, label="X")
        p += plt.plot(t_list, Y_list, label="Y")
        plt.legend(loc="upper left", bbox_to_anchor=(1,1))
        plt.xlabel('time')

    #displays chosen plot
    plt.show(p)
    plt.clf(p)
    plt.close(p)

def main():
    root = Tk()
    root.wm_title("Brusselator")
    app = Application(master=root)
    app.mainloop()

#############
#SCRIPT BODY#
#############

if __name__ == '__main__':
    main()
4

0 回答 0