{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "\n# Mouse move and click events\n\nAn example of how to interact with the plotting canvas by connecting to move\nand click events.\n\n

Note

\n\nThis example exercises the interactive capabilities of Matplotlib, and this\nwill not appear in the static documentation. Please run this code on your\nmachine to see the interactivity.\n\nYou can copy and paste individual parts, or download the entire example\nusing the link at the bottom of the page.\n\n
\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "import matplotlib.pyplot as plt\nimport numpy as np\n\nfrom matplotlib.backend_bases import MouseButton\n\nt = np.arange(0.0, 1.0, 0.01)\ns = np.sin(2 * np.pi * t)\nfig, ax = plt.subplots()\nax.plot(t, s)\n\n\ndef on_move(event):\n if event.inaxes:\n print(f'data coords {event.xdata} {event.ydata},',\n f'pixel coords {event.x} {event.y}')\n\n\ndef on_click(event):\n if event.button is MouseButton.LEFT:\n print('disconnecting callback')\n plt.disconnect(binding_id)\n\n\nbinding_id = plt.connect('motion_notify_event', on_move)\nplt.connect('button_press_event', on_click)\n\nplt.show()" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.12.15" } }, "nbformat": 4, "nbformat_minor": 0 }