Guest User

Untitled

a guest
Nov 25th, 2017
52
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.89 KB | None | 0 0
  1. def random_walk_animated_2D(n, how_many = 1):
  2.  
  3. possible_jumps = np.array([[0, 1], [1, 0], [-1, 0], [0, -1]])
  4. where_to_go = np.random.randint(4, size = n)
  5. temp = possible_jumps[where_to_go, :]
  6. x = np.array([[0, 0]])
  7. temp1 = np.concatenate((x, temp), axis = 0)
  8. trajectory = np.cumsum(temp1, axis = 0)
  9.  
  10. fig = plt.figure()
  11. ax = plt.axes(xlim = (np.amin(trajectory, axis = 0)[0], np.amax(trajectory, axis = 0)[0]),
  12. ylim = (np.amin(trajectory, axis = 0)[1], np.amax(trajectory, axis = 0)[1]))
  13. line, = ax.plot([], [], lw = 2)
  14.  
  15. def init():
  16. line.set_data([], [])
  17. return line,
  18.  
  19. def animate(i):
  20. line.set_data(trajectory[i, 0], trajectory[i, 1])
  21. return line,
  22.  
  23. anim = animation.FuncAnimation(fig, animate, init_func = init,
  24. frames = 200, interval = 30, blit = True)
  25. plt.show()
Add Comment
Please, Sign In to add comment