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-07-21 14:51:24 runnervm3jd5f trajan.accessor[2874] DEBUG Detecting trajectory dimension
2026-07-21 14:51:24 runnervm3jd5f trajan.accessor[2874] DEBUG Detecting time-variable for "obs"..
2026-07-21 14:51:24 runnervm3jd5f trajan.accessor[2874] DEBUG Detected obs-dim: obs, detected time-variable: time.
2026-07-21 14:51:24 runnervm3jd5f trajan.accessor[2874] DEBUG Detected Ragged trajectory dataset
2026-07-21 14:51:25 runnervm3jd5f trajan.accessor[2874] DEBUG Detecting trajectory dimension
2026-07-21 14:51:25 runnervm3jd5f trajan.accessor[2874] DEBUG Detecting time-variable for "obs"..
2026-07-21 14:51:25 runnervm3jd5f trajan.accessor[2874] DEBUG Detected obs-dim: obs, detected time-variable: time.
2026-07-21 14:51:25 runnervm3jd5f trajan.accessor[2874] DEBUG Detected Ragged trajectory dataset
2026-07-21 14:51:25 runnervm3jd5f trajan.accessor[2874] DEBUG Detecting trajectory dimension
2026-07-21 14:51:25 runnervm3jd5f trajan.accessor[2874] DEBUG Detecting time-variable for "obs"..
2026-07-21 14:51:25 runnervm3jd5f trajan.accessor[2874] DEBUG Detected obs-dim: obs, detected time-variable: time.
2026-07-21 14:51:25 runnervm3jd5f trajan.accessor[2874] DEBUG Detected Ragged trajectory dataset
2026-07-21 14:51:25 runnervm3jd5f trajan.accessor[2874] DEBUG Detecting trajectory dimension
2026-07-21 14:51:25 runnervm3jd5f trajan.accessor[2874] DEBUG Detected obs-dim: time, detected time-variable: time.
2026-07-21 14:51:25 runnervm3jd5f trajan.accessor[2874] DEBUG Detected Orthogonal trajectory dataset
2026-07-21 14:51:25 runnervm3jd5f trajan.accessor[2874] DEBUG Detecting trajectory dimension
2026-07-21 14:51:25 runnervm3jd5f trajan.accessor[2874] DEBUG Detecting time-variable for "obs"..
2026-07-21 14:51:25 runnervm3jd5f trajan.accessor[2874] DEBUG Detected obs-dim: obs, detected time-variable: time.
2026-07-21 14:51:25 runnervm3jd5f trajan.accessor[2874] DEBUG Detected Ragged trajectory dataset
2026-07-21 14:51:25 runnervm3jd5f trajan.accessor[2874] DEBUG Detecting trajectory dimension
2026-07-21 14:51:25 runnervm3jd5f trajan.accessor[2874] DEBUG Detected obs-dim: time, detected time-variable: time.
2026-07-21 14:51:25 runnervm3jd5f trajan.accessor[2874] DEBUG Detected Orthogonal trajectory dataset
2026-07-21 14:51:25 runnervm3jd5f trajan.accessor[2874] DEBUG Detecting trajectory dimension
2026-07-21 14:51:25 runnervm3jd5f trajan.accessor[2874] DEBUG Detected obs-dim: time, detected time-variable: time.
2026-07-21 14:51:25 runnervm3jd5f trajan.accessor[2874] 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-07-21 14:51:25 runnervm3jd5f trajan.traj[2874] DEBUG Setting up new plot object.
2026-07-21 14:51:25 runnervm3jd5f trajan.traj[2874] DEBUG No grid-mapping specified, checking if coordinates are lon/lat..
2026-07-21 14:51:25 runnervm3jd5f trajan.traj[2874] DEBUG No grid-mapping specified, checking if coordinates are lon/lat..
2026-07-21 14:51:25 runnervm3jd5f trajan.traj[2874] DEBUG No grid-mapping specified, checking if coordinates are lon/lat..
2026-07-21 14:51:25 runnervm3jd5f trajan.traj[2874] DEBUG No grid-mapping specified, checking if coordinates are lon/lat..
2026-07-21 14:51:25 runnervm3jd5f trajan.traj[2874] DEBUG No grid-mapping specified, checking if coordinates are lon/lat..
2026-07-21 14:51:25 runnervm3jd5f trajan.traj[2874] DEBUG No grid-mapping specified, checking if coordinates are lon/lat..
2026-07-21 14:51:25 runnervm3jd5f trajan.traj[2874] DEBUG No grid-mapping specified, checking if coordinates are lon/lat..
2026-07-21 14:51:25 runnervm3jd5f trajan.traj[2874] DEBUG No grid-mapping specified, checking if coordinates are lon/lat..
2026-07-21 14:51:25 runnervm3jd5f trajan.traj[2874] DEBUG No grid-mapping specified, checking if coordinates are lon/lat..
2026-07-21 14:51:25 runnervm3jd5f trajan.plot[2874] DEBUG Creating new figure and axes..
2026-07-21 14:51:25 runnervm3jd5f trajan.animation[2874] DEBUG Dataset is already regularly gridded, skipping gridtime
2026-07-21 14:51:25 runnervm3jd5f trajan.animation[2874] DEBUG Building animation: 192 frames
2026-07-21 14:51:25 runnervm3jd5f trajan.traj[2874] DEBUG No grid-mapping specified, checking if coordinates are lon/lat..
2026-07-21 14:51:25 runnervm3jd5f trajan.traj[2874] DEBUG No grid-mapping specified, checking if coordinates are lon/lat..
2026-07-21 14:51:25 runnervm3jd5f trajan.traj[2874] DEBUG No grid-mapping specified, checking if coordinates are lon/lat..
2026-07-21 14:51:25 runnervm3jd5f trajan.traj[2874] DEBUG No grid-mapping specified, checking if coordinates are lon/lat..
2026-07-21 14:51:25 runnervm3jd5f trajan.traj[2874] DEBUG No grid-mapping specified, checking if coordinates are lon/lat..
Colour particles by drift speed.
Pass the DataArray directly to color_by — no need to add it to the dataset.

