1""" 2=========== 3Coords Demo 4=========== 5 6An example of how to interact with the plotting canvas by connecting 7to move and click events 8""" 9from __future__ import print_function 10import sys 11import matplotlib.pyplot as plt 12import numpy as np 13 14t = np.arange(0.0, 1.0, 0.01) 15s = np.sin(2 * np.pi * t) 16fig, ax = plt.subplots() 17ax.plot(t, s) 18 19 20def on_move(event): 21 # get the x and y pixel coords 22 x, y = event.x, event.y 23 24 if event.inaxes: 25 ax = event.inaxes # the axes instance 26 print('data coords %f %f' % (event.xdata, event.ydata)) 27 28 29def on_click(event): 30 # get the x and y coords, flip y from top to bottom 31 x, y = event.x, event.y 32 if event.button == 1: 33 if event.inaxes is not None: 34 print('data coords %f %f' % (event.xdata, event.ydata)) 35 36 37binding_id = plt.connect('motion_notify_event', on_move) 38plt.connect('button_press_event', on_click) 39 40if "test_disconnect" in sys.argv: 41 print("disconnecting console coordinate printout...") 42 plt.disconnect(binding_id) 43 44plt.show() 45