没有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()
没有太大变化,行数相同,这里没有什么特别尴尬的地方。
此外,当动画变得更复杂、想要重复动画、想要使用位图传输或想要将其导出到文件时,您可以从 FuncAnimation 获得所有好处。