Note
Go to the endto download the full example code.
Close event#
Example to show connecting events that occur when the figure closes.
Note
This example exercises the interactive capabilities of Matplotlib, and thiswill not appear in the static documentation. Please run this code on yourmachine to see the interactivity.
You can copy and paste individual parts, or download the entire exampleusing the link at the bottom of the page.

importmatplotlib.pyplotaspltdefon_close(event):print('Closed Figure!')fig=plt.figure()fig.canvas.mpl_connect('close_event',on_close)plt.text(0.35,0.5,'Close Me!',dict(size=30))plt.show()