这是一个FuncAnimation示例,我对其进行了修改以暂停鼠标单击。由于动画是由生成器函数驱动的,因此
simData,当全局变量
pause为True时,产生相同的数据会使动画显得暂停。
paused通过设置事件回调来切换的值:
def onClick(event): global pause pause ^= Truefig.canvas.mpl_connect('button_press_event', onClick)
import matplotlib.pyplot as pltimport numpy as npimport matplotlib.animation as animationpause = Falsedef simData(): t_max = 10.0 dt = 0.05 x = 0.0 t = 0.0 while t < t_max: if not pause: x = np.sin(np.pi*t) t = t + dt yield x, tdef onClick(event): global pause pause ^= Truedef simPoints(simData): x, t = simData[0], simData[1] time_text.set_text(time_template%(t)) line.set_data(t, x) return line, time_textfig = plt.figure()ax = fig.add_subplot(111)line, = ax.plot([], [], 'bo', ms=10)ax.set_ylim(-1, 1)ax.set_xlim(0, 10)time_template = 'Time = %.1f s'time_text = ax.text(0.05, 0.9, '', transform=ax.transAxes)fig.canvas.mpl_connect('button_press_event', onClick)ani = animation.FuncAnimation(fig, simPoints, simData, blit=False, interval=10, repeat=True)plt.show()
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)