Skip to content

Commit

Permalink
Merge pull request matplotlib#27006 from QuLogic/fix-anim-resize
Browse files Browse the repository at this point in the history
DOC: Fix resizing of animation examples
  • Loading branch information
story645 authored Oct 6, 2023
2 parents 9cd50a3 + cd7ba82 commit d9b3449
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 10 deletions.
7 changes: 6 additions & 1 deletion galleries/examples/animation/bayes_update.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,11 @@ def __init__(self, ax, prob=0.5):
# which the plotted distribution should converge.
self.ax.axvline(prob, linestyle='--', color='black')

def start(self):
# Used for the *init_func* parameter of FuncAnimation; this is called when
# initializing the animation, and also after resizing the figure.
return self.line,

def __call__(self, i):
# This way the plot can continuously run and we just keep
# watching new realizations of the process
Expand All @@ -62,5 +67,5 @@ def __call__(self, i):

fig, ax = plt.subplots()
ud = UpdateDist(ax, prob=0.7)
anim = FuncAnimation(fig, ud, frames=100, interval=100, blit=True)
anim = FuncAnimation(fig, ud, init_func=ud.start, frames=100, interval=100, blit=True)
plt.show()
11 changes: 2 additions & 9 deletions galleries/examples/animation/double_pendulum.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,6 @@
Output generated via `matplotlib.animation.Animation.to_jshtml`.
"""

from collections import deque

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


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

if i == 0:
history_x.clear()
history_y.clear()

history_x.appendleft(thisx[2])
history_y.appendleft(thisy[2])
history_x = x2[:i]
history_y = y2[:i]

line.set_data(thisx, thisy)
trace.set_data(history_x, history_y)
Expand Down

0 comments on commit d9b3449

Please sign in to comment.