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

import sysimport matplotlib.pyplot as pltimport numpy as npt = np.arange(0.0, 1.0, 0.01)s = np.sin(2 * np.pi * t)fig, ax = plt.subplots()ax.plot(t, s)def on_move(event):# get the x and y pixel coordsx, y = event.x, event.yif event.inaxes:ax = event.inaxes # the axes instanceprint('data coords %f %f' % (event.xdata, event.ydata))def on_click(event):# get the x and y coords, flip y from top to bottomx, y = event.x, event.yif event.button == 1:if event.inaxes is not None:print('data coords %f %f' % (event.xdata, event.ydata))binding_id = plt.connect('motion_notify_event', on_move)plt.connect('button_press_event', on_click)if "test_disconnect" in sys.argv:print("disconnecting console coordinate printout...")plt.disconnect(binding_id)plt.show()
