Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 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()
Add Comment
Please, Sign In to add comment