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

Skip to content

Commit 60a2b92

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 60a2b92

File tree

2 files changed

+8
-8
lines changed

2 files changed

+8
-8
lines changed

galleries/examples/animation/bayes_update.py

Lines changed: 6 additions & 1 deletion
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

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -92,19 +92,14 @@ def derivs(t, state):
9292
trace, = ax.plot([], [], '.-', lw=1, ms=2)
9393
time_template = 'time = %.1fs'
9494
time_text = ax.text(0.05, 0.9, '', transform=ax.transAxes)
95-
history_x, history_y = deque(maxlen=history_len), deque(maxlen=history_len)
9695

9796

9897
def animate(i):
9998
thisx = [0, x1[i], x2[i]]
10099
thisy = [0, y1[i], y2[i]]
101100

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])
101+
history_x = x2[:i]
102+
history_y = y2[:i]
108103

109104
line.set_data(thisx, thisy)
110105
trace.set_data(history_x, history_y)

0 commit comments

Comments
 (0)