Currently I am working on 2 body problem which involves the motion of planet around the sun. In order to do so, in order to do animation I've used FuncAnimation
of matplotlib.animation
. In this code x_graph
and y_graph
contains the values for the motion. To complete one revolution, the frames has to be 6282. But in order to do so, the output takes a lot of time to complete and the animation is also not smooth.
fig = plt.figure()
p1 = fig.add_subplot(111)
l1, = p1.plot([],[])
l2, = p1.plot([-0.8],[0],marker= 'o')
print(func(0.98, -np.pi, 0, 0, 0.001))
plt.xlim(-1.2,1.5)
plt.ylim(-1.5,1.5)
plt.xlabel('x axis')
plt.ylabel('y axis')
plt.title('Planet\'s orbit')
def polar_animator(i):
l1.set_data(x_graph[:i], y_graph[:i])
return l1,
ani = animation.FuncAnimation(fig, polar_animator, frames= len(x_graph), interval=1, blit=True)
ani.save('planet.mp4', writer= 'ffmpeg')
Please assume that you already have values for x_graph and y_graph