import numpy as np
import matplotlib.pyplot as plt
import matplotlib.animation as animation
##定义一些常量,比如轨道的半径,球心位置。
x0,y0 = 0,0
r = 10
r_ball = 0.5
T = 5
##定义画布
fig,ax = plt.subplots()
ax.set_xlim(-r*1.2,r*1.2)
ax.set_ylim(-r*1.2,r*1.2)
ax.set_aspect('equal') #画布成正方形
ax.axis('off') #不显示坐标轴
##定义圆
circle = plt.Circle((x0,y0),r_ball,color='y',fill=True)
circle_guidao = plt.Circle((x0,y0),r,color='red',fill=False)
ax.add_artist(circle)
ax.add_artist(circle_guidao)
##定义更新函数,更新每一帧圆的位置
def update(frame):
x = x0+(r-r_ball)*np.cos(2*np.pi*frame/T)
y = y0+(r-r_ball)*np.sin(2*np.pi*frame/T)
circle.set_center((x,y))
return circle,
# 创建动画
anim = animation.FuncAnimation(fig, update, frames=np.arange(0, 10, 0.1), interval=50,blit=True)
anim.save('fig.gif', writer='pillow', fps=10)
# 显示动画
# plt.show()
plt.close()