如何在python的动画(tkinter)中更新曲线图

2024-09-29 19:27:28 发布

您现在位置:Python中文网/ 问答频道 /正文

从下面的代码中,我可以通过单击“replot”按钮来更新该行。我必须使用set_data()并绘制一个新的绘图,否则旧的绘图仍然在这里

但是,我希望程序自动更新animate()中的曲线,我使用5秒控件触发replot()函数,但失败了。如果我不使用blit=True,只使用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)
f = Figure(figsize=(6,6), dpi=100)

subplot_1 = f.add_subplot(211)
subplot_2 = f.add_subplot(212)
#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 = 0
start_time = 0
a_t = [1,2,3,4,5]
a_T = np.ones(5)
def replot(): 
    global a_t, count_plot,theory_line,a_T 

    a_T = np.ones(5)*2
    #f.axes[0].clear
    #theory_line[0].remove()
    theory_line[0].set_data(a_t,a_T)
    #theory_line.clear()
    #theory_line = subplot_1.plot(a_t,a_T,'g-')
    #subplot_1.draw
    canvas.draw

def animate(i):
    global a_count,b_count,first_replot

    time1 = np.random.rand(2, 25)  

    data = np.random.rand(2, 25)*2   
    if (time.time() - start_time > 5) and (first_replot == 0):
        replot()
        first_replot = 1
    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)
        collectbtn=Button(self,text='replot',command=self.clickreplot)
        collectbtn.place(x=280,y=100)
        canvas = FigureCanvasTkAgg(f, self)
##        canvas.draw()
        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-')
        #animate(0)
        start_time = time.time()
        aniplot_photon = animation.FuncAnimation(f, animate, blit=True,interval=100)
        #aniplot_photon = animation.FuncAnimation(f, animate, interval=100)
        canvas.draw()
    def clickreplot(self):
        replot()
        canvas.draw()
app = home()
app.mainloop()

Tags: fromimportselftruetimematplotlibdefcount
1条回答
网友
1楼 · 发布于 2024-09-29 19:27:28

不确定编程逻辑,但我认为可能至少有三个问题

  1. 如果不单击“收集”,则不会重置开始时间,因此不会每5秒回复一次
    def clickstart(self):
        ....
        start_time = time.time()
        aniplot_photon = animation.FuncAnimation(f, animate, blit=True,interval=100)
        ....
  1. first_replot最初设置为0,单击“collect”并经过5秒后,first_replot的值将始终为1。这意味着函数“animate”中不再有replot

  2. 函数“replot”最后一行的canvas.draw应为canvas.draw()

相关问题 更多 >

    热门问题