File tree Expand file tree Collapse file tree 4 files changed +16
-25
lines changed
galleries/examples/user_interfaces Expand file tree Collapse file tree 4 files changed +16
-25
lines changed Original file line number Diff line number Diff line change @@ -281,7 +281,7 @@ locators as desired because the two axes are independent.
281281Generate 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
285285instance::
286286
287287 from matplotlib.figure import Figure
@@ -292,12 +292,10 @@ instance::
292292
293293This 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])
Original file line number Diff line number Diff line change 3232from matplotlib .figure import Figure
3333
3434fig = 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.
4137ax = fig .add_subplot ()
4541# etc.).
4642fig .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 )
5050canvas .draw ()
5151rgba = np .asarray (canvas .buffer_rgba ())
5252# ... and pass it to PIL.
Original file line number Diff line number Diff line change 55
66When using Matplotlib in a web server it is strongly recommended to not use
77pyplot (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
99figures are properly closed).
1010
1111Since 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
Original file line number Diff line number Diff 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()`.
You can’t perform that action at this time.
0 commit comments