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

[ENH]: Provide a standard place to manually register widgets/animations on a figure/artist/etc. #26881

Open
@anntzer

Description

@anntzer

Problem

It is well known that references must be kept to widgets to keep them responsive and to animations to keep them running. Yet sometimes there's no obvious place to stash them, e.g. (minimal example, fill in real-world usage as desired)

importmatplotlib.pyplotaspltfrommatplotlib.widgetsimportCursordefmain():fig,ax=plt.subplots(figsize=(8,6))cursor=Cursor(ax)plt.show()main()

If you run this in non-interactive mode (e.g. saved in module.py and run from an external shell) then you are fine because show() blocks and thus the function frame keeps a reference to the cursor. But if you run this in interactive mode (e.g. from an IPython shell --import module; module.main()) then show() is non-blocking and immediately returns, so while the figure is displayed, the cursor is gc'ed.

As the developer of module.py, if you want to guard against this problem, then you need either to promote your simple function to a full-blown custom class holding the figure and the cursor, or you can go for the "practical" approach of stashing a reference to the cursor at the only reasonable place to do so:fig._cursor_ref = cursor (or similarly as an axes attribute). But adding a custom attribute on the figure for that purpose feels rather dirty, and I guess that static type checkers probably don't like that at all either (not that I've checked...).

Proposed solution

Provide e.g. Figure.register_child/register_widget/register_animation (name up to bikeshedding, and I'm not sure either whether that should just exist on Figures or on all Artists), which simply appends the child object (a widget or an animation, typically) to an internal list whose purpose is only to keep it alive. Perhaps also provide a manual unregistration method.

(Many, including myself, have argued elsewhere that widgets and animations should be auto-kept alive anyways, but I can understand the arguments against that behavior; in this issue I'm just arguing that we should have a standard way to manually register widgets and animations as to be kept alive.)

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