Density Plot Example

This example looks at how to plot 2d density plots. The Plotter().plot_density function can be used to plot any number of StateMutableSequence objects. StateMutableSequences are just a container for a number of states - examples include tracks and ground truth paths. The examples below show how to plot ground truth paths (as they’re easy to generate). The function can be used to analyse large data sets.

Set the imports and set the start time

from datetime import datetime, timedelta
from matplotlib import animation

from stonesoup.types.groundtruth import GroundTruthPath, GroundTruthState
from stonesoup.models.transition.linear import CombinedLinearGaussianTransitionModel, \
from stonesoup.plotter import Plotter

start_time =

Generate the State Sequences to Plot

In this example we will plot ground truth paths due to their ease of creation. The ground truths are created in the following function:

def generate_ground_truth_path(initial_state, num_steps=20, motion_model_noise=0.01):

    transition_model = CombinedLinearGaussianTransitionModel(
        [ConstantVelocity(motion_model_noise), ConstantVelocity(motion_model_noise)])

    ground_truth = GroundTruthPath([GroundTruthState(initial_state, timestamp=start_time)])

    for k in range(0, num_steps):
            transition_model.function(ground_truth[k], noise=True,

    return ground_truth

Create one ground truth path starting at the origin (0, 0) and plot it:

n_time_steps = 20
truth = generate_ground_truth_path(initial_state=[0, 0, 0, 1], num_steps=n_time_steps)

plotter = Plotter()
plotter.plot_ground_truths(truth, [0, 2])
density plot example
[<matplotlib.lines.Line2D object at 0x7f2f77591d60>, <matplotlib.legend.Legend object at 0x7f2f74adbfb0>]

Generate 100 ground truth paths and plot them all at once. This looks quite messy:

truths = [generate_ground_truth_path(initial_state=[0, 0, 0, 1],
          for _ in range(100)]

plotter = Plotter()
plotter.plot_ground_truths(set(truths), [0, 2])
density plot example
[<matplotlib.lines.Line2D object at 0x7f2f74a84290>, <matplotlib.lines.Line2D object at 0x7f2f738dd1f0>, <matplotlib.lines.Line2D object at 0x7f2f738ddac0>, <matplotlib.lines.Line2D object at 0x7f2f738ddc70>, <matplotlib.lines.Line2D object at 0x7f2f738ddf40>, <matplotlib.lines.Line2D object at 0x7f2f738de270>, <matplotlib.lines.Line2D object at 0x7f2f738dfd10>, <matplotlib.lines.Line2D object at 0x7f2f738df8c0>, <matplotlib.lines.Line2D object at 0x7f2f738df770>, <matplotlib.lines.Line2D object at 0x7f2f738df200>, <matplotlib.lines.Line2D object at 0x7f2f73aeb110>, <matplotlib.lines.Line2D object at 0x7f2f73ae8c50>, <matplotlib.lines.Line2D object at 0x7f2f73ae9790>, <matplotlib.lines.Line2D object at 0x7f2f73aea420>, <matplotlib.lines.Line2D object at 0x7f2f73ae9520>, <matplotlib.lines.Line2D object at 0x7f2f73ae9a30>, <matplotlib.lines.Line2D object at 0x7f2f73aea450>, <matplotlib.lines.Line2D object at 0x7f2f73aebef0>, <matplotlib.lines.Line2D object at 0x7f2f73ae8800>, <matplotlib.lines.Line2D object at 0x7f2f73ae92b0>, <matplotlib.lines.Line2D object at 0x7f2f73ae9190>, <matplotlib.lines.Line2D object at 0x7f2f73ae8e30>, <matplotlib.lines.Line2D object at 0x7f2f73ae98b0>, <matplotlib.lines.Line2D object at 0x7f2f73ae9a90>, <matplotlib.lines.Line2D object at 0x7f2f73aea4b0>, <matplotlib.lines.Line2D object at 0x7f2f73aea600>, <matplotlib.lines.Line2D object at 0x7f2f73aeb290>, <matplotlib.lines.Line2D object at 0x7f2f73ae9d00>, <matplotlib.lines.Line2D object at 0x7f2f73aeb410>, <matplotlib.lines.Line2D object at 0x7f2f73c98c50>, <matplotlib.lines.Line2D object at 0x7f2f73c9abd0>, <matplotlib.lines.Line2D object at 0x7f2f73c9a3c0>, <matplotlib.lines.Line2D object at 0x7f2f74a5e570>, <matplotlib.lines.Line2D object at 0x7f2f74a5f0b0>, <matplotlib.lines.Line2D object at 0x7f2f74a5fc50>, <matplotlib.lines.Line2D object at 0x7f2f74a5f350>, <matplotlib.lines.Line2D object at 0x7f2f74a5ce60>, <matplotlib.lines.Line2D object at 0x7f2f74a5fec0>, <matplotlib.lines.Line2D object at 0x7f2f7490a7b0>, <matplotlib.lines.Line2D object at 0x7f2f74908d10>, <matplotlib.lines.Line2D object at 0x7f2f749099a0>, <matplotlib.lines.Line2D object at 0x7f2f74909ee0>, <matplotlib.lines.Line2D object at 0x7f2f7490bb00>, <matplotlib.lines.Line2D object at 0x7f2f74908770>, <matplotlib.lines.Line2D object at 0x7f2f7490a0f0>, <matplotlib.lines.Line2D object at 0x7f2f7490b350>, <matplotlib.lines.Line2D object at 0x7f2f749093d0>, <matplotlib.lines.Line2D object at 0x7f2f7490a4e0>, <matplotlib.lines.Line2D object at 0x7f2f7490ad50>, <matplotlib.lines.Line2D object at 0x7f2f7490b260>, <matplotlib.lines.Line2D object at 0x7f2f74a3f050>, <matplotlib.lines.Line2D object at 0x7f2f74a3dc40>, <matplotlib.lines.Line2D object at 0x7f2f74a3d6d0>, <matplotlib.lines.Line2D object at 0x7f2f74a3d9d0>, <matplotlib.lines.Line2D object at 0x7f2f74de49e0>, <matplotlib.lines.Line2D object at 0x7f2f74de67b0>, <matplotlib.lines.Line2D object at 0x7f2f74de5c10>, <matplotlib.lines.Line2D object at 0x7f2f74de5100>, <matplotlib.lines.Line2D object at 0x7f2f74de4770>, <matplotlib.lines.Line2D object at 0x7f2f74de74d0>, <matplotlib.lines.Line2D object at 0x7f2f74de5df0>, <matplotlib.lines.Line2D object at 0x7f2f74de5430>, <matplotlib.lines.Line2D object at 0x7f2f74de65a0>, <matplotlib.lines.Line2D object at 0x7f2f74de60c0>, <matplotlib.lines.Line2D object at 0x7f2f74de5850>, <matplotlib.lines.Line2D object at 0x7f2f74de7560>, <matplotlib.lines.Line2D object at 0x7f2f74de6cf0>, <matplotlib.lines.Line2D object at 0x7f2f74ac1970>, <matplotlib.lines.Line2D object at 0x7f2f74ac0b00>, <matplotlib.lines.Line2D object at 0x7f2f74ac1fd0>, <matplotlib.lines.Line2D object at 0x7f2f74ac1be0>, <matplotlib.lines.Line2D object at 0x7f2f74ac1b50>, <matplotlib.lines.Line2D object at 0x7f2f74ac1c70>, <matplotlib.lines.Line2D object at 0x7f2f74ac0a70>, <matplotlib.lines.Line2D object at 0x7f2f74a9baa0>, <matplotlib.lines.Line2D object at 0x7f2f74a98b00>, <matplotlib.lines.Line2D object at 0x7f2f74a99400>, <matplotlib.lines.Line2D object at 0x7f2f74a99520>, <matplotlib.lines.Line2D object at 0x7f2f76d2c860>, <matplotlib.lines.Line2D object at 0x7f2f76d2e030>, <matplotlib.lines.Line2D object at 0x7f2f7fe93020>, <matplotlib.lines.Line2D object at 0x7f2f74cbcfe0>, <matplotlib.lines.Line2D object at 0x7f2f74cbcd40>, <matplotlib.lines.Line2D object at 0x7f2f74cbdf40>, <matplotlib.lines.Line2D object at 0x7f2f74cbf380>, <matplotlib.lines.Line2D object at 0x7f2f74cbc860>, <matplotlib.lines.Line2D object at 0x7f2f74cbe930>, <matplotlib.lines.Line2D object at 0x7f2f74cbcef0>, <matplotlib.lines.Line2D object at 0x7f2f74cbe2a0>, <matplotlib.lines.Line2D object at 0x7f2f74cbf590>, <matplotlib.lines.Line2D object at 0x7f2f74cbe9f0>, <matplotlib.lines.Line2D object at 0x7f2f74cbc4a0>, <matplotlib.lines.Line2D object at 0x7f2f74cbd910>, <matplotlib.lines.Line2D object at 0x7f2f74cbe7e0>, <matplotlib.lines.Line2D object at 0x7f2f74cbfa70>, <matplotlib.lines.Line2D object at 0x7f2f74cbf1d0>, <matplotlib.lines.Line2D object at 0x7f2f74cbc920>, <matplotlib.lines.Line2D object at 0x7f2f74cbd880>, <matplotlib.lines.Line2D object at 0x7f2f74cbed80>, <matplotlib.lines.Line2D object at 0x7f2f74cbcb60>, <matplotlib.legend.Legend object at 0x7f2f74af5100>]

Density Plot of All States

Plot a 2D density plot for all the states in the ground truths. This is a better visualisation because we can see a clear concentration of states around the origin where all the tracks start.

density plot example

Plot of the Last State of the Ground Truths

The function allows you to pick an index of the state sequence (ground truth in this example) to plot. In this example we’re only interested in the final state of the sequences. An index of ‘-1’ is the last state in the sequence. The resultant plot is much more spread out.

density plot example

Plot each state over time

Plot the density at each time-step and see how the density plot evolves. Define an animation update function.

def update(i):
    plotter.plot_density(truths, index=i)

Finally, plot the densities over time.