.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "gallery/event_handling/coords_demo.py" .. LINE NUMBERS ARE GIVEN BELOW. .. only:: html .. meta:: :keywords: codex .. note:: :class: sphx-glr-download-link-note :ref:`Go to the end ` to download the full example code .. rst-class:: sphx-glr-example-title .. _sphx_glr_gallery_event_handling_coords_demo.py: =========================== Mouse move and click events =========================== An example of how to interact with the plotting canvas by connecting to move and click events. .. note:: This example exercises the interactive capabilities of Matplotlib, and this will not appear in the static documentation. Please run this code on your machine to see the interactivity. You can copy and paste individual parts, or download the entire example using the link at the bottom of the page. .. GENERATED FROM PYTHON SOURCE LINES 17-45 .. image-sg:: /gallery/event_handling/images/sphx_glr_coords_demo_001.png :alt: coords demo :srcset: /gallery/event_handling/images/sphx_glr_coords_demo_001.png, /gallery/event_handling/images/sphx_glr_coords_demo_001_2_00x.png 2.00x :class: sphx-glr-single-img .. code-block:: Python import matplotlib.pyplot as plt import numpy as np from matplotlib.backend_bases import MouseButton t = 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): if event.inaxes: print(f'data coords {event.xdata} {event.ydata},', f'pixel coords {event.x} {event.y}') def on_click(event): if event.button is MouseButton.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() .. _sphx_glr_download_gallery_event_handling_coords_demo.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: coords_demo.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: coords_demo.py ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_