如何在python的动画(tkinter)中更新曲线图 [英] how to update curve plot in animation ( tkinter) of python

查看:108
本文介绍了如何在python的动画(tkinter)中更新曲线图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

从以下代码中,我可以通过单击重新绘制"按钮来更新该行.我必须同时使用set_data()并绘制一个新图,否则旧图仍在这里.

from the following code, I could update the line by clicking the "replot" button. I have to use both set_data() and also plot a new plot, otherwise the old plot is still here.

但是,我希望程序自动更新animate()内部的曲线,我使用5秒钟的控件来触发replot()函数,但是失败了.如果我不使用blit = True,仅使用blit = False,那么一切都很好.那么我的问题是如何更新条件触发的动画曲线,还需要保持blit = True?

However, I want the program automatically to update the curve inside of animate(), I use a 5 secs control to trigger replot() function, but it fails. if I do not use blit=True, just use blit=False, then everything is fine. Then my question is how to update the curve in animation triggered by condition, and also need to keep 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()

推荐答案

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

Not sure about the programming logic, but I think there maybe at least three issues.

  1. 如果您不单击收集",则不会重置开始时间,因此不会每5秒重新绘制一次.

    def clickstart(self):
        ....
        start_time = time.time()
        aniplot_photon = animation.FuncAnimation(f, animate, blit=True,interval=100)
        ....

  1. first_replot最初设置为0,在单击收集"并经过5秒钟后,first_replot的值将始终为1.这意味着函数动画"中不再有replot.

  1. first_replot initially set to 0, after 'collect' clicked and 5 seconds passed, then the value of first_replot will be always 1. It means no more replot in function 'animate'.

canvas.draw应该是canvas.draw().

canvas.draw at last line of function 'replot', should be canvas.draw().

这篇关于如何在python的动画(tkinter)中更新曲线图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