diff --git a/doc/users/faq.rst b/doc/users/faq.rst index c86b97033cbd..b08bd75cee4e 100644 --- a/doc/users/faq.rst +++ b/doc/users/faq.rst @@ -287,6 +287,7 @@ the desired format:: import matplotlib.pyplot as plt plt.plot([1, 2, 3]) plt.savefig('myfig.png') + plt.close() .. seealso:: diff --git a/lib/matplotlib/pyplot.py b/lib/matplotlib/pyplot.py index 25aa3b558227..44e7e0b7d98a 100644 --- a/lib/matplotlib/pyplot.py +++ b/lib/matplotlib/pyplot.py @@ -1172,7 +1172,7 @@ def disconnect(cid: int) -> None: def close(fig: None | int | str | Figure | Literal["all"] = None) -> None: """ - Close a figure window. + Close a figure window, and unregister it from pyplot. Parameters ---------- @@ -1185,6 +1185,14 @@ def close(fig: None | int | str | Figure | Literal["all"] = None) -> None: - ``str``: a figure name - 'all': all figures + Notes + ----- + pyplot maintains a reference to figures created with `figure()`. When + work on the figure is completed, it should be closed, i.e. deregistered + from pyplot, to free its memory (see also :rc:figure.max_open_warning). + Closing a figure window created by `show()` automatically deregisters the + figure. For all other use cases, most prominently `savefig()` without + `show()`, the figure must be deregistered explicitly using `close()`. """ if fig is None: manager = _pylab_helpers.Gcf.get_active()
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: