Title: Detect which figure was closed with Matplotlib
Tags: python;matplotlib
Question: Im using matplotlib embedded in a GUI application using the qt4 backend.

I need to store a list of figures that the user plots and keeps open ie multiple figures are able to be plotted separately with different clicks of the plot button.

However when the user closes a figure I need to remove it from the list of figures.

How do I tell which figure is closed?

I am using the event handler to detect a figure has been closed but I cannot tell which one.

Here is some trivial example code:

```from __future__ import print_function
import matplotlib.pyplot as plt
import numpy as np

figs = []
figNum = len(figs)

def handle_close(evt):
    evt.canvas.figure.axes[0].has_been_closed = True
    print ('Closed Figure')

fig = plt.figure()
figs.append(fig)


ax = figs[figNum].add_subplot(1, 1, 1)
ax.has_been_closed = False
# fig2 = plt.figure()
# ax2 = fig2.add_axes([0.15, 0.1, 0.7, 0.3])

t = np.arange(0.0, 1.0, 0.01)
s = np.sin(2*np.pi*t)
line, = ax.plot(t, s, color='blue', lw=2)


# fig2 = plt.figure()
# figs.append(fig2)
# fig2.canvas.mpl_connect('close_event', handle_close)
fig.canvas.mpl_connect('close_event', handle_close)

plt.show()
print (ax.has_been_closed)
```

Comment: @tcaswell: Are you suggesting creating a list and appending figure numbers to the list? If so then when closing the figure how would I know which figure number or label to pop from the list if Im managing it myself?

Comment: If you are embedding I would suggest _not_ using pyplot and doing the figure state management your self.  You can then use the standard qt singnals/slots for this sort of thing.

Comment: The qt window closed signal

Here is the accepted answer: The ```evt``` that is passed to your handler has a member ```canvas```, which in turn has a member ```figure```, which of course is the figure the event refers to.

So if you want to remove that figure from your list, you would do

```figs.remove(evt.canvas.figure)
```

If you want to get the figure number, you access 

```evt.canvas.figure.number
```

and you could get the name from

```evt.canvas.figure._label
```

However, the leading ```_``` probably means you shouldn't rely on that.

Comment for this answer: @Daniel can you please explain how that would be used to return the figure name/number or some sort of reference as to which figure was closed ie there may be multiple figures open
Comment for this answer: @DanielSk Thanks so much. Thats what I was looking for. Not entirely clear in the documentation
Comment for this answer: @RicDavimes: I hope this makes the answer more clear. If not, please update your question so we can see what exactly you are having trouble with.
Comment for this answer: Welcome to SO. Generally it is considered good practice to explain your answer and not just add links. You can refer to [this guide](http://stackoverflow.com/help/how-to-answer)


{'source': 'StackExchangeDataset', 'subset': 'stackoverflow.com', 'metadata': {'AnswerCount': 1, 'CommentCount': 3, 'ContentLicense': 'CC BY-SA 3.0', 'CreationDate': '2015:00:06/26/15', 'FavoriteCount': None, 'PostTypeId': 1, 'Tags': '<python><matplotlib>', 'Title': 'Detect which figure was closed with Matplotlib', 'ViewCount': 1516, 'id': 31069350, 'user_AboutMe': '', 'user_AccountId': 5427419.0, 'user_DisplayName': 'RicDavimes', 'user_Reputation': 315.0, 'user_Views': 31.0, 'user_id': 4319267.0}}