Guest User

Planet Animation Code

a guest
Nov 7th, 2022
43
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.20 KB | Source Code | 0 0
  1. import matplotlib.pyplot as plt
  2. from matplotlib.animation import FuncAnimation
  3. import numpy as np
  4.  
  5. plt.style.use('dark_background')
  6.  
  7. n_points = 1000
  8. A, B = 5.5, 6
  9. SIZE = 20
  10. theta = np.linspace(0, 2 * np.pi, n_points)
  11.  
  12. frame = plt.gca()
  13. fig, axs = plt.subplots(1, 2)
  14. fig.suptitle('Same orbit, different reference frames')
  15.  
  16. for ax in axs:
  17.     ax.set_xlim(-7, 7)
  18.     ax.set_ylim(-7, 7)
  19.     ax.get_xaxis().set_visible(False)
  20.     ax.get_yaxis().set_visible(False)
  21.  
  22. x = A * np.sin(theta)
  23. y = B * np.cos(theta)
  24.  
  25. xx = -A * np.sin(theta)
  26. yy = -B * np.cos(theta)
  27.  
  28. orbit1 = axs[0].plot([], [], 'b.', markersize=SIZE)[0]
  29. orbit2 = axs[1].plot([], [], 'r.', markersize=SIZE)[0]
  30. axs[0].plot(0, 0, '.', markersize=SIZE, color="red")
  31. axs[1].plot(0, 0, '.', markersize=SIZE, color="blue")
  32. #plt.grid(True, lw=0.3)
  33. axs[0].plot(x, y, 'b-')
  34. axs[1].plot(xx, yy, 'r-')
  35.  
  36. def animate(i):
  37.     orbit1.set_data(x[i], y[i])
  38.     orbit2.set_data(xx[i], yy[i])
  39.     return orbit1, orbit2
  40.  
  41. anim = FuncAnimation(fig, animate, frames=n_points, interval=10, repeat=False, blit=True)
  42.  
  43. frame.axes.get_xaxis().set_visible(False)
  44. frame.axes.get_yaxis().set_visible(False)
  45. anim.save('planets.gif', writer='pillow')
  46. plt.show()
Advertisement
Add Comment
Please, Sign In to add comment