私はPythonで2番目の次元を無作為にコーディングしています。私はそれが "成長"する方法をアニメーション化したいと思います。私はmatplotlib
からanimation.FuncAnimation
を使いたかったのですが、残念ながらそれはうまくいきません。しかし、エラーはありません。私は%matplotlib tk
をiPythonコンソールに使用しました。
マイコード:エラーはありませんが、動作しないアニメーション[Python]
def random_walk_animated_2D(n, how_many = 1):
possible_jumps = np.array([[0, 1], [1, 0], [-1, 0], [0, -1]])
where_to_go = np.random.randint(4, size = n)
temp = possible_jumps[where_to_go, :]
x = np.array([[0, 0]])
temp1 = np.concatenate((x, temp), axis = 0)
trajectory = np.cumsum(temp1, axis = 0)
fig = plt.figure()
ax = plt.axes(xlim = (np.amin(trajectory, axis = 0)[0], np.amax(trajectory, axis = 0)[0]),
ylim = (np.amin(trajectory, axis = 0)[1], np.amax(trajectory, axis = 0)[1]))
line, = ax.plot([], [], lw = 2)
def init():
line.set_data([], [])
return line,
def animate(i):
line.set_data(trajectory[i, 0], trajectory[i, 1])
return line,
anim = animation.FuncAnimation(fig, animate, init_func = init,
frames = 200, interval = 30, blit = True)
plt.show()
後で、プロット内に複数のランダムウォークを生成する可能性を追加したいと思います(私はそれらを同時に生成したいと思います)。どうしたらいいですか?
残念ながらまだ何もありません:( – Hendrra
コード自体を修正した場合、アニメーションが表示されない場合は、アニメーションへの参照が緩和されているためにアニメーションが返されます。 'plt.show()'が必要です。 – ImportanceOfBeingErnest