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.

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