使用FuncAnimation以两种不同的方式设置绘图动画

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

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

我想连续更新绘图的一个属性(例如,将y值随机化),然后每隔一段时间以不同的方式更新它(例如,将所有y值设置为零)。在

是否可以只使用一个FuncAnimation例程来完成此操作?在

或者,最好的方法是什么?在

编辑:例如,先做“updatePlot()”,然后再做“otherUpdatePlot”:

from matplotlib import pyplot as plt
import matplotlib.animation as animation
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg
import numpy as np
import Tkinter as tk


class AnimatedPlot(tk.Frame):
    def __init__(self, master=None, **kwargs):
        tk.Frame.__init__(self, master, **kwargs)
        self.fig = plt.Figure()
        self.ax1 = self.fig.add_subplot(111)
        self.line, = self.ax1.plot([], [], lw=2)
        self.canvas = FigureCanvasTkAgg(self.fig, master=self)
        self.canvas.show()
        self.canvas.get_tk_widget().grid(row=1, column=0, columnspan=2, rowspan=8)
        self.ax1.set_ylim(0,20) #only the visible limits of the grad
        self.ax1.set_xlim(0,20) ##the whole dataset swill be longer                         
        self.startPlot()

    def startPlot(self):
        self.xdata = np.linspace(0, 20, 20) 
        self.ydata = np.linspace(0, 20, 20)    
        self.anim = animation.FuncAnimation(
            self.fig,
            self.updatePlot,
            repeat=True) #here is the animation routine that right now only runs one updatePlot function
        self.anim._start()
        print("Running")

    def updatePlot(self,i): #the function to be animated most of the time               
        self.ydata = np.random.randint(20, size=20)
        self.line.set_data(self.xdata, self.ydata)        
        return self.line,

    def otherUpdatePlot(self,i): #to be animated once in a while
        self.ydata = np.zeros(shape=(1, 20))
        self.line.set_data(self.xdata, self.ydata)
        return self.line,

def main():
    root = tk.Tk()
    app = AnimatedPlot(root)
    app.pack()
    root.mainloop()

if __name__ == '__main__':
    main()

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

创建一个函数,该函数根据某些条件调用这两个方法之一。将此函数用作动画功能。在

在下面的例子中,我们使用一个属性self.once_in_a_while = 6使绘图每6帧都为零。在

from matplotlib import pyplot as plt
import matplotlib.animation as animation
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg
import numpy as np
import Tkinter as tk


class AnimatedPlot(tk.Frame):
    def __init__(self, master=None, **kwargs):
        tk.Frame.__init__(self, master, **kwargs)
        self.fig = plt.Figure()
        self.ax1 = self.fig.add_subplot(111)
        self.line, = self.ax1.plot([], [], lw=2)
        self.canvas = FigureCanvasTkAgg(self.fig, master=self)
        self.canvas.show()
        self.canvas.get_tk_widget().grid(row=1, column=0, columnspan=2, rowspan=8)
        self.ax1.set_ylim(0,20) #only the visible limits of the grad
        self.ax1.set_xlim(0,20) ##the whole dataset swill be longer                         
        self.startPlot()

    def startPlot(self):
        self.xdata = np.linspace(0, 20, 20) 
        self.ydata = np.linspace(0, 20, 20) 
        self.once_in_a_while = 6
        self.anim = animation.FuncAnimation(
            self.fig,
            self.update,
            repeat=True) 
        self.anim._start()
        print("Running")

    def update(self, i):
        if i%self.once_in_a_while:
            r = self.updatePlot(i)
        else:
            r = self.otherUpdatePlot(i)
        return r

    def updatePlot(self,i): #the function to be animated most of the time               
        self.ydata = np.random.randint(20, size=20)
        self.line.set_data(self.xdata, self.ydata)        
        return self.line,

    def otherUpdatePlot(self,i): #to be animated once in a while
        self.ydata = np.zeros(shape=(1, 20))
        self.line.set_data(self.xdata, self.ydata)
        return self.line,

def main():
    root = tk.Tk()
    app = AnimatedPlot(root)
    app.pack()
    root.mainloop()

if __name__ == '__main__':
    main()

相关问题 更多 >