.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "gallery/event_handling/figure_axes_enter_leave.py" .. LINE NUMBERS ARE GIVEN BELOW. .. only:: html .. meta:: :keywords: codex .. note:: :class: sphx-glr-download-link-note Click :ref:`here ` to download the full example code .. rst-class:: sphx-glr-example-title .. _sphx_glr_gallery_event_handling_figure_axes_enter_leave.py: ================================== Figure/Axes enter and leave events ================================== Illustrate the figure and Axes enter and leave events by changing the frame colors on enter and leave. .. 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-53 .. image-sg:: /gallery/event_handling/images/sphx_glr_figure_axes_enter_leave_001.png :alt: mouse hover over figure or axes to trigger events :srcset: /gallery/event_handling/images/sphx_glr_figure_axes_enter_leave_001.png, /gallery/event_handling/images/sphx_glr_figure_axes_enter_leave_001_2_0x.png 2.0x :class: sphx-glr-single-img .. code-block:: default import matplotlib.pyplot as plt def on_enter_axes(event): print('enter_axes', event.inaxes) event.inaxes.patch.set_facecolor('yellow') event.canvas.draw() def on_leave_axes(event): print('leave_axes', event.inaxes) event.inaxes.patch.set_facecolor('white') event.canvas.draw() def on_enter_figure(event): print('enter_figure', event.canvas.figure) event.canvas.figure.patch.set_facecolor('red') event.canvas.draw() def on_leave_figure(event): print('leave_figure', event.canvas.figure) event.canvas.figure.patch.set_facecolor('grey') event.canvas.draw() fig, axs = plt.subplots(2, 1) fig.suptitle('mouse hover over figure or axes to trigger events') fig.canvas.mpl_connect('figure_enter_event', on_enter_figure) fig.canvas.mpl_connect('figure_leave_event', on_leave_figure) fig.canvas.mpl_connect('axes_enter_event', on_enter_axes) fig.canvas.mpl_connect('axes_leave_event', on_leave_axes) plt.show() .. _sphx_glr_download_gallery_event_handling_figure_axes_enter_leave.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: figure_axes_enter_leave.py ` .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: figure_axes_enter_leave.ipynb ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_