- import numpy as np
- import matplotlib.pyplot as plt
- from matplotlib.animation import FuncAnimation
- fig=plt.figure()
- ax=fig.subplots()
-
- t=np.linspace(0,10,100)
- y=np.sin(t)
- ax.set_aspect(3)
- ax.plot(t,y,'--',c='gray')
- line=ax.plot(t,y,c='red')
-
- def update(i):#帧更新函数
- global t #直接用全局变量,也可以通过frames设置
- t+=0.2
- y=np.sin(t)
- line[0].set_ydata(y)
- return line
-
- ani=FuncAnimation(fig=fig,func=update,interval=100)
- ani.save('正弦曲线.gif')
-
- plt.show()