Skip to content

Commit 40516e8

Browse files
authored
Merge pull request #30244 from timhoffm/doc-save-image
2 parents e22a6e8 + c8361f0 commit 40516e8

File tree

1 file changed

+17
-2
lines changed

1 file changed

+17
-2
lines changed

doc/users/faq.rst

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -281,8 +281,23 @@ locators as desired because the two axes are independent.
281281
Generate images without having a window appear
282282
----------------------------------------------
283283

284-
Simply do not call `~matplotlib.pyplot.show`, and directly save the figure to
285-
the desired format::
284+
The recommended approach since matplotlib 3.1 is to explicitly create a Figure
285+
instance::
286+
287+
from matplotlib.figure import Figure
288+
fig = Figure()
289+
ax = fig.subplots()
290+
ax.plot([1, 2, 3])
291+
fig.savefig('myfig.png')
292+
293+
This prevents any interaction with GUI frameworks and the window manager.
294+
295+
It's alternatively still possible to use the pyplot interface. Instead of
296+
calling `matplotlib.pyplot.show`, call `matplotlib.pyplot.savefig`.
297+
298+
Additionally, you must ensure to close the figure after saving it. Not
299+
closing the figure is a memory leak, because pyplot keeps references
300+
to all not-yet-shown figures::
286301

287302
import matplotlib.pyplot as plt
288303
plt.plot([1, 2, 3])

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy