trajan.animation.Animation#
- class trajan.animation.Animation(ds)[source]#
Builder for trajectory animations.
Obtain via
ds.traj.animate, configure with method chaining, then callshow()orsave()to render.Examples
Simple:
>>> ds.traj.animate().show()
Chained:
>>> (ds.traj.animate() ... .color_by('speed', cmap='RdYlBu_r') ... .show_trajectories(alpha=0.2) ... .save('barents.mp4'))
With background field:
>>> (ds.traj.animate() ... .overlay_variable(current_speed_da, cmap='Blues', label='speed [m/s]') ... .show())
Methods
__init__(ds)build([blit])Build the
matplotlib.animation.FuncAnimation.color_by(variable[, cmap, vmin, vmax, ...])Color particles by a variable or a fixed matplotlib colour.
overlay_variable(data[, cmap, alpha, vmin, ...])Overlay an animated gridded variable as a background field.
save(filename[, fps])Save the animation to a file.
set_fps(fps)Set the playback speed.
set_markersize(size)Set the particle marker size.
set_title(title)Control the axes title.
show()Display the animation.
show_trajectories([alpha])Draw full trajectory lines as a static background.
Attributes
- DEFAULT_LINE_COLOR = 'gray'#
- build(blit=True)[source]#
Build the
matplotlib.animation.FuncAnimation.The result is cached (for
blit=Trueonly); callset_fps()to invalidate the cache and rebuild on the next call.- Parameters:
blit (
bool, optional) – Use blitting for faster rendering (defaultTrue). PassFalsewhen the output will be embedded as jshtml (Jupyter) to avoid artefacts caused by the blit background-capture path.- Returns:
- color_by(variable, cmap=None, vmin=None, vmax=None, label=None, colorbar=True)[source]#
Color particles by a variable or a fixed matplotlib colour.
- Parameters:
variable (
strorxarray.DataArray) – A DataArray with dims(trajectory, time), a dataset variable name (e.g.'speed'), or a fixed matplotlib colour string (e.g.'red').cmap (
strorColormap, optional) – Colormap (default:'jet').vmin, vmax (
float, optional) – Colour scale limits.label (
str, optional) – Colorbar label.colorbar (
bool, optional) – Whether to draw a colorbar (default:True).
- Returns:
self
- ds#
- gcrs = None#
- overlay_variable(data, cmap='viridis', alpha=0.5, vmin=None, vmax=None, label=None)[source]#
Overlay an animated gridded variable as a background field.
- Parameters:
data (
xarray.DataArray) – Must have atimedimension andlat/lon(orlatitude/longitude) coordinates.cmap (
strorColormap, optional)alpha (
float, optional)vmin, vmax (
float, optional)label (
str, optional) – Colorbar label.
- Returns:
self
- show()[source]#
Display the animation.
In a Jupyter notebook the animation is embedded as HTML; in a script
matplotlib.pyplot.show()is called.- Returns:
self