2026-07-21 14:52:05 runnervm3jd5f trajan.traj[2874] DEBUG No grid-mapping specified, checking if coordinates are lon/lat..
2026-07-21 14:52:05 runnervm3jd5f trajan.traj[2874] DEBUG No grid-mapping specified, checking if coordinates are lon/lat..
2026-07-21 14:52:05 runnervm3jd5f trajan.traj[2874] DEBUG No grid-mapping specified, checking if coordinates are lon/lat..
2026-07-21 14:52:05 runnervm3jd5f trajan.traj[2874] DEBUG No grid-mapping specified, checking if coordinates are lon/lat..
2026-07-21 14:52:05 runnervm3jd5f trajan.traj[2874] DEBUG No grid-mapping specified, checking if coordinates are lon/lat..
2026-07-21 14:52:05 runnervm3jd5f trajan.traj[2874] DEBUG No grid-mapping specified, checking if coordinates are lon/lat..
2026-07-21 14:52:05 runnervm3jd5f trajan.traj[2874] DEBUG No grid-mapping specified, checking if coordinates are lon/lat..
2026-07-21 14:52:05 runnervm3jd5f trajan.traj[2874] DEBUG No grid-mapping specified, checking if coordinates are lon/lat..
2026-07-21 14:52:05 runnervm3jd5f trajan.traj[2874] DEBUG No grid-mapping specified, checking if coordinates are lon/lat..
2026-07-21 14:52:05 runnervm3jd5f trajan.plot[2874] DEBUG Creating new figure and axes..
2026-07-21 14:52:05 runnervm3jd5f trajan.animation[2874] DEBUG Dataset is already regularly gridded, skipping gridtime
2026-07-21 14:52:05 runnervm3jd5f trajan.animation[2874] DEBUG Building animation: 192 frames
2026-07-21 14:52:05 runnervm3jd5f trajan.traj[2874] DEBUG No grid-mapping specified, checking if coordinates are lon/lat..
2026-07-21 14:52:05 runnervm3jd5f trajan.traj[2874] DEBUG No grid-mapping specified, checking if coordinates are lon/lat..
2026-07-21 14:52:05 runnervm3jd5f trajan.traj[2874] DEBUG No grid-mapping specified, checking if coordinates are lon/lat..
2026-07-21 14:52:05 runnervm3jd5f trajan.traj[2874] DEBUG No grid-mapping specified, checking if coordinates are lon/lat..
2026-07-21 14:52:05 runnervm3jd5f trajan.traj[2874] DEBUG No grid-mapping specified, checking if coordinates are lon/lat..
2026-07-21 14:52:05 runnervm3jd5f matplotlib.colorbar[2874] DEBUG locator: <matplotlib.ticker.AutoLocator object at 0x7f5586fe68d0>
Show static trajectory lines in the background so the full path of each drifter is always visible.

