how to update curve plot in animation ( tkinter) of python

穿精又带淫゛_ 提交于 2021-01-29 17:06:37

问题


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.

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()

回答1:


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

  1. start_time won't be reset if you don't click 'collect', so it won't replot every 5 seconds.
    def clickstart(self):
        ....
        start_time = time.time()
        aniplot_photon = animation.FuncAnimation(f, animate, blit=True,interval=100)
        ....
  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'.

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



来源:https://stackoverflow.com/questions/62394564/how-to-update-curve-plot-in-animation-tkinter-of-python

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!