Note
Go to the end to download the full example code.
Animating drifter trajectories#
Trajan supports animating trajectory datasets using a builder pattern.
Start with ds.traj.animate(), chain configuration methods, then call
.show() to display interactively or .save() to write a file.
import logging
import numpy as np
import xarray as xr
import coloredlogs
import trajan as ta
coloredlogs.install(level=logging.DEBUG, logger=logging.getLogger('trajan'))
Load the Barents Sea drifter dataset, drop short gaps and grid to 6-hourly.
ds = xr.open_dataset(ta.DATA_DIR + 'barents.nc')
ds = ds.traj.drop_where(ds.traj.time_to_next() < np.timedelta64(5, 'm'))
ds = ds.traj.gridtime('6h')
speed = ds.traj.speed()
2026-11-17 14:15:20 ip-10-0-129-17 trajan.accessor[4511] DEBUG Detecting trajectory dimension
2026-11-17 14:15:20 ip-10-0-129-17 trajan.accessor[4511] DEBUG Detecting time-variable for "obs"..
2026-11-17 14:15:20 ip-10-0-129-17 trajan.accessor[4511] DEBUG Detected obs-dim: obs, detected time-variable: time.
2026-11-17 14:15:20 ip-10-0-129-17 trajan.accessor[4511] DEBUG Detected Ragged trajectory dataset
2026-11-17 14:15:20 ip-10-0-129-17 trajan.accessor[4511] DEBUG Detecting trajectory dimension
2026-11-17 14:15:20 ip-10-0-129-17 trajan.accessor[4511] DEBUG Detecting time-variable for "obs"..
2026-11-17 14:15:20 ip-10-0-129-17 trajan.accessor[4511] DEBUG Detected obs-dim: obs, detected time-variable: time.
2026-11-17 14:15:20 ip-10-0-129-17 trajan.accessor[4511] DEBUG Detected Ragged trajectory dataset
2026-11-17 14:15:20 ip-10-0-129-17 trajan.accessor[4511] DEBUG Detecting trajectory dimension
2026-11-17 14:15:20 ip-10-0-129-17 trajan.accessor[4511] DEBUG Detecting time-variable for "obs"..
2026-11-17 14:15:20 ip-10-0-129-17 trajan.accessor[4511] DEBUG Detected obs-dim: obs, detected time-variable: time.
2026-11-17 14:15:20 ip-10-0-129-17 trajan.accessor[4511] DEBUG Detected Ragged trajectory dataset
2026-11-17 14:15:20 ip-10-0-129-17 trajan.accessor[4511] DEBUG Detecting trajectory dimension
2026-11-17 14:15:20 ip-10-0-129-17 trajan.accessor[4511] DEBUG Detected obs-dim: time, detected time-variable: time.
2026-11-17 14:15:20 ip-10-0-129-17 trajan.accessor[4511] DEBUG Detected Orthogonal trajectory dataset
2026-11-17 14:15:20 ip-10-0-129-17 trajan.accessor[4511] DEBUG Detecting trajectory dimension
2026-11-17 14:15:20 ip-10-0-129-17 trajan.accessor[4511] DEBUG Detecting time-variable for "obs"..
2026-11-17 14:15:20 ip-10-0-129-17 trajan.accessor[4511] DEBUG Detected obs-dim: obs, detected time-variable: time.
2026-11-17 14:15:20 ip-10-0-129-17 trajan.accessor[4511] DEBUG Detected Ragged trajectory dataset
2026-11-17 14:15:20 ip-10-0-129-17 trajan.accessor[4511] DEBUG Detecting trajectory dimension
2026-11-17 14:15:20 ip-10-0-129-17 trajan.accessor[4511] DEBUG Detected obs-dim: time, detected time-variable: time.
2026-11-17 14:15:20 ip-10-0-129-17 trajan.accessor[4511] DEBUG Detected Orthogonal trajectory dataset
2026-11-17 14:15:20 ip-10-0-129-17 trajan.accessor[4511] DEBUG Detecting trajectory dimension
2026-11-17 14:15:20 ip-10-0-129-17 trajan.accessor[4511] DEBUG Detected obs-dim: time, detected time-variable: time.
2026-11-17 14:15:20 ip-10-0-129-17 trajan.accessor[4511] DEBUG Detected Orthogonal trajectory dataset
Basic animation — just call .animate() and .show().
The title shows the current timestamp automatically.
ds.traj.animate().set_title('Barents Sea drifters').show()

2026-11-17 14:15:20 ip-10-0-129-17 trajan.traj[4511] DEBUG Setting up new plot object.
2026-11-17 14:15:20 ip-10-0-129-17 trajan.plot[4511] DEBUG Creating new figure and axes..
2026-11-17 14:15:20 ip-10-0-129-17 trajan.animation[4511] DEBUG Dataset is already regularly gridded, skipping gridtime
2026-11-17 14:15:20 ip-10-0-129-17 trajan.animation[4511] DEBUG Building animation: 192 frames
Colour particles by drift speed.
Pass the DataArray directly to color_by — no need to add it to the dataset.

2026-11-17 14:15:34 ip-10-0-129-17 trajan.plot[4511] DEBUG Creating new figure and axes..
2026-11-17 14:15:34 ip-10-0-129-17 trajan.animation[4511] DEBUG Dataset is already regularly gridded, skipping gridtime
2026-11-17 14:15:34 ip-10-0-129-17 trajan.animation[4511] DEBUG Building animation: 192 frames
2026-11-17 14:15:34 ip-10-0-129-17 matplotlib.colorbar[4511] DEBUG locator: <matplotlib.ticker.AutoLocator object at 0x7fb952ce4710>
Show static trajectory lines in the background so the full path of each drifter is always visible.

2026-11-17 14:15:49 ip-10-0-129-17 trajan.plot[4511] DEBUG Creating new figure and axes..
2026-11-17 14:15:49 ip-10-0-129-17 trajan.animation[4511] DEBUG Dataset is already regularly gridded, skipping gridtime
2026-11-17 14:15:49 ip-10-0-129-17 trajan.animation[4511] DEBUG Building animation: 192 frames
2026-11-17 14:15:49 ip-10-0-129-17 trajan.plot[4511] DEBUG Plotting lines
2026-11-17 14:15:49 ip-10-0-129-17 trajan.plot[4511] DEBUG Plotting to existing Cartopy axes
2026-11-17 14:15:49 ip-10-0-129-17 matplotlib.colorbar[4511] DEBUG locator: <matplotlib.ticker.AutoLocator object at 0x7fb94f038d40>