目前我正在研究涉及行星围绕太阳运动的 2 个身体问题。为了做到这一点,为了做动画,我使用FuncAnimation
了matplotlib.animation
. 在此代码中x_graph
并y_graph
包含运动的值。要完成一圈,帧数必须为 6282。但要做到这一点,输出需要很长时间才能完成,并且动画也不流畅。
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')
请假设您已经有了 x_graph 和 y_graph 的值