Note

Go to the endto download the full example code.

Mouse move and click events#

An example of how to interact with the plotting canvas by connecting to moveand click events.

Note

This example exercises the interactive capabilities of Matplotlib, and thiswill not appear in the static documentation. Please run this code on yourmachine to see the interactivity.

You can copy and paste individual parts, or download the entire exampleusing the link at the bottom of the page.

coords demo
importmatplotlib.pyplotaspltimportnumpyasnpfrommatplotlib.backend_basesimportMouseButtont=np.arange(0.0,1.0,0.01)s=np.sin(2*np.pi*t)fig,ax=plt.subplots()ax.plot(t,s)defon_move(event):ifevent.inaxes:print(f'data coords{event.xdata}{event.ydata},',f'pixel coords{event.x}{event.y}')defon_click(event):ifevent.buttonisMouseButton.LEFT:print('disconnecting callback')plt.disconnect(binding_id)binding_id=plt.connect('motion_notify_event',on_move)plt.connect('button_press_event',on_click)plt.show()

Gallery generated by Sphinx-Gallery