2026-07-21 14:52:48 runnervm3jd5f trajan.traj[2874] DEBUG No grid-mapping specified, checking if coordinates are lon/lat..
2026-07-21 14:52:48 runnervm3jd5f trajan.traj[2874] DEBUG No grid-mapping specified, checking if coordinates are lon/lat..
2026-07-21 14:52:48 runnervm3jd5f trajan.traj[2874] DEBUG No grid-mapping specified, checking if coordinates are lon/lat..
2026-07-21 14:52:48 runnervm3jd5f trajan.traj[2874] DEBUG No grid-mapping specified, checking if coordinates are lon/lat..
2026-07-21 14:52:48 runnervm3jd5f trajan.traj[2874] DEBUG No grid-mapping specified, checking if coordinates are lon/lat..
2026-07-21 14:52:48 runnervm3jd5f trajan.traj[2874] DEBUG No grid-mapping specified, checking if coordinates are lon/lat..
2026-07-21 14:52:48 runnervm3jd5f trajan.traj[2874] DEBUG No grid-mapping specified, checking if coordinates are lon/lat..
2026-07-21 14:52:48 runnervm3jd5f trajan.traj[2874] DEBUG No grid-mapping specified, checking if coordinates are lon/lat..
2026-07-21 14:52:48 runnervm3jd5f trajan.traj[2874] DEBUG No grid-mapping specified, checking if coordinates are lon/lat..
2026-07-21 14:52:48 runnervm3jd5f trajan.plot[2874] DEBUG Creating new figure and axes..
2026-07-21 14:52:48 runnervm3jd5f trajan.animation[2874] DEBUG Dataset is already regularly gridded, skipping gridtime
2026-07-21 14:52:48 runnervm3jd5f trajan.animation[2874] DEBUG Building animation: 192 frames
2026-07-21 14:52:48 runnervm3jd5f trajan.traj[2874] DEBUG No grid-mapping specified, checking if coordinates are lon/lat..
2026-07-21 14:52:48 runnervm3jd5f trajan.traj[2874] DEBUG No grid-mapping specified, checking if coordinates are lon/lat..
2026-07-21 14:52:48 runnervm3jd5f trajan.traj[2874] DEBUG No grid-mapping specified, checking if coordinates are lon/lat..
2026-07-21 14:52:48 runnervm3jd5f trajan.traj[2874] DEBUG No grid-mapping specified, checking if coordinates are lon/lat..
2026-07-21 14:52:48 runnervm3jd5f trajan.traj[2874] DEBUG No grid-mapping specified, checking if coordinates are lon/lat..
2026-07-21 14:52:48 runnervm3jd5f trajan.plot[2874] DEBUG Plotting lines
2026-07-21 14:52:48 runnervm3jd5f trajan.traj[2874] DEBUG No grid-mapping specified, checking if coordinates are lon/lat..
2026-07-21 14:52:48 runnervm3jd5f trajan.plot[2874] DEBUG axes already set up
2026-07-21 14:52:48 runnervm3jd5f trajan.traj[2874] DEBUG No grid-mapping specified, checking if coordinates are lon/lat..
2026-07-21 14:52:48 runnervm3jd5f trajan.traj[2874] DEBUG No grid-mapping specified, checking if coordinates are lon/lat..
2026-07-21 14:52:48 runnervm3jd5f matplotlib.colorbar[2874] DEBUG locator: <matplotlib.ticker.AutoLocator object at 0x7f559415a3c0>