Skip to content

Commit cd7ba82

Browse files
committed
DOC: Fix resizing of animation examples
On resize, either `init_func` will be called, or if not defined, the main `func` with `i=0` is called. On animations that save a state, the latter can cause weird glitching as the history will skip around. * For `bayes_update`, I fixed it by providing an `init_func`. * For `double_pendulum`, I fixed it by simplifying the trace line (since we have the entire history pre-computed.)
1 parent 6ba7d5f commit cd7ba82

File tree

2 files changed

+8
-10
lines changed

2 files changed

+8
-10
lines changed

galleries/examples/animation/bayes_update.py

+6-1
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,11 @@ def __init__(self, ax, prob=0.5):
4141
# which the plotted distribution should converge.
4242
self.ax.axvline(prob, linestyle='--', color='black')
4343

44+
def start(self):
45+
# Used for the *init_func* parameter of FuncAnimation; this is called when
46+
# initializing the animation, and also after resizing the figure.
47+
return self.line,
48+
4449
def __call__(self, i):
4550
# This way the plot can continuously run and we just keep
4651
# watching new realizations of the process
@@ -62,5 +67,5 @@ def __call__(self, i):
6267

6368
fig, ax = plt.subplots()
6469
ud = UpdateDist(ax, prob=0.7)
65-
anim = FuncAnimation(fig, ud, frames=100, interval=100, blit=True)
70+
anim = FuncAnimation(fig, ud, init_func=ud.start, frames=100, interval=100, blit=True)
6671
plt.show()

galleries/examples/animation/double_pendulum.py

+2-9
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,6 @@
1111
Output generated via `matplotlib.animation.Animation.to_jshtml`.
1212
"""
1313

14-
from collections import deque
15-
1614
import matplotlib.pyplot as plt
1715
import numpy as np
1816
from numpy import cos, sin
@@ -92,19 +90,14 @@ def derivs(t, state):
9290
trace, = ax.plot([], [], '.-', lw=1, ms=2)
9391
time_template = 'time = %.1fs'
9492
time_text = ax.text(0.05, 0.9, '', transform=ax.transAxes)
95-
history_x, history_y = deque(maxlen=history_len), deque(maxlen=history_len)
9693

9794

9895
def animate(i):
9996
thisx = [0, x1[i], x2[i]]
10097
thisy = [0, y1[i], y2[i]]
10198

102-
if i == 0:
103-
history_x.clear()
104-
history_y.clear()
105-
106-
history_x.appendleft(thisx[2])
107-
history_y.appendleft(thisy[2])
99+
history_x = x2[:i]
100+
history_y = y2[:i]
108101

109102
line.set_data(thisx, thisy)
110103
trace.set_data(history_x, history_y)

0 commit comments

Comments
 (0)