Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

[MNT]: Lifetime of pyplot figures #29849

Open
@timhoffm

Description

@timhoffm

Summary

Inspired by but technically orthogonal to the recent discussions#29782,#29836: Can we change the lifetime behavior so that (1) users have to care/know less about how pyplot manages the figure - ideally noclose() is needed - and (2) we do not break typical existing usage patterns?

Proposed fix

Observations / Questions:

  1. Why doespyplot has to keep a figure reference?
    • 1.1: This prevents garbage collection if the user does not store the figure in a variable.
    • 1.2: It allows managing multiple figures to change thecurrent figure.
    • 1.3: It manages the figure interactions if the figure is shown.
  2. It is unlikely that people manage a lot of figures in parallel through the pyplot state - (if you have 20 figures, you'll propably not organize them through pyplot - or if you do, you at least assign dedicated fignums when creating them)

This combination tells me there is a space for criterions to auto-delete figures:

One interpretation could be: a figure is not needed anymore if all of the following apply:

  • the figure is not shown (no interaction possible)
  • the figure is not the active figure (it's currently not worked on)
  • the figure does not have a user-created fignum (it's not possible to make the figure active again)

This would effectively mean that you only have muliple figures tracked in pyplot if either they are currently shown or they have explicit user-created fignums. Conversely, figures without user-created fignums are auto-deleted when appropriate.
IMHO this is a very reasonable logic and removes the need to understand pyplot tracking or use ofclose for almost all users.

There's only one minor use case I can imagine that would get broken by this: Users relying on the implictly created fignumbers for changing the active figure:

plt.figure()  # num=1plt.plot()plt.figure()  # num=2plt.plot()plt.figure(1)  # knowing that the first figure() call auto-assigned num=1

But this behavior is brittle anyway because you have to know that you're in a fresh interpreter. There are three mitigation strategies:

  • deprecate recalling a figure with an implicitly created figure number
  • keep the last N (e.g. 5) implicitly created figures still around. This recall by implicit number may be feasible for one or two figures but nobody can do this for many figures
  • don't care as this is a brittle edge-case, which people should not be using anyway

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions


      [8]ページ先頭

      ©2009-2025 Movatter.jp