FuncAnimation
なしでアニメーションすることは可能です。しかし、 "enivisioned function"の目的は本当に明確ではありません。アニメーションでは、時間は独立変数です。つまり、各時間ステップごとにプロットするための新しいデータを作成します。したがって、関数は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
のすべてのメリットがあります。