Skip to content

animation.py: Stop event sources instead of nulling them on animation stop - #32296

Open
DavidVadnais wants to merge 1 commit into
matplotlib:mainfrom
DavidVadnais:fix-issue-#30622
Open

DavidVadnais wants to merge 1 commit into
matplotlib:mainfrom
DavidVadnais:fix-issue-#30622

Conversation

@DavidVadnais

@DavidVadnais DavidVadnais commented Sep 4, 2026 •

Copy link
Copy Markdown

PR summary

Closes: #30622
Followed issue link to #30590. Then followed suggestions made there.

code to exercise this change

import matplotlib
matplotlib.use('agg')
import matplotlib.pyplot as plt
from matplotlib.animation import FuncAnimation

fig, ax = plt.subplots()
anim = FuncAnimation(fig, lambda f: [], frames=3, repeat=False)
anim._start()
while anim._step():
    pass
anim.event_source 

Output before change

None

Output after change

print(anim.event_source)
<matplotlib.backend_bases.TimerBase at 0x...>

AI Disclosure

Used ai to help me find an easy issue to try and dip my toe in the water. Then had it write the exercising code.

PR quality check

  • Use an expressive title, e.g. "Fix title font property precedence"
  • New and changed code is tested
  • Plotting related features are demonstrated in an example
  • New features and API changes have release notes
  • Documentation complies with general and docstring guidelines

@github-actions

github-actions Bot commented Sep 4, 2026

Copy link
Copy Markdown

Thank you for opening your first PR into Matplotlib!

If you have not heard from us in a week or so, please leave a new comment below and that should bring it to our attention. Most of our reviewers are volunteers and sometimes things fall through the cracks. We also ask that you please finish addressing any review comments on this PR and wait for it to be merged (or closed) before opening a new one, as it can be a valuable learning experience to go through the review process.

You can also join us on discourse chat for real-time discussion.

For details on testing, writing docs, and our review process, please see the developer guide.
Please let us know if (and how) you use AI, it will help us give you better feedback on your PR.

We strive to be a welcoming and open project. Please follow our Code of Conduct.

@DavidVadnais

Copy link
Copy Markdown
Author

I think i need to add a file to doc/api/next_api_changes/behavior/ named something like animation_stop_event_stop_not_null.rst explaining this change.

@DavidVadnais

Copy link
Copy Markdown
Author

To my understanding, if all the actions pass this PR is ready for review. Please let me know if I’ve missed any pre-review steps

@DavidVadnais
DavidVadnais force-pushed the fix-issue-#30622 branch 2 times, most recently from a4793c2 to 30da2c4 Compare September 6, 2026 21:43
@DavidVadnais

Copy link
Copy Markdown
Author

Aloha,

I am following up after a week as instructed by the github bot. Let me know what I am missing.

@DavidVadnais

Copy link
Copy Markdown
Author

rebasing since this getting out of sync

git fetch origin
git rebase origin/main
git push --force-with-lease

@melissawm

Copy link
Copy Markdown
Member

Hi @DavidVadnais - I am flagging this for review, but our maintainers are currently pretty busy so apologies if it's taking longer than expected. Thanks for the patience!

@DavidVadnais

Copy link
Copy Markdown
Author

Sounds good. Thank you for all the work you all do.

Comment on lines +1 to +6
``TimedAnimation``/``Animation`` now perform .stop() on mpl_disconnect
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Previously these classes set ``self.event_source = None`` when
``self._fig.canvas.mpl_disconnect`` was called. Now they run
``self.event_source.stop()``.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

This is a very internal description of what changed. Users aren't calling mpl_disconnect on a private _fig attribute.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

I did my best here but let me know if you want further changes.

Comment thread doc/api/next_api_changes/behavior/animation_stop_event_stop_not_null.rst Outdated
Comment thread doc/api/next_api_changes/behavior/animation_stop_event_stop_not_null.rst Outdated
Comment thread lib/matplotlib/tests/test_animation.py Outdated
Comment on lines +580 to +581
Tests that animations now stop the event source instead
of destroying it on animation completion.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Don't really need to mention what happened before.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

Removed reference to previous behavior.

Comment thread lib/matplotlib/tests/test_animation.py Outdated
fig, ax = plt.subplots()
anim = FuncAnimation(fig, lambda f: [], frames=3)
anim._start()
plt.close(fig)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

This isn't necessary for testing in general, so you should add a comment that it's intentionally there.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

added comment.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[MNT]: Stop event sources rather than setting them to None in Animation._stop()

3 participants