2017-08-11 90 views
0

我想不断更新绘图的一个属性(例如,随机化y值),然后偶尔更新它(例如,将所有y值为零)。使用FuncAnimation以两种不同方式绘制动画

只用一个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() 
+0

是的,它可以用一个FuncAnimation实例做到这一点。如果您展示问题的[mcve]并清楚地描述您遇到问题,您一定会得到答案。 – ImportanceOfBeingErnest

+0

@ImportanceOfBeingErnest增加了一个例子:) – janizer

回答

1

创建一个功能,这取决于一些条件,调用两种方法之一。使用此功能作为动画功能。

在下面的示例中,我们使用属性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() 
+0

那太优雅了!对我来说,我绝对是过分复杂的事情。非常感谢! – janizer

相关问题