Clustering and Dimensionality Reduction

Clustering Trajectories

Trajectories can be clustered using traja.plotting.plot_clustermap().

Colors corresponding to each trajectory can be specified with the colors argument.

traja.plotting.plot_clustermap(displacements: List[Series], rule: Optional[str] = None, nr_steps=None, colors: Optional[List[Union[int, str]]] = None, **kwargs)[source]

Plot cluster map / dendrogram of trajectories with DatetimeIndex.

  • displacements – list of pd.Series, outputs of traja.calc_displacement()

  • rule – how to resample series, eg ’30s’ for 30-seconds

  • nr_steps – select first N samples for clustering

  • colors – list of colors (eg, ‘b’,’r’) to map to each trajectory

  • kwargs – keyword arguments for seaborn.clustermap()


a seaborn.matrix.ClusterGrid() instance

Return type



Requires seaborn to be installed. Install it with ‘pip install seaborn’.


Prinicipal component analysis can be used to cluster trajectories based on grid cell occupancy. PCA is computed by converting the trajectory to a trip grid (see traja.plotting.trip_grid()) followed by PCA (sklearn.decomposition.PCA).

traja.plotting.plot_pca(trj: TrajaDataFrame, id_col: str = 'id', bins: tuple = (8, 8), three_dims: bool = False, ax=None)[source]

Plot PCA comparing animals ids by trip grids.

  • Trajectory (trj -) –

  • IDs (id_col - column representing animal) –

  • grid (bins - shape for binning trajectory into a trip) –

  • Default (three_dims - 3D plot.) – False (2D plot)

  • axes (ax - Matplotlib) –


fig - Figure

(Source code)