Skip to content

Commit 420750c

Browse files
authored
Merge pull request #30853 from anntzer/close
Minor doc fixes re: close()ing figures.
2 parents f1ea23f + f33265e commit 420750c

File tree

4 files changed

+16
-25
lines changed

4 files changed

+16
-25
lines changed

doc/users/faq.rst

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

284-
The recommended approach since matplotlib 3.1 is to explicitly create a Figure
284+
The recommended approach since Matplotlib 3.1 is to explicitly create a Figure
285285
instance::
286286

287287
from matplotlib.figure import Figure
@@ -292,12 +292,10 @@ instance::
292292

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

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::
295+
It's alternatively still possible to use the pyplot interface: instead of
296+
calling `matplotlib.pyplot.show`, call `matplotlib.pyplot.savefig`. In that
297+
case, you must close the figure after saving it. Not closing the figure causes
298+
a memory leak, because pyplot keeps references to all not-yet-shown figures. ::
301299

302300
import matplotlib.pyplot as plt
303301
plt.plot([1, 2, 3])

galleries/examples/user_interfaces/canvasagg.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -32,10 +32,6 @@
3232
from matplotlib.figure import Figure
3333

3434
fig = Figure(figsize=(5, 4), dpi=100)
35-
# A canvas must be manually attached to the figure (pyplot would automatically
36-
# do it). This is done by instantiating the canvas with the figure as
37-
# argument.
38-
canvas = FigureCanvasAgg(fig)
3935

4036
# Do some plotting.
4137
ax = fig.add_subplot()
@@ -45,8 +41,12 @@
4541
# etc.).
4642
fig.savefig("test.png")
4743

48-
# Option 2: Retrieve a memoryview on the renderer buffer, and convert it to a
44+
# Option 2 (low-level approach to directly save to a numpy array): Manually
45+
# attach a canvas to the figure (pyplot or savefig would automatically do
46+
# it), by instantiating the canvas with the figure as argument; then draw the
47+
# figure, retrieve a memoryview on the renderer buffer, and convert it to a
4948
# numpy array.
49+
canvas = FigureCanvasAgg(fig)
5050
canvas.draw()
5151
rgba = np.asarray(canvas.buffer_rgba())
5252
# ... and pass it to PIL.

galleries/examples/user_interfaces/web_application_server_sgskip.py

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
66
When using Matplotlib in a web server it is strongly recommended to not use
77
pyplot (pyplot maintains references to the opened figures to make
8-
`~.matplotlib.pyplot.show` work, but this will cause memory leaks unless the
8+
`~.pyplot.show` work, but this will cause memory leaks unless the
99
figures are properly closed).
1010
1111
Since Matplotlib 3.1, one can directly create figures using the `.Figure`
@@ -45,21 +45,14 @@ def hello():
4545
# %%
4646
#
4747
# Since the above code is a Flask application, it should be run using the
48-
# `flask command-line tool <https://flask.palletsprojects.com/en/latest/cli/>`_
49-
# Assuming that the working directory contains this script:
50-
#
51-
# Unix-like systems
48+
# `flask command-line tool <https://flask.palletsprojects.com/en/latest/cli/>`_:
49+
# run
5250
#
5351
# .. code-block:: console
5452
#
55-
# FLASK_APP=web_application_server_sgskip flask run
56-
#
57-
# Windows
58-
#
59-
# .. code-block:: console
53+
# flask --app web_application_server_sgskip run
6054
#
61-
# set FLASK_APP=web_application_server_sgskip
62-
# flask run
55+
# from the directory containing this script.
6356
#
6457
#
6558
# Clickable images for HTML

lib/matplotlib/pyplot.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1263,7 +1263,7 @@ def close(fig: None | int | str | Figure | Literal["all"] = None) -> None:
12631263
-----
12641264
pyplot maintains a reference to figures created with `figure()`. When
12651265
work on the figure is completed, it should be closed, i.e. deregistered
1266-
from pyplot, to free its memory (see also :rc:figure.max_open_warning).
1266+
from pyplot, to free its memory (see also :rc:`figure.max_open_warning`).
12671267
Closing a figure window created by `show()` automatically deregisters the
12681268
figure. For all other use cases, most prominently `savefig()` without
12691269
`show()`, the figure must be deregistered explicitly using `close()`.

0 commit comments

Comments
 (0)