003《Python数据分析、挖掘与可视化(第2版)》/例9-33.py
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.animation as animation

fig, ax = plt.subplots()
ax.set_xlim(-np.pi, np.pi)
ax.set_ylim(-1.2, 1.2)
x, y = [], []                                     # 存放曲线上采样点坐标的列表
line, = plt.plot(x, y, 'bo', markersize=4)        # 绘制曲线上的采样点

def init():
    x.clear()                                     # 删除所有采样点坐标,更新曲线
    y.clear()
    line.set_data(x, y)
    return line,

def update(frame):
    x.append(frame)                               # 每次更新曲线时增加一个采样点
    y.append(np.cos(frame))
    line.set_data(x, y)
    return line,

anim = animation.FuncAnimation(fig, init_func=init, func=update,
                               frames=np.arange(-np.pi,np.pi,0.1),
                               interval=10, repeat=True,
                               repeat_delay=500, blit=True)
plt.show()