按键演示

显示如何连接到按键事件

按键演示

  1. import sys
  2. import numpy as np
  3. import matplotlib.pyplot as plt
  4. def press(event):
  5. print('press', event.key)
  6. sys.stdout.flush()
  7. if event.key == 'x':
  8. visible = xl.get_visible()
  9. xl.set_visible(not visible)
  10. fig.canvas.draw()
  11. # Fixing random state for reproducibility
  12. np.random.seed(19680801)
  13. fig, ax = plt.subplots()
  14. fig.canvas.mpl_connect('key_press_event', press)
  15. ax.plot(np.random.rand(12), np.random.rand(12), 'go')
  16. xl = ax.set_xlabel('easy come, easy go')
  17. ax.set_title('Press a key')
  18. plt.show()

下载这个示例