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

Conversation

anntzer
Copy link
Contributor

@anntzer anntzer commented Mar 31, 2025

See #29782 (comment)

PR summary

PR checklist

Comment on lines 1190 to 1192
pyplot maintains a reference to figures created with `figure()` until
`close()` is called on them. Closing figures is thus necessary to prevent
running out of memory (see also :rc:`figure.max_open_warning`).
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we get this more in the direction that close() is the explicit way to deregister a figure from pyplot? I want to prevent the implication that every figure() must be paired with close(). Closing a figure window that was created using show() also deregisters the figure from pyplot and hence close() is not needed in that usage scenario.

Something like

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().

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sure, done

@timhoffm
Copy link
Member

timhoffm commented Apr 1, 2025

Do we have an explicit documentation on the lifecycle of figures with pyplot (I'm not aware of that). If so, we could link there. If not, that's for another time/PR.

Anybody can merge after CI has run.

@timhoffm timhoffm added this to the v3.10.2 milestone Apr 1, 2025
@timhoffm timhoffm merged commit 90385b4 into matplotlib:main Apr 1, 2025
41 checks passed
meeseeksmachine pushed a commit to meeseeksmachine/matplotlib that referenced this pull request Apr 1, 2025
@anntzer anntzer deleted the close branch April 1, 2025 11:49
timhoffm added a commit that referenced this pull request Apr 1, 2025
…839-on-v3.10.x

Backport PR #29839 on branch v3.10.x (Improve docs regarding plt.close().)
@ksunden ksunden mentioned this pull request May 9, 2025
5 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Documentation: user guide files in galleries/users_explain or doc/users Documentation topic: pyplot API
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants