Repository navigation
Add pause() and resume() methods to the base Animation class #17826
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
Changes from all commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
81af2b1
Add pause() and resume() methods to base Animation class.
DCtheTall 6b2553b
fix style issues
DCtheTall 22a935b
use set_animated correctly
DCtheTall 849f3b2
remove _paused attribute from Animation class
DCtheTall f5b1649
rm fig.set_animated call left in
DCtheTall f569671
Add example script
DCtheTall d4a256c
rm docstring
DCtheTall 8dcce25
rm unused sys import
DCtheTall 8d84206
add ending newline to pause_resume.py
DCtheTall be90cb7
added entry to doc/users/next_whats_new/
DCtheTall 29d38c2
add title to pause/resume example
DCtheTall 408331a
review comments on pause/resume animation example
DCtheTall c5a5f45
add tmp variable for PauseAnimation in example, update docs/missing-r…
DCtheTall 6fd9dbe
add TimedAnimation methods to docs/missing-references.json
DCtheTall File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,6 @@ | ||
| Pausing and Resuming Animations | ||
| ------------------------------- | ||
| The `.animation.Animation.pause` and `.animation.Animation.resume` methods | ||
| allow you to pause and resume animations. These methods can be used as callbacks | ||
| for event listeners on UI elements so that your plots can have some playback | ||
| control UI. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,47 @@ | ||
| """ | ||
| ================================= | ||
| Pausing and Resuming an Animation | ||
| ================================= | ||
|
|
||
| This example showcases: | ||
| - using the Animation.pause() method to pause an animation. | ||
| - using the Animation.resume() method to resume an animation. | ||
| """ | ||
|
|
||
| import matplotlib.pyplot as plt | ||
| import matplotlib.animation as animation | ||
| import numpy as np | ||
|
|
||
|
|
||
| class PauseAnimation: | ||
| def __init__(self): | ||
| fig, ax = plt.subplots() | ||
| ax.set_title('Click to pause/resume the animation') | ||
| x = np.linspace(-0.1, 0.1, 1000) | ||
|
|
||
| # Start with a normal distribution | ||
| self.n0 = (1.0 / ((4 * np.pi * 2e-4 * 0.1) ** 0.5) | ||
| * np.exp(-x ** 2 / (4 * 2e-4 * 0.1))) | ||
| self.p, = ax.plot(x, self.n0) | ||
|
|
||
| self.animation = animation.FuncAnimation( | ||
| fig, self.update, frames=200, interval=50, blit=True) | ||
| self.paused = False | ||
|
|
||
| fig.canvas.mpl_connect('button_press_event', self.toggle_pause) | ||
|
|
||
| def toggle_pause(self, *args, **kwargs): | ||
| if self.paused: | ||
| self.animation.resume() | ||
| else: | ||
| self.animation.pause() | ||
| self.paused = not self.paused | ||
|
|
||
| def update(self, i): | ||
| self.n0 += i / 100 % 5 | ||
| self.p.set_ydata(self.n0 % 20) | ||
| return (self.p,) | ||
|
|
||
|
|
||
| pa = PauseAnimation() | ||
| plt.show() | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.