Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson

This lesson is for members only. Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson

Hint: You can adjust the default video playback speed in your account settings.
Hint: You can set your subtitle preferences in your account settings.
Sorry! Looks like there’s an issue with video playback 🙁 This might be due to a temporary outage or because of a configuration issue with your browser. Please refer to our video player troubleshooting guide for assistance.

The Figures Behind the Scenes

Each time you call plt.subplots() or the less frequently used plt.figure(), which creates a figure with no axes, you’re creating a new figure object that matplotlib sneakily keeps around in the background.

Earlier, you saw the concept of a current figure and current axes. By default, these are the most recently created figure and axes objects in memory. Start by using subplots() to grab your figure and axes objects:

Python
>>> import matplotlib.pyplot as plt
>>> fig1, ax1 = plt.subplots()

00:00 Each time you call plt.subplots() or the less frequently used plt.figure(), which creates a Figure with no Axes, you are creating a new Figure object that Matplotlib sneakily keeps around in the background.

00:18 Earlier, we alluded to the concept of a current Figure and current Axes. By default, these are the most recently created Figure and Axes objects in memory.

00:32 I’ll start by using the subplots() function to grab our Figure and Axes objects. We can use the built-in id() function to see the address of this object, just like this. Earlier, we saw how pyplot, or plt, uses its gcf(), or get current figure function, to implicitly track the current Figure in memory.

00:59 I’ll pass that into the id() function, and we see that we get the exact same ID back. This tells us that the Figure object we created is the current Figure being tracked. Let’s grab new Figure and Axes objects.

01:17 I want to see if the current Figure being tracked is now this new one so I’ll write id(fig2) == id(plt.gcf()).

01:32 And it is! fig2 is now the current Figure being tracked by pyplot,

01:38 but both figures are actually still hanging around in memory. And in fact, pyplot knows about both of them. I can say plt.get_fignums() and we see we get [1, 2], corresponding to the first and second Figure objects we created. Say our program has lots of figures, and we want to see all of them.

02:07 We can write a one-liner function that will do just that. I’ll write def get_all_figures(): and that will return [plt.figure(i) for i in plt.get_fignums()].

02:31 That will create a new Figure object for each of the figures being tracked by pyplot, just so we can see that it exists. When we’re done, we want to make sure we close it. And so we’ll type plt.close('all'). Now if we run get_all_figures() again, we see nothing.

Become a Member to join the conversation.