肯定可以在没有FuncAnimation
的情况下生成动画。然而,“设定的功能”的目的并不十分清楚。在动画中,时间是自变量,即对于每个时间步,您都会生成一些新的数据以进行绘图或类似操作。因此该函数将以t
作为输入并返回一些数据。
import matplotlib.pyplot as plt
import numpy as np
def f(t):
x=np.random.rand(1)
y=np.random.rand(1)
return x,y
fig, ax = plt.subplots()
ax.set_xlim(0,1)
ax.set_ylim(0,1)
for t in range(100):
x,y = f(t)
# optionally clear axes and reset limits
#plt.gca().cla()
#ax.set_xlim(0,1)
#ax.set_ylim(0,1)
ax.plot(x, y, marker="s")
ax.set_title(str(t))
fig.canvas.draw()
plt.pause(0.1)
plt.show()
而且,目前尚不清楚为什么你会希望避免FuncAnimation
。同样的动画如上可以用FuncAnimation
产生如下:
import matplotlib.pyplot as plt
import matplotlib.animation
import numpy as np
def f(t):
x=np.random.rand(1)
y=np.random.rand(1)
return x,y
fig, ax = plt.subplots()
ax.set_xlim(0,1)
ax.set_ylim(0,1)
def update(t):
x,y = f(t)
# optionally clear axes and reset limits
#plt.gca().cla()
#ax.set_xlim(0,1)
#ax.set_ylim(0,1)
ax.plot(x, y, marker="s")
ax.set_title(str(t))
ani = matplotlib.animation.FuncAnimation(fig, update, frames=100)
plt.show()
没有太多的变化,你有相同数量的行,没有什么尴尬的看这里。
当动画变得更加复杂时,当您想要重复动画,想要使用blitting,或者想要将其导出到文件时,您还可以从FuncAnimation
获得所有好处。