Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 3c8af99

Browse files
authored
Merge pull request #17826 from DCtheTall/pauseresume
Add pause() and resume() methods to the base Animation class
2 parents 11d5b75 + 6fd9dbe commit 3c8af99

File tree

4 files changed

+85
-0
lines changed

4 files changed

+85
-0
lines changed

doc/missing-references.json

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1136,6 +1136,12 @@
11361136
"matplotlib.animation.ArtistAnimation.to_jshtml": [
11371137
"doc/api/_as_gen/matplotlib.animation.ArtistAnimation.rst:21:<autosummary>:1"
11381138
],
1139+
"matplotlib.animation.ArtistAnimation.pause": [
1140+
"doc/api/_as_gen/matplotlib.animation.ArtistAnimation.rst:23:<autosummary>:1"
1141+
],
1142+
"matplotlib.animation.ArtistAnimation.resume": [
1143+
"doc/api/_as_gen/matplotlib.animation.ArtistAnimation.rst:23:<autosummary>:1"
1144+
],
11391145
"matplotlib.animation.FFMpegFileWriter.args_key": [
11401146
"lib/matplotlib/animation.py:docstring of matplotlib.animation.FFMpegFileWriter.supported_formats:1:<autosummary>:1"
11411147
],
@@ -1235,6 +1241,12 @@
12351241
"matplotlib.animation.FuncAnimation.to_jshtml": [
12361242
"lib/matplotlib/animation.py:docstring of matplotlib.animation.FuncAnimation.new_frame_seq:1:<autosummary>:1"
12371243
],
1244+
"matplotlib.animation.FuncAnimation.pause": [
1245+
"doc/api/_as_gen/matplotlib.animation.ArtistAnimation.rst:23:<autosummary>:1"
1246+
],
1247+
"matplotlib.animation.FuncAnimation.resume": [
1248+
"doc/api/_as_gen/matplotlib.animation.ArtistAnimation.rst:23:<autosummary>:1"
1249+
],
12381250
"matplotlib.animation.HTMLWriter.bin_path": [
12391251
"doc/api/_as_gen/matplotlib.animation.HTMLWriter.rst:28:<autosummary>:1"
12401252
],
@@ -1361,6 +1373,12 @@
13611373
"matplotlib.animation.TimedAnimation.to_jshtml": [
13621374
"doc/api/_as_gen/matplotlib.animation.TimedAnimation.rst:21:<autosummary>:1"
13631375
],
1376+
"matplotlib.animation.TimedAnimation.pause": [
1377+
"doc/api/_as_gen/matplotlib.animation.ArtistAnimation.rst:23:<autosummary>:1"
1378+
],
1379+
"matplotlib.animation.TimedAnimation.resume": [
1380+
"doc/api/_as_gen/matplotlib.animation.ArtistAnimation.rst:23:<autosummary>:1"
1381+
],
13641382
"matplotlib.cbook.ls_mapper": [
13651383
"doc/api/prev_api_changes/api_changes_1.5.0.rst:11",
13661384
"doc/api/prev_api_changes/api_changes_1.5.0.rst:8"
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
Pausing and Resuming Animations
2+
-------------------------------
3+
The `.animation.Animation.pause` and `.animation.Animation.resume` methods
4+
allow you to pause and resume animations. These methods can be used as callbacks
5+
for event listeners on UI elements so that your plots can have some playback
6+
control UI.

examples/animation/pause_resume.py

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
"""
2+
=================================
3+
Pausing and Resuming an Animation
4+
=================================
5+
6+
This example showcases:
7+
- using the Animation.pause() method to pause an animation.
8+
- using the Animation.resume() method to resume an animation.
9+
"""
10+
11+
import matplotlib.pyplot as plt
12+
import matplotlib.animation as animation
13+
import numpy as np
14+
15+
16+
class PauseAnimation:
17+
def __init__(self):
18+
fig, ax = plt.subplots()
19+
ax.set_title('Click to pause/resume the animation')
20+
x = np.linspace(-0.1, 0.1, 1000)
21+
22+
# Start with a normal distribution
23+
self.n0 = (1.0 / ((4 * np.pi * 2e-4 * 0.1) ** 0.5)
24+
* np.exp(-x ** 2 / (4 * 2e-4 * 0.1)))
25+
self.p, = ax.plot(x, self.n0)
26+
27+
self.animation = animation.FuncAnimation(
28+
fig, self.update, frames=200, interval=50, blit=True)
29+
self.paused = False
30+
31+
fig.canvas.mpl_connect('button_press_event', self.toggle_pause)
32+
33+
def toggle_pause(self, *args, **kwargs):
34+
if self.paused:
35+
self.animation.resume()
36+
else:
37+
self.animation.pause()
38+
self.paused = not self.paused
39+
40+
def update(self, i):
41+
self.n0 += i / 100 % 5
42+
self.p.set_ydata(self.n0 % 20)
43+
return (self.p,)
44+
45+
46+
pa = PauseAnimation()
47+
plt.show()

lib/matplotlib/animation.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1355,6 +1355,20 @@ def _repr_html_(self):
13551355
elif fmt == 'jshtml':
13561356
return self.to_jshtml()
13571357

1358+
def pause(self):
1359+
"""Pause the animation."""
1360+
self.event_source.stop()
1361+
if self._blit:
1362+
for artist in self._drawn_artists:
1363+
artist.set_animated(False)
1364+
1365+
def resume(self):
1366+
"""Resume the animation."""
1367+
self.event_source.start()
1368+
if self._blit:
1369+
for artist in self._drawn_artists:
1370+
artist.set_animated(True)
1371+
13581372

13591373
class TimedAnimation(Animation):
13601374
"""

0 commit comments

Comments
 (0)