Skip to content

Improve docs regarding plt.close(). #29839

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 1, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions doc/users/faq.rst
Original file line number Diff line number Diff line change
Expand Up @@ -287,6 +287,7 @@ the desired format::
import matplotlib.pyplot as plt
plt.plot([1, 2, 3])
plt.savefig('myfig.png')
plt.close()

.. seealso::

Expand Down
10 changes: 9 additions & 1 deletion lib/matplotlib/pyplot.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
----------
Expand All @@ -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()
Expand Down
Loading