Animations#
This is an example of making animations by updating the plot. First, create a simple line, a tracker and a particle set, and track it for a couple turns:
Adjust some parameters to improve rendering performance of the animation:
# use a faster backend
xplt.mpl.rcParams["backend"] = "nbagg"
# increase buffer size
xplt.mpl.rcParams["animation.embed_limit"] = 100 # MB
# use a faster style
xplt.mpl.style.use("fast")
Phase space animation#
Use a FuncAnimation together with the update method to create an animation:
plot = xplt.PhaseSpacePlot(animated=True, std=True, mean=True)
particle_data = line.record_last_track
def animate(i):
turn = particle_data.at_turn[0, i]
plot.fig.suptitle(f"Turn {turn}")
return plot.update(
particle_data,
mask=(slice(None), i), # select all particles and a single turn
autoscale=(i == 0), # only on first frame, then freeze to avoid jitter
)
anim = FuncAnimation(
plot.fig, animate, frames=range(0, 50, 3), interval=100, blit=True
)
display(HTML(anim.to_jshtml()))
# anim.save('anim.gif', dpi=150)
# especially for larger animations, it is good practice to clean up:
plot.fig.clear()
# xplt.plt.close()
del plot
import gc
gc.collect();