动画情节在几秒钟后变慢,如何保持其快速运行?

时间:2019-04-23 13:42:06

标签: python-3.x matplotlib animation

我正在使用tkinter,matplotlib和动画功能构建一个GUI,当按下绘图按钮时,该功能应该显示3个不同的子图。这些子图必须实时更新,而不会降低速度,以确保应用程序平稳运行。 y轴是通过TCP连接到仿真器接收的数据,x轴应该是从按下按钮起的处理时间。

此外,我成功地绘制和显示了数据,并且基本上一切都在运行,但是在几秒钟后,绘制速度,线条绘制速度以及我认为帧速率都下降了,并且变得非常慢。当可视化足够的数据时,我无法找到一种开始将图形x轴向左移动的方法。

我在阅读了新方法之后尝试了一种新方法,并尝试使用它,但是我的图并没有显示出来,好像我的数据一开始就没有。

因此,如果有人可以帮助我更快地实现可视化并且可以移动,那么我将非常感谢您的帮助,可以帮助我指出正确方法并向我展示正在做错事情的任何帮助都将受到极大的赞赏……我我只是发布与绘图有关的代码部分。

import tkinter
import tkinter.font
import matplotlib
matplotlib.use("TkAgg")
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg
from matplotlib.backends._backend_tk import NavigationToolbar2Tk
from matplotlib.figure import Figure
import matplotlib.pyplot as plt
import matplotlib.animation as animation
from matplotlib import style
style.use('ggplot')

## the lists am plotting st is system temp rp is rotor position and rs is rotor speed time is the x-axis and value is the y-axis
stime_st = []
svalue_st = []
stime_rp = []
svalue_rp = []
stime_rs = []
svalue_rs = []


## this class creates the canvas figure and toolbar
class ploting(tkinter.Frame):
    def __init__(self, master):
        tkinter.Frame.__init__(self, master)
        self.master = master
        self.init_window()
    def init_window(self):
        global exit_bt
        self.master.title("Plot Display")
        self.pack(fill=tkinter.BOTH, expand=True) 
        self.canvas = FigureCanvasTkAgg(fig, master=self)
        self.toolbar = NavigationToolbar2Tk(self.canvas,self) 
        self.toolbar.update()
        close_bt = tkinter.Button(self,relief = 'raised', bg = '#668e99', font =myFont,text = 'Close', command = plot_close, width = 25)
        close_bt.place(relx = 0.999,rely = 0.984, anchor = 'e')
        exit_bt = tkinter.Button(self, font =myFont, bg = '#668e99',text = 'Exit Fullscreen', command = self.toggle, width = 25)
        exit_bt.place(relx = 0.82,rely = 0.984, anchor = 'e')
        self.canvas.get_tk_widget().pack(side= tkinter.BOTTOM, fill = tkinter.BOTH, expand = True) ## placing the canvas 
        self.canvas._tkcanvas.pack(side= tkinter.TOP, fill = tkinter.BOTH, expand = True)
        self.master.attributes("-fullscreen", True)        
    def exit_fs(self):
        self.master.attributes("-fullscreen", False)
    def enter_fs(self):
        self.master.attributes("-fullscreen", True)
    def toggle(self):
        if exit_bt.config('text')[-1] == 'Exit Fullscreen':
            self.exit_fs()
            exit_bt.config(text = 'Enter Fullscreen')
        else:
            self.enter_fs()
            exit_bt.config(text = 'Exit Fullscreen')

## this function creates the figure, subplots, lines and main window as well as assigning limits and labels
def plot():
    global plot_win
    global fig
    global rs
    global rp
    global st
    global line_st
    global line_rp
    global line_rs
    plt.ion()

    fig = Figure(figsize = (5,5), dpi = 100)
    rs = fig.add_subplot(311, position = [0.08,0.71,0.9,0.22])
    rp = fig.add_subplot(312, position = [0.08,0.39,0.9,0.22])
    st = fig.add_subplot(313, position = [0.08,0.07,0.9,0.22])

    line_rs, = rs.plot(stime_rs,svalue_rs,'k-')   
    rs.set_xlabel("Time(s)")
    rs.set_ylabel("Speed(RPM)")
    rs.set_title("Rotor Speed")

    line_rp, = rp.plot(stime_rp,svalue_rp,'k-')
    rp.set_xlabel("Time(s)")
    rp.set_ylabel("Position(DEG)")
    rp.set_title("Rotor Position")
    rp.set_ylim(0,360)

    line_st, = st.plot(stime_st,svalue_st,'k-')
    st.set_xlabel("Time(s)")
    st.set_ylabel("Tempreture(C°)")
    st.set_title("System Tempreture")
    st.set_ylim(0,75)    
    plot_bt.config(state = 'disabled') ## this is irrelevant its from the main page of the GUI
    newTime = time.process_time() ## time in seconds from the moment the plot is drawn
    plot_win = tkinter.Tk()
    plot_win.geometry("1280x800")
    plot_win.protocol("WM_DELETE_WINDOW",plot_close)
    app = ploting(plot_win) ## am creating the figure and canvas... etc
    ani = animation.FuncAnimation(fig, animate, interval = 100, blit = True)
## ani is calling the animation function with blit to increase the speed and reduce unwanted drawings
    app.mainloop() 

## in this function if a connection to tcp is made that means i have data and in that case it will append the time and values into the list and set the ydata and xdata

def animate(i):
    if conn_tcp.is_set():
        stime_rs.append(newTime)
        stime_rp.append(newTime)
        stime_st.append(newTime)
        svalue_rs.append(value_rs)
        svalue_rp.append(value_rp)
        svalue_st.append(value_st)        
        line_rs.set_ydata(svalue_rs)
        line_rp.set_ydata(svalue_rp)
        line_st.set_ydata(svalue_st)
        line_rs.set_xdata(stime_rs)
        line_rp.set_xdata(stime_rp)
        line_st.set_xdata(stime_st)
    return line_rs,line_rp,line_st,

0 个答案:

没有答案