计时器

使用通用计时器对象的简单示例。这用于更新图中标题的时间。

计时器示例

  1. import matplotlib.pyplot as plt
  2. import numpy as np
  3. from datetime import datetime
  4. def update_title(axes):
  5. axes.set_title(datetime.now())
  6. axes.figure.canvas.draw()
  7. fig, ax = plt.subplots()
  8. x = np.linspace(-3, 3)
  9. ax.plot(x, x ** 2)
  10. # Create a new timer object. Set the interval to 100 milliseconds
  11. # (1000 is default) and tell the timer what function should be called.
  12. timer = fig.canvas.new_timer(interval=100)
  13. timer.add_callback(update_title, ax)
  14. timer.start()
  15. # Or could start the timer on first figure draw
  16. #def start_timer(evt):
  17. # timer.start()
  18. # fig.canvas.mpl_disconnect(drawid)
  19. #drawid = fig.canvas.mpl_connect('draw_event', start_timer)
  20. plt.show()

下载这个示例