%matplotlib ipympl
# based on OP's code & code currently next to 'In [6]' at https://nbviewer.org/github/fomightez/animated_matplotlib-binder/blob/main/index.ipynb , adapted from https://discourse.jupyter.org/t/matplotlib-animation-not-appearing-in-jupyter-notebook/24938/3?u=fomightez
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.animation import FuncAnimation
tstep = 1
#xs, ys = block_centroids.T
xs, ys = (1,4,6),(2,5,9)
timepoints = list(zip(xs,ys))
points = list(zip(xs,ys))
num_steps = len(timepoints)
#print(points)
#print(np.arange(0, num_steps))
fig = plt.figure(figsize=(9,5))
ax = fig.add_subplot()
fig.subplots_adjust(left=0.1, right=0.85)
def animate(frame):
step_num = frame % (num_steps)
ax.scatter(points[step_num][0],points[step_num][1], c='skyblue', s=60)
ax.set_xlabel('x')
ax.set_ylabel('y')
ax.set_title(f"frame: {frame} ; step_num: {step_num} ; {points[step_num][0]}; {points[step_num][1]}")
ax.grid(True)
return ax
fig.suptitle(f"animation with {len(timepoints)} timepoints and setting of tsteps = {tstep}.")
#ani = FuncAnimation(fig, animate, frames= np.arange(0, num_steps), interval = 1000, repeat = False)
ani = FuncAnimation(fig, animate, frames= 3, interval = 1000, repeat = False)
ani;
So you can make a controller, you can just add a couple lines and remove the ending ;
(Note the warning at the top of ignore the extra static frame or r-reun):
# If upon first running, it shows a non-interactive, single static shot of the plot below the interactive one with the widget controller,
# JUST RE-RUN TWICE. Re-run usually fixes that display quirk.
%matplotlib ipympl
# based on OP's code & code currently next to 'In [6]' at https://nbviewer.org/github/fomightez/animated_matplotlib-binder/blob/main/index.ipynb , adapted from https://discourse.jupyter.org/t/matplotlib-animation-not-appearing-in-jupyter-notebook/24938/3?u=fomightez
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.animation import FuncAnimation
plt.rcParams["animation.html"] = "jshtml"
plt.ioff() #needed so the second time you run it you get only single plot
tstep = 1
#xs, ys = block_centroids.T
xs, ys = (1,4,6),(2,5,9)
timepoints = list(zip(xs,ys))
points = list(zip(xs,ys))
num_steps = len(timepoints)
#print(points)
#print(np.arange(0, num_steps))
fig = plt.figure(figsize=(9,5))
ax = fig.add_subplot()
fig.subplots_adjust(left=0.1, right=0.85)
def animate(frame):
step_num = frame % (num_steps)
ax.scatter(points[step_num][0],points[step_num][1], c='skyblue', s=60)
ax.set_xlabel('x')
ax.set_ylabel('y')
ax.set_title(f"frame: {frame} ; step_num: {step_num} ; {points[step_num][0]}; {points[step_num][1]}")
ax.grid(True)
return ax
fig.suptitle(f"animation with {len(timepoints)} timepoints and setting of tsteps = {tstep}.")
#ani = FuncAnimation(fig, animate, frames= np.arange(0, num_steps), interval = 1000, repeat = False)
ani = FuncAnimation(fig, animate, frames= 3, interval = 1000, repeat = False)
ani