Coords 演示

如何通过连接到移动和单击事件来与绘图画布交互的示例

Coords 演示

  1. import sys
  2. import matplotlib.pyplot as plt
  3. import numpy as np
  4. t = np.arange(0.0, 1.0, 0.01)
  5. s = np.sin(2 * np.pi * t)
  6. fig, ax = plt.subplots()
  7. ax.plot(t, s)
  8. def on_move(event):
  9. # get the x and y pixel coords
  10. x, y = event.x, event.y
  11. if event.inaxes:
  12. ax = event.inaxes # the axes instance
  13. print('data coords %f %f' % (event.xdata, event.ydata))
  14. def on_click(event):
  15. # get the x and y coords, flip y from top to bottom
  16. x, y = event.x, event.y
  17. if event.button == 1:
  18. if event.inaxes is not None:
  19. print('data coords %f %f' % (event.xdata, event.ydata))
  20. binding_id = plt.connect('motion_notify_event', on_move)
  21. plt.connect('button_press_event', on_click)
  22. if "test_disconnect" in sys.argv:
  23. print("disconnecting console coordinate printout...")
  24. plt.disconnect(binding_id)
  25. plt.show()

下载这个示例