Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/19.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 3.x 在python中为tkinter更新动画中的绘图_Python 3.x_Animation_Tkinter - Fatal编程技术网

Python 3.x 在python中为tkinter更新动画中的绘图

Python 3.x 在python中为tkinter更新动画中的绘图,python-3.x,animation,tkinter,Python 3.x,Animation,Tkinter,我有一个复杂的程序,无法在动画中更新曲线,因此我简化了程序,并在之前发布了类似的帖子,但该示例简化得太多,无法代表实际问题。所以这个例子得到了修正,但是实际的程序仍然不起作用。在这里,我做了一些更改,并再次发布了代码。 程序将尝试在时间=3秒和5秒时更新直线两次,问题是 如果我没有定义xlim和ylim,那么如果更新超出范围,我们就看不到它;如果我定义xlim和ylim,那么每次使用canvas.draw()时,动画将绘制两条不同的曲线。我还尝试了subplot1.clear()和replot,

我有一个复杂的程序,无法在动画中更新曲线,因此我简化了程序,并在之前发布了类似的帖子,但该示例简化得太多,无法代表实际问题。所以这个例子得到了修正,但是实际的程序仍然不起作用。在这里,我做了一些更改,并再次发布了代码。
程序将尝试在时间=3秒和5秒时更新直线两次,问题是 如果我没有定义xlim和ylim,那么如果更新超出范围,我们就看不到它;如果我定义xlim和ylim,那么每次使用canvas.draw()时,动画将绘制两条不同的曲线。我还尝试了subplot1.clear()和replot,但都是一样的。当我设置blit=False时,一切正常,但我必须使用blit=true。看起来动画在内存中保留了两组数据,有没有办法删除旧数据

from multiprocessing import Process
import matplotlib
matplotlib.use("TkAgg")
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg
##, NavigationToolbar2TkAgg
from matplotlib.figure import Figure
import matplotlib.animation as animation
from matplotlib import style
import tkinter as tk
from tkinter import ttk
from tkinter import *
import matplotlib.pyplot as plt
import numpy as np
import time
f = Figure(figsize=(6,6), dpi=100)

subplot_1 = f.add_subplot(211)
subplot_2 = f.add_subplot(212)
a_count = 0
b_count = 0
LARGE_FONT= ("Verdana", 12)
style.use("ggplot")
count_plot = 0
first_replot,second_replot = 0,0
start_time = 0
draw_replot = 0
a_t = [1,2,3,4,5]
a_T = np.ones(5)
canvas_draw_timer = 0
def replot(update_flag): 
    global a_t, count_plot,theory_line,a_T 
    if update_flag == 1:
        a_t = [3,4,5,6,7]
        a_T = np.ones(5)*2 
        theory_line[0].set_data(a_t,a_T)
    elif update_flag == 2:
        a_t = [5,6,7,8,9]
        a_T = np.ones(5)*1.5 
        theory_line[0].set_data(a_t,a_T)
    else:
        theory_line = subplot_1.plot(a_t,a_T,'c-')
    canvas.draw()

def animate(i):
    global a_count,b_count,first_replot,second_replot,canvas_draw_timer
    time1 = np.random.rand(2, 25)  

    data = np.random.rand(2, 25)*2  
    #first time update the canvas 
    if (time.time() - start_time > 3) and (first_replot == 0):
        replot(1)
        first_replot = 1
    #second time update the canvas
    if (time.time() - start_time > 5) and (second_replot == 0):
        replot(2)
        second_replot = 1
    #refresh the canvas per second, just example, many other places need to use canvas.draw() later
    if time.time() - canvas_draw_timer > 1: 
        canvas.draw()
        canvas_draw_timer = time.time()

    a = subplot_1.scatter(time1,data,c='blue',s=2)

    return a, 

class home(tk.Tk):

    def __init__(self, *args, **kwargs):        
        tk.Tk.__init__(self, *args, **kwargs)
        tk.Tk.wm_title(self, "Graph ")       
        self.geometry("1000x1000")

        container = tk.Frame(self)
        container.pack(side="top", fill="both", expand = True)
        container.grid_rowconfigure(0, weight=1)
        container.grid_columnconfigure(0, weight=1)

        self.frames = {}
        F=Graph
        frame=Graph(container, self)
        self.frames[F] = frame
        frame.grid(row=0, column=0, sticky="nsew")
        self.show_frame(Graph)

    def show_frame(self, cont):
        frame = self.frames[cont]
        frame.tkraise()
    def get_frame(self, frame_class):
        return self.frames[frame_class]
##     
class Graph(tk.Frame):
    def __init__(self, parent, controller):
        global canvas
        tk.Frame.__init__(self, parent)
        label = tk.Label(self, text=" ", font=LARGE_FONT)
        label.pack(pady=120,padx=10)
        collectbtn=Button(self,text='collect',command=self.clickstart)
        collectbtn.place(x=200,y=100)
        canvas = FigureCanvasTkAgg(f, self)
        canvas.get_tk_widget().pack(side=tk.BOTTOM, fill=tk.BOTH, expand=True)
        canvas._tkcanvas.pack(side=tk.TOP, fill=tk.BOTH, expand=True)

    def clickstart(self):
        global theory_line,start_time
        theory_line = subplot_1.plot(a_t,a_T,'c-')
        #set the large x and y range first, otherwise blit = true could not plot out of range plots
        f.axes[0].set_xlim([0,10])
        f.axes[0].set_ylim([0,5])
        start_time = time.time()
        aniplot_photon = animation.FuncAnimation(f, animate, blit=True,interval=100)
        canvas.draw()

app = home()
app.mainloop()