Connecting Matplotlib Figure Events

MatplotlibMatplotlibBeginner
Practice Now

This tutorial is from open-source community. Access the source code

Introduction

Matplotlib is a popular data visualization library in Python. In this tutorial, you will learn how to connect events that occur when a figure closes. This is useful when you want to perform an action after closing a figure.

VM Tips

After the VM startup is done, click the top left corner to switch to the Notebook tab to access Jupyter Notebook for practice.

Sometimes, you may need to wait a few seconds for Jupyter Notebook to finish loading. The validation of operations cannot be automated because of limitations in Jupyter Notebook.

If you face issues during learning, feel free to ask Labby. Provide feedback after the session, and we will promptly resolve the problem for you.


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL matplotlib(("`Matplotlib`")) -.-> matplotlib/BasicConceptsGroup(["`Basic Concepts`"]) matplotlib(("`Matplotlib`")) -.-> matplotlib/AdvancedTopicsGroup(["`Advanced Topics`"]) python(("`Python`")) -.-> python/BasicConceptsGroup(["`Basic Concepts`"]) python(("`Python`")) -.-> python/DataStructuresGroup(["`Data Structures`"]) python(("`Python`")) -.-> python/FunctionsGroup(["`Functions`"]) python(("`Python`")) -.-> python/ModulesandPackagesGroup(["`Modules and Packages`"]) python(("`Python`")) -.-> python/PythonStandardLibraryGroup(["`Python Standard Library`"]) python(("`Python`")) -.-> python/DataScienceandMachineLearningGroup(["`Data Science and Machine Learning`"]) matplotlib/BasicConceptsGroup -.-> matplotlib/importing_matplotlib("`Importing Matplotlib`") matplotlib/BasicConceptsGroup -.-> matplotlib/figures_axes("`Understanding Figures and Axes`") matplotlib/AdvancedTopicsGroup -.-> matplotlib/event_handling("`Event Handling`") python/BasicConceptsGroup -.-> python/variables_data_types("`Variables and Data Types`") python/DataStructuresGroup -.-> python/tuples("`Tuples`") python/FunctionsGroup -.-> python/function_definition("`Function Definition`") python/ModulesandPackagesGroup -.-> python/importing_modules("`Importing Modules`") python/PythonStandardLibraryGroup -.-> python/data_collections("`Data Collections`") python/DataScienceandMachineLearningGroup -.-> python/data_visualization("`Data Visualization`") python/FunctionsGroup -.-> python/build_in_functions("`Build-in Functions`") subgraph Lab Skills matplotlib/importing_matplotlib -.-> lab-48602{{"`Connecting Matplotlib Figure Events`"}} matplotlib/figures_axes -.-> lab-48602{{"`Connecting Matplotlib Figure Events`"}} matplotlib/event_handling -.-> lab-48602{{"`Connecting Matplotlib Figure Events`"}} python/variables_data_types -.-> lab-48602{{"`Connecting Matplotlib Figure Events`"}} python/tuples -.-> lab-48602{{"`Connecting Matplotlib Figure Events`"}} python/function_definition -.-> lab-48602{{"`Connecting Matplotlib Figure Events`"}} python/importing_modules -.-> lab-48602{{"`Connecting Matplotlib Figure Events`"}} python/data_collections -.-> lab-48602{{"`Connecting Matplotlib Figure Events`"}} python/data_visualization -.-> lab-48602{{"`Connecting Matplotlib Figure Events`"}} python/build_in_functions -.-> lab-48602{{"`Connecting Matplotlib Figure Events`"}} end

Import Matplotlib and Define the on_close Function

In this step, we will import Matplotlib and define the on_close function that will be called when the figure is closed. The function will simply print a message to the console.

import matplotlib.pyplot as plt

def on_close(event):
    print('Closed Figure!')

Create a Figure and Connect the Close Event

In this step, we will create a figure and connect the close event to the on_close function defined in Step 1. This is done using the mpl_connect method of the figure's canvas.

fig = plt.figure()
fig.canvas.mpl_connect('close_event', on_close)

Add Text to the Figure

In this step, we will add text to the figure to prompt the user to close it. This is done using the text method of Matplotlib.

plt.text(0.35, 0.5, 'Close Me!', dict(size=30))

Show the Figure

In this step, we will show the figure using the show method of Matplotlib.

plt.show()

Summary

In this tutorial, you learned how to connect events that occur when a figure closes using Matplotlib. You can use this to perform an action after closing a figure.

Other Matplotlib Tutorials you may like