Managing Multiple Figures

Matplotlib can keep several figures open at once, and managing them means knowing how to create each one, switch which is current, save them separately, and close them to free memory.

Learn Managing Multiple Figures in our free Matplotlib course — a beginner-friendly interactive lesson with worked examples, a practice exercise and a quick…

Part of the free Matplotlib course at LearnCodingFast — hands-on lessons with examples you run in your browser, plus practice exercises and a quick quiz.

In this lesson you'll create figures with plt.figure() , switch between them, grab the current one with plt.gcf() , save each separately, and close figures with plt.close() .

Each call to plt.figure() makes a new figure and marks it as the current one, so plain plt.plot() and plt.title() commands draw onto it. Give figures a number or a name, and you can jump back to one by calling plt.figure(n) again — pyplot switches rather than creating a duplicate.

What you'll see: two separate figure windows — a crimson sine (with an added dashed orange half-sine) and a teal cosine — plus a printout of the open figure numbers proving both exist at once.

The pyplot state machine always has one current figure and axes. plt.gcf() ("get current figure") hands you that Figure object, and plt.gca() hands you the current axes — useful when you drew with state-machine commands but now need the real object to resize or save it. The explicit fig, ax = plt.subplots() style avoids the ambiguity entirely.

When you hold several Figure objects, save each with its own fig.savefig('name.png') . Close figures you no longer need with plt.close(fig) , plt.close('all') , or plt.close(number) — vital inside loops, where unclosed figures pile up in memory. Check whether a figure is still open with plt.fignum_exists(n) .

What you'll see: a printout confirming both PNGs were saved, then that fig1 is closed while fig2 is still open, and finally an empty list after close('all') clears everything.

Replace each ___ to make two figures and then close every figure.

You never called plt.figure() between plots. ✅ Start each new figure with plt.figure() (or a fresh plt.subplots() ).

❌ RuntimeWarning: More than 20 figures opened

A loop creates figures without closing them. ✅ Call plt.close(fig) at the end of each iteration.

pyplot targets the current figure. ✅ Call plt.figure(n) to switch first, or use explicit ax objects.

Loop over three datasets, give each its own figure and title, then report how many figures are open.

Lesson complete — you can juggle many figures!

You created numbered and named figures, switched between them, grabbed the current one with gcf, saved each separately, and closed them cleanly with close.

🚀 Up next: Adding Tables to Plots — pair a data table with your chart for dashboard- style figures.

Practice quiz

What does each call to plt.figure() do?

  • Creates a new figure and makes it current
  • Clears all figures
  • Shows the chart
  • Saves the figure

Answer: Creates a new figure and makes it current. plt.figure() makes a new figure and marks it as the current one.

What happens if you call plt.figure(1) again after it already exists?

  • It raises an error
  • It deletes figure 1
  • pyplot switches to figure 1 instead of duplicating it
  • It renames the figure

Answer: pyplot switches to figure 1 instead of duplicating it. Calling plt.figure with an existing number switches to that figure rather than creating a new one.

Which call returns the current Figure object?

  • plt.figure()
  • plt.gcf()
  • plt.current()
  • plt.fig()

Answer: plt.gcf(). plt.gcf() ('get current figure') returns the active Figure object.

Which call returns the current Axes object?

  • plt.axes_now()
  • plt.gcf()
  • plt.current_ax()
  • plt.gca()

Answer: plt.gca(). plt.gca() ('get current axes') returns the active Axes.

Which call closes every open figure?

  • plt.close('all')
  • plt.clear()
  • plt.reset()
  • plt.delete()

Answer: plt.close('all'). plt.close('all') closes all open figures.

Which call lists the numbers of all open figures?

  • plt.figures()
  • plt.list()
  • plt.get_fignums()
  • plt.open()

Answer: plt.get_fignums(). plt.get_fignums() returns the list of open figure numbers.

Why close figures inside a loop that creates many of them?

  • To change their color
  • It is required for saving
  • It speeds up plotting
  • Open figures stay in memory and pile up

Answer: Open figures stay in memory and pile up. Unclosed figures accumulate in memory, so call plt.close(fig) each iteration.

Which call checks whether a figure number is still open?

  • plt.is_open(n)
  • plt.fignum_exists(n)
  • plt.has_fig(n)
  • plt.exists(n)

Answer: plt.fignum_exists(n). plt.fignum_exists(n) returns whether that figure is still open.

Why does naming figures (plt.figure('revenue')) help?

  • It saves memory
  • It logs the y-axis
  • It is clearer than numbers when a script builds many
  • It speeds up rendering

Answer: It is clearer than numbers when a script builds many. Named figures are easier to track than numbered ones in a busy script.

What does pyplot draw onto when you call plain plt.plot()?

  • A random figure
  • The first figure created
  • All figures at once
  • The current figure

Answer: The current figure. State-machine pyplot commands target the current figure and axes.