From cd7ba8268b4f2006c4c3777f079163ed706d0e5c Mon Sep 17 00:00:00 2001 From: Elliott Sales de Andrade Date: Thu, 5 Oct 2023 17:23:04 -0400 Subject: [PATCH] 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.) --- galleries/examples/animation/bayes_update.py | 7 ++++++- galleries/examples/animation/double_pendulum.py | 11 ++--------- 2 files changed, 8 insertions(+), 10 deletions(-) diff --git a/galleries/examples/animation/bayes_update.py b/galleries/examples/animation/bayes_update.py index 86b6e5c342d1..1081b4704623 100644 --- a/galleries/examples/animation/bayes_update.py +++ b/galleries/examples/animation/bayes_update.py @@ -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 @@ -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() diff --git a/galleries/examples/animation/double_pendulum.py b/galleries/examples/animation/double_pendulum.py index 5f8282daea0f..7a42a6d989ba 100644 --- a/galleries/examples/animation/double_pendulum.py +++ b/galleries/examples/animation/double_pendulum.py @@ -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 @@ -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) pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy