Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import numpy as np
- import matplotlib.pyplot as plt
- from matplotlib.animation import FuncAnimation
- fig = plt.figure(figsize=(7, 7))
- ax = fig.add_axes([0, 0, 1, 1], frameon=False)
- ax.set_xlim(-1, 1), ax.set_xticks([])
- ax.set_ylim(-1, 1), ax.set_yticks([])
- # Set initial coordinates
- x0 = 0
- y0 = 0
- # Set current coordinates
- x_curr = x0
- y_curr = y0
- # Initialize array of coordinates history
- x_ac = [x0]
- y_ac = [y0]
- # Initialize destination coordinates
- xd = x0
- yd = y0
- # Set number and index of steps
- n_steps = 20
- step_i = 0
- # Set waiting parameters
- waiting = True
- n_waiting = 5
- wait_i = 0
- def update(frame_number):
- global step_i, waiting, wait_i, x_curr, y_curr, x0, y0, xd, yd
- if not waiting:
- if step_i < n_steps:
- # Task 7.1.1
- x_curr = 2*np.random.rand()-1
- y_curr = 2*np.random.rand()-1
- step_i += 1
- else:
- # Task 7.1.1
- x_curr = xd
- y_curr = yd
- step_i = 0
- waiting = True
- # Accumulate coordinates
- x_ac.append(x_curr)
- y_ac.append(y_curr)
- else:
- if wait_i <= n_waiting:
- wait_i += 1
- else:
- wait_i = 0
- # Task 7.1.1
- x0 = xd
- y0 = yd
- xd = 2*np.random.rand()-1
- yd = 2*np.random.rand()-1
- waiting = False
- # Plot animation
- ax.clear()
- plt.plot(x_ac, y_ac)
- plt.plot(x_curr,y_curr,'o')
- ax.set_xlim(-1, 1), ax.set_xticks([])
- ax.set_ylim(-1, 1), ax.set_yticks([])
- # Construct the animation, using the update function as the animation director.
- animation = FuncAnimation(fig, update, interval=10)
- plt.show()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement