Animated Plotters

This example shows how to animate several 2D state sequences to be plotted in time order. First, truths, detections, and tracks are created. These are then plotted as animations using two options that are provided by Stone Soup: Matplotlib-based AnimationPlotter, and Plotly-based AnimatedPlotterly. The two options are then compared with pros and cons given for both options.

Creating Ground Truths, Detections, and Tracks

For simplicity, we are going to quickly make a simulation with a basic Kalman Tracker using Stone Soup simulators. To see the animations in action, scroll down to “Creating Animations”.

All non-generic imports will be given in order of usage.

from datetime import datetime, timedelta

import numpy as np

from stonesoup.dataassociator.neighbour import GNNWith2DAssignment
from stonesoup.deleter.error import CovarianceBasedDeleter
from stonesoup.hypothesiser.distance import DistanceHypothesiser
from stonesoup.initiator.simple import MultiMeasurementInitiator
from stonesoup.measures import Mahalanobis
from stonesoup.models.transition.linear import (
    CombinedLinearGaussianTransitionModel, ConstantVelocity)
from stonesoup.models.measurement.linear import LinearGaussian
from stonesoup.predictor.kalman import KalmanPredictor
from stonesoup.simulator.simple import MultiTargetGroundTruthSimulator, SimpleDetectionSimulator
from stonesoup.tracker.simple import MultiTargetTracker
from stonesoup.types.array import StateVector, CovarianceMatrix
from stonesoup.types.state import GaussianState
from stonesoup.updater.kalman import KalmanUpdater

Set up the platform and detection simulators:

# Models
transition_model = CombinedLinearGaussianTransitionModel(
    [ConstantVelocity(1), ConstantVelocity(1)])
measurement_model = LinearGaussian(4, [0, 2], np.diag([20, 20]))

start_time =
timestep = timedelta(seconds=5)

# Simulators
groundtruth_sim = MultiTargetGroundTruthSimulator(
        StateVector([[0], [0], [0], [0]]),
        CovarianceMatrix(np.diag([1000, 10, 1000, 10])),
detection_sim = SimpleDetectionSimulator(
    meas_range=np.array([[-1, 1], [-1, 1]]) * 2500,  # Area to generate clutter

Set up the tracker:

Generate the Truths, Detections, and Tracks:

groundtruth = set()
detections = set()
all_tracks = set()

for time, tracks in tracker:

Simulation overview:

from stonesoup.types.detection import Clutter, TrueDetection

average_life_of_gt = timestep * sum(len(gt) for gt in groundtruth)/len(groundtruth)
n_clutter = sum(isinstance(det, Clutter) for det in detections)
n_true_detections = sum(isinstance(det, TrueDetection) for det in detections)
average_life_of_track = timestep * sum(len(track) for track in all_tracks)/len(all_tracks)

print("The simulation produced:\n",
      len(groundtruth), "Ground truth paths with an average life of", average_life_of_gt, "\n",
      n_clutter, "Clutter Detections\n",
      n_true_detections, "True Detections\n",
      len(all_tracks), "Tracks with an average life of", average_life_of_track, "\n", )
The simulation produced:
 16 Ground truth paths with an average life of 0:01:24.062500
 62 Clutter Detections
 243 True Detections
 15 Tracks with an average life of 0:01:33

Creating Animations

We now create animations using both plotters and compare them.


AnimationPlotter is built on Matplotlib. Here we show off some of its functionality, and save the output. First, we create the plotter object and add an argument for the legend. One drawback with this plotter is that the user cannot currently set a custom title.

from stonesoup.plotter import AnimationPlotter
plotter = AnimationPlotter(legend_kwargs=dict(loc='upper left'))

Plot the truths, detections, and tracks, and provide the mapping from state space to Cartesian:

The following command ensures the animation is playable via the interactive player in Jupyter Notebooks:

import matplotlib
matplotlib.rcParams['animation.html'] = 'jshtml'

Run the animation. To prevent a cluttered plot, include an argument that deletes information older than 60 seconds: