From dc10b55603bc3353cc764de6dd53344c26323a49 Mon Sep 17 00:00:00 2001 From: kumar <147685522+Lankalapallikumar@users.noreply.github.com> Date: Thu, 16 Apr 2026 07:23:02 +0530 Subject: [PATCH 1/2] [Doc] Add Jupyter notebook animation example with phase display This PR improves the animation documentation by adding an example for displaying animations in Jupyter notebooks. - Added a new section demonstrating how to use HTML(ani.to_jshtml()) for inline display. - Included a dynamic title update showing both frame number and phase value. This helps users better understand how to work with animations in notebook environments. --- .../users_explain/animations/animations.py | 27 +++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/galleries/users_explain/animations/animations.py b/galleries/users_explain/animations/animations.py index 45c42f538fa6..241942312def 100644 --- a/galleries/users_explain/animations/animations.py +++ b/galleries/users_explain/animations/animations.py @@ -131,6 +131,33 @@ def update(frame): ani = animation.FuncAnimation(fig=fig, func=update, frames=40, interval=30) plt.show() +# %% +# Animations in Jupyter Notebooks +# =============================== +# +# Animations can also be displayed inside Jupyter notebooks using HTML rendering. +# This allows interactive playback controls directly in the notebook. +# +# Example: + +from IPython.display import HTML + +fig, ax = plt.subplots() +x = np.linspace(0, 2*np.pi) +line, = ax.plot(x, np.sin(x)) +title = ax.set_title("Frame 0") + +FRAMES = 50 + +def animate(i): + phi = 2 * np.pi * i / FRAMES + line.set_data(x, np.sin(x - phi)) + title.set_text(f"Frame {i}, Phase = {phi:.2f}") + return (line,) + +ani = animation.FuncAnimation(fig, animate, frames=FRAMES) +plt.close(fig) +HTML(ani.to_jshtml(fps=20)) # %% # ``ArtistAnimation`` From 6e452af9a2d1158d66c06d6e509fdda730509121 Mon Sep 17 00:00:00 2001 From: kumar <147685522+Lankalapallikumar@users.noreply.github.com> Date: Thu, 16 Apr 2026 13:17:03 +0530 Subject: [PATCH 2/2] Fix lint: move import to top and correct blank lines Moved HTML import to the top of the file to fix E402 error. Added required blank lines before function definition to fix E302. --- galleries/users_explain/animations/animations.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/galleries/users_explain/animations/animations.py b/galleries/users_explain/animations/animations.py index 241942312def..1259a7b7c6f6 100644 --- a/galleries/users_explain/animations/animations.py +++ b/galleries/users_explain/animations/animations.py @@ -16,8 +16,8 @@ import matplotlib.pyplot as plt import numpy as np - import matplotlib.animation as animation +from IPython.display import HTML # %% # Animation classes @@ -140,7 +140,6 @@ def update(frame): # # Example: -from IPython.display import HTML fig, ax = plt.subplots() x = np.linspace(0, 2*np.pi) @@ -149,6 +148,7 @@ def update(frame): FRAMES = 50 + def animate(i): phi = 2 * np.pi * i / FRAMES line.set_data(x, np.sin(x - phi))