Advertisement
Guest User

y7y

a guest
Jun 24th, 2018
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.72 KB | None | 0 0
  1. import numpy as np
  2. import matplotlib.pyplot as plt
  3. from matplotlib.animation import FuncAnimation
  4.  
  5. fig = plt.figure(figsize=(7, 7))
  6. ax = fig.add_axes([0, 0, 1, 1], frameon=False)
  7. ax.set_xlim(-1, 1), ax.set_xticks([])
  8. ax.set_ylim(-1, 1), ax.set_yticks([])
  9.  
  10. # Set initial coordinates
  11. x0 = 0
  12. y0 = 0
  13.  
  14. # Set current coordinates
  15. x_curr = x0
  16. y_curr = y0
  17.  
  18. # Initialize array of coordinates history
  19. x_ac = [x0]
  20. y_ac = [y0]
  21.  
  22. # Initialize destination coordinates
  23. xd = x0
  24. yd = y0
  25.  
  26. # Set number and index of steps
  27. n_steps = 20
  28. step_i = 0
  29.  
  30. # Set waiting parameters
  31. waiting = True
  32. n_waiting = 5
  33. wait_i = 0
  34.  
  35. def update(frame_number):
  36.     global step_i, waiting, wait_i, x_curr, y_curr, x0, y0, xd, yd
  37.  
  38.     if not waiting:
  39.         if step_i < n_steps:
  40.             # Task 7.1.1
  41.             x_curr = 2*np.random.rand()-1
  42.             y_curr = 2*np.random.rand()-1
  43.             step_i += 1
  44.         else:
  45.             # Task 7.1.1
  46.             x_curr = xd
  47.             y_curr = yd
  48.             step_i = 0
  49.             waiting = True
  50.         # Accumulate coordinates
  51.         x_ac.append(x_curr)
  52.         y_ac.append(y_curr)
  53.  
  54.     else:
  55.         if wait_i <= n_waiting:
  56.             wait_i += 1
  57.         else:
  58.             wait_i = 0
  59.             # Task 7.1.1
  60.             x0 = xd
  61.             y0 = yd
  62.             xd = 2*np.random.rand()-1
  63.             yd = 2*np.random.rand()-1
  64.             waiting = False
  65.  
  66.     # Plot animation
  67.     ax.clear()
  68.     plt.plot(x_ac, y_ac)
  69.     plt.plot(x_curr,y_curr,'o')
  70.     ax.set_xlim(-1, 1), ax.set_xticks([])
  71.     ax.set_ylim(-1, 1), ax.set_yticks([])
  72.  
  73.  
  74. # Construct the animation, using the update function as the animation director.
  75. animation = FuncAnimation(fig, update, interval=10)
  76. plt.show()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement