8000 DOC: Recommend to use bare Figure instances for saving to file by timhoffm · Pull Request #30244 · matplotlib/matplotlib · GitHub
[go: up one dir, main page]

Skip to content

DOC: Recommend to use bare Figure instances for saving to file #30244

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 2, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 17 additions & 2 deletions doc/users/faq.rst
Original file line number Diff line number Diff line change
Expand Up @@ -281,8 +281,23 @@ locators as desired because the two axes are independent.
Generate images without having a window appear
----------------------------------------------

Simply do not call `~matplotlib.pyplot.show`, and directly save the figure to
the desired format::
The recommended approach since matplotlib 3.1 is to explicitly create a Figure
instance::

from matplotlib.figure import Figure
fig = Figure()
ax = fig.subplots()
ax.plot([1, 2, 3])
fig.savefig('myfig.png')

This prevents any interaction with GUI frameworks and the window manager.

It's alternatively still possible to use the pyplot interface. Instead of
calling `matplotlib.pyplot.show`, call `matplotlib.pyplot.savefig`.

Additionally, you must ensure to close the figure after saving it. Not
closing the figure is a memory leak, because pyplot keeps references
to all not-yet-shown figures::

import matplotlib.pyplot as plt
plt.plot([1, 2, 3])
Expand Down
0