File tree Expand file tree Collapse file tree 1 file changed +17
-2
lines changed Expand file tree Collapse file tree 1 file changed +17
-2
lines changed Original file line number Diff line number Diff line change @@ -281,8 +281,23 @@ locators as desired because the two axes are independent.
281
281
Generate images without having a window appear
282
282
----------------------------------------------
283
283
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::
286
301
287
302
import matplotlib.pyplot as plt
288
303
plt.plot([1, 2, 3])
You can’t perform that action at this time.
0 commit comments