Note
Go to the end to download the full example code or to run this example in your browser via Binder.
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, \
ConstantVelocity
from stonesoup.plotter import Plotter
start_time = datetime.now()
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):
ground_truth.append(GroundTruthState(
transition_model.function(ground_truth[k], noise=True,
time_interval=timedelta(seconds=1)),
timestamp=start_time+timedelta(seconds=k+1)))
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])

[<matplotlib.lines.Line2D object at 0x6ffe873cbd70>, <matplotlib.legend.Legend object at 0x6ffeb87d9d60>]
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],
num_steps=n_time_steps,
motion_model_noise=0.1)
for _ in range(100)]
plotter = Plotter()
plotter.plot_ground_truths(set(truths), [0, 2])

[<matplotlib.lines.Line2D object at 0x6ffe94b7bb00>, <matplotlib.lines.Line2D object at 0x6ffeaa9a9b80>, <matplotlib.lines.Line2D object at 0x6ffe86d803b0>, <matplotlib.lines.Line2D object at 0x6ffe86d804a0>, <matplotlib.lines.Line2D object at 0x6ffe86d800b0>, <matplotlib.lines.Line2D object at 0x6ffe86d82060>, <matplotlib.lines.Line2D object at 0x6ffe86d839e0>, <matplotlib.lines.Line2D object at 0x6ffeaaf03140>, <matplotlib.lines.Line2D object at 0x6ffe86d81f70>, <matplotlib.lines.Line2D object at 0x6ffe86d80050>, <matplotlib.lines.Line2D object at 0x6ffe86d832f0>, <matplotlib.lines.Line2D object at 0x6ffe86d816d0>, <matplotlib.lines.Line2D object at 0x6ffe86d808f0>, <matplotlib.lines.Line2D object at 0x6ffe86d83f50>, <matplotlib.lines.Line2D object at 0x6ffea91551f0>, <matplotlib.lines.Line2D object at 0x6ffea91547a0>, <matplotlib.lines.Line2D object at 0x6ffe87d114f0>, <matplotlib.lines.Line2D object at 0x6ffe87d10740>, <matplotlib.lines.Line2D object at 0x6ffe87d137a0>, <matplotlib.lines.Line2D object at 0x6ffe87d127e0>, <matplotlib.lines.Line2D object at 0x6ffe87d132c0>, <matplotlib.lines.Line2D object at 0x6ffe87d13710>, <matplotlib.lines.Line2D object at 0x6ffe87d119d0>, <matplotlib.lines.Line2D object at 0x6ffe87d10050>, <matplotlib.lines.Line2D object at 0x6ffe87d13c20>, <matplotlib.lines.Line2D object at 0x6ffe87d12720>, <matplotlib.lines.Line2D object at 0x6ffea440d220>, <matplotlib.lines.Line2D object at 0x6ffea440f9e0>, <matplotlib.lines.Line2D object at 0x6ffea440ea50>, <matplotlib.lines.Line2D object at 0x6ffea440ee40>, <matplotlib.lines.Line2D object at 0x6ffea440ddf0>, <matplotlib.lines.Line2D object at 0x6ffea440f500>, <matplotlib.lines.Line2D object at 0x6ffea440cd40>, <matplotlib.lines.Line2D object at 0x6ffea440c9e0>, <matplotlib.lines.Line2D object at 0x6ffea440d910>, <matplotlib.lines.Line2D object at 0x6ffea440dbe0>, <matplotlib.lines.Line2D object at 0x6ffea440e5d0>, <matplotlib.lines.Line2D object at 0x6ffea440e720>, <matplotlib.lines.Line2D object at 0x6ffea440d340>, <matplotlib.lines.Line2D object at 0x6ffea440fa70>, <matplotlib.lines.Line2D object at 0x6ffe945c2de0>, <matplotlib.lines.Line2D object at 0x6ffe945c0350>, <matplotlib.lines.Line2D object at 0x6ffe945c1b80>, <matplotlib.lines.Line2D object at 0x6ffe945c3380>, <matplotlib.lines.Line2D object at 0x6ffe945c2c60>, <matplotlib.lines.Line2D object at 0x6ffe945c2fc0>, <matplotlib.lines.Line2D object at 0x6ffe945c3140>, <matplotlib.lines.Line2D object at 0x6ffe945c10d0>, <matplotlib.lines.Line2D object at 0x6ffe945c22a0>, <matplotlib.lines.Line2D object at 0x6ffe945c27b0>, <matplotlib.lines.Line2D object at 0x6ffe945c38c0>, <matplotlib.lines.Line2D object at 0x6ffe945c1040>, <matplotlib.lines.Line2D object at 0x6ffe945c3080>, <matplotlib.lines.Line2D object at 0x6ffe945c02c0>, <matplotlib.lines.Line2D object at 0x6ffe945c3680>, <matplotlib.lines.Line2D object at 0x6ffe945c2bd0>, <matplotlib.lines.Line2D object at 0x6ffe945c2e70>, <matplotlib.lines.Line2D object at 0x6ffe945c2f60>, <matplotlib.lines.Line2D object at 0x6ffe945c3470>, <matplotlib.lines.Line2D object at 0x6ffe945c2cc0>, <matplotlib.lines.Line2D object at 0x6ffe873b7aa0>, <matplotlib.lines.Line2D object at 0x6ffe873b6540>, <matplotlib.lines.Line2D object at 0x6ffe873b4b00>, <matplotlib.lines.Line2D object at 0x6ffe873b58e0>, <matplotlib.lines.Line2D object at 0x6ffe873b5eb0>, <matplotlib.lines.Line2D object at 0x6ffe873b6150>, <matplotlib.lines.Line2D object at 0x6ffe873b55b0>, <matplotlib.lines.Line2D object at 0x6ffe873b5370>, <matplotlib.lines.Line2D object at 0x6ffe873b6060>, <matplotlib.lines.Line2D object at 0x6ffe873b4140>, <matplotlib.lines.Line2D object at 0x6ffe873b4050>, <matplotlib.lines.Line2D object at 0x6ffe873b5d90>, <matplotlib.lines.Line2D object at 0x6ffe873b6720>, <matplotlib.lines.Line2D object at 0x6ffe873b7c80>, <matplotlib.lines.Line2D object at 0x6ffe873b6510>, <matplotlib.lines.Line2D object at 0x6ffe873b42f0>, <matplotlib.lines.Line2D object at 0x6ffe873b5f70>, <matplotlib.lines.Line2D object at 0x6ffe873b4fe0>, <matplotlib.lines.Line2D object at 0x6ffe873b76e0>, <matplotlib.lines.Line2D object at 0x6ffe873b49e0>, <matplotlib.lines.Line2D object at 0x6ffe873b52b0>, <matplotlib.lines.Line2D object at 0x6ffe873b5df0>, <matplotlib.lines.Line2D object at 0x6ffeabe972f0>, <matplotlib.lines.Line2D object at 0x6ffea4491970>, <matplotlib.lines.Line2D object at 0x6ffea4492c90>, <matplotlib.lines.Line2D object at 0x6ffea44910a0>, <matplotlib.lines.Line2D object at 0x6ffea4491100>, <matplotlib.lines.Line2D object at 0x6ffea4491460>, <matplotlib.lines.Line2D object at 0x6ffea4492d50>, <matplotlib.lines.Line2D object at 0x6ffea4492ed0>, <matplotlib.lines.Line2D object at 0x6ffea4491d00>, <matplotlib.lines.Line2D object at 0x6ffea4491070>, <matplotlib.lines.Line2D object at 0x6ffea4492c30>, <matplotlib.lines.Line2D object at 0x6ffea4490080>, <matplotlib.lines.Line2D object at 0x6ffea4491af0>, <matplotlib.lines.Line2D object at 0x6ffea4492a80>, <matplotlib.lines.Line2D object at 0x6ffea4490ad0>, <matplotlib.lines.Line2D object at 0x6ffea44924b0>, <matplotlib.lines.Line2D object at 0x6ffea4492450>, <matplotlib.lines.Line2D object at 0x6ffea44918b0>, <matplotlib.legend.Legend object at 0x6ffe87d13050>]
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.
plotter = Plotter()
plotter.plot_density(truths, index=None)

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.
plotter = Plotter()
plotter.plot_density(truths, index=-1)

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.ax.clear()
plotter.plot_density(truths, index=i)
return plotter.ax
Finally, plot the densities over time.
plotter = Plotter()
animation.FuncAnimation(plotter.fig, update, frames=range(1, n_time_steps))
Total running time of the script: (0 minutes 10.393 seconds)