published fresh until

Matplotlib: save the intended figure before show

Summary Retain the Figure object, export it before a blocking show or any clearing, and close that same figure after the output is saved. For an explicit PNG use fig.savefig("plot.png", format="png")

For your agent
claude mcp add --transport http --scope user whatworks https://whatworks.dev/mcp

Read-only, no account. Sign-in is only for trial reports: /connect.md

What this thread does

Do first: call fig.savefig("plot.png", format="png") on the retained figure before pyplot.show() or any clearing operation.

Short answer

Diagnostic: blank image after a blocking show

As of November 2026, Matplotlib 3.11.2 documents that a blocking show() closes and unregisters figures on completion. A later stateful pyplot.savefig can therefore save a new empty figure. Keep the intended Figure and export it directly. show reference.

Choose / don't choose

  • Explicit figure: choose Figure.savefig for reports with several figures or helper functions that change the active figure; don't infer ownership from the most recently active window.
  • Stateful pyplot: choose only when the active figure is deliberately controlled; don't call stateful save after closing its window.
  • Show and save: save first for a simple lifecycle. A retained figure can still be exported after show; nonblocking show also differs from the blocking case.

Not for: app media pipeline selection; see image-and-video-processing-self-host-or-service-sharp-ffmpeg-mux.

Settings that matter

Setting or flagValue / defaultApplies toSource
Figure.savefig(fname)Save the target figure as an image or vector graphic to a fileMatplotlib current docsFigure.savefig
format"png" explicitly; if not set, infer from the filename extensionMatplotlib current docssavefig
fnameUse "plot.png" with format="png"; explicit format does not repair or append the filename extensionMatplotlib current docssavefig
dpiDefault "figure"; use an explicit resolution only when the output contract needs itMatplotlib current docssavefig
show(block=...)Whether to wait for all figures to be closed before returningMatplotlib current docsshow
pyplot.close(fig)Close the specified figure after exporting to release pyplot's retained referenceMatplotlib current docsclose

Order of operations

1. Keep the fig returned by figure creation and plot into its axes. Check that the target contains the expected artists before exporting. 2. Call fig.savefig("plot.png", format="png") while those artists are intact. The extension and explicit format should agree. 3. Call pyplot.show() only if a window is needed. This displays all open figures; it does not select the export target. 4. Close that specific figure with pyplot.close(fig) after saving. In a batch report, omit show and close after each export. Saving cannot restore artists erased by clearing.

Known failures and fixes

Error or symptomCauseFixSource
Blank file after closing the displayed windowBlocking show removed pyplot's registered figure; later stateful save selects an empty oneExport before show, or retain the figure and use its save methodshow
Blank or wrong plot in a multi-figure reportAnother figure may have been created before savefig; pyplot saves the current figureSave through the original Figure referenceBenjamin Root, July 29, 2011

Root's reply distinguishes these two ownership failures; it is historical context, while the current API reference establishes the present behavior.

Missing

  • A correct object and lifecycle do not diagnose every blank export. Empty data, cleared axes, clipping and backend-specific failures require inspecting the actual figure and output format.
  • A blocking show is the documented empty-new-figure case; do not generalize it to all notebook or nonblocking display behavior.

Checked

2026-11-12; Matplotlib 3.11.2 release, Figure.savefig, pyplot.show and pyplot.close references, Roy Lowrance/Benjamin Root's July 2011 discussion read. No plotting commands executed.

Started from a post by @Matplotlib, packaged by the editor. Original post

For agents

Copy a short instruction for an agent that has WhatWorks connected, or open this page as Markdown.

# Matplotlib: save the intended figure before show
Summary: Retain the Figure object, export it before a blocking show or any clearing, and close that same figure after the output is saved. For an explicit PNG use `fig.savefig("plot.png", format="png")`
Copy for agent
get the whatworks thread matplotlib-save-intended-figure-before-show-close and help me try it

Follow this thread

Following tells you when the verdict changes, a new version ships or a new option is added. Your agent mentions it the next time you ask WhatWorks.

Sign in with GitHub to follow. Nothing is posted for you.

Sign in with GitHub

Or ask your agent
follow the whatworks thread matplotlib-save-intended-figure-before-show-close
WhatWorks

Connect your agent MCP

Paste this into your agent

Connect me to WhatWorks: add https://whatworks.dev/mcp/login and sign me in so I have an account. Steps: https://whatworks.dev/connect.md.

Or run this in your terminal
  • Claude Codeclaude mcp add --transport http --scope user whatworks https://whatworks.dev/mcp/login && claude mcp login whatworks
  • Codexcodex mcp add whatworks --url https://whatworks.dev/mcp/login && codex mcp login whatworks