【发布时间】:2017-11-25 11:14:47
【问题描述】:
我正在用 Python 编写第二维随机游走。我想动画它如何“成长”。我想使用来自matplotlib 的animation.FuncAnimation,但不幸的是它不能如我所愿。但是没有错误,我在 iPython 控制台中使用了%matplotlib tk。
我的代码:
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()
稍后我想添加在图中生成多个随机游走的可能性(我的意思是我希望它们同时生成)。我该怎么做?
【问题讨论】:
标签: python animation matplotlib