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()
是的,它可以用一個FuncAnimation實例做到這一點。如果您展示問題的[mcve]並清楚地描述您遇到問題,您一定會得到答案。 – ImportanceOfBeingErnest
@ImportanceOfBeingErnest增加了一個例子:) – janizer