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 these examples we’re going plot ground-truth as that is the easiest to generate. An simple function has been created to ground truth
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 0x7f6c71f57650>, <matplotlib.legend.Legend object at 0x7f6c72071e10>]
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 0x7f6c73b61190>, <matplotlib.lines.Line2D object at 0x7f6c73cfc650>, <matplotlib.lines.Line2D object at 0x7f6c73cfc750>, <matplotlib.lines.Line2D object at 0x7f6c73cffc10>, <matplotlib.lines.Line2D object at 0x7f6c73cfec90>, <matplotlib.lines.Line2D object at 0x7f6c73cffcd0>, <matplotlib.lines.Line2D object at 0x7f6c73cfe390>, <matplotlib.lines.Line2D object at 0x7f6c73a479d0>, <matplotlib.lines.Line2D object at 0x7f6c73cfc710>, <matplotlib.lines.Line2D object at 0x7f6c73cfebd0>, <matplotlib.lines.Line2D object at 0x7f6c74973a90>, <matplotlib.lines.Line2D object at 0x7f6c73b7d410>, <matplotlib.lines.Line2D object at 0x7f6c73b7c790>, <matplotlib.lines.Line2D object at 0x7f6c73b7dbd0>, <matplotlib.lines.Line2D object at 0x7f6c73b7ddd0>, <matplotlib.lines.Line2D object at 0x7f6c73b7ea50>, <matplotlib.lines.Line2D object at 0x7f6c73b7fe50>, <matplotlib.lines.Line2D object at 0x7f6c73b7c150>, <matplotlib.lines.Line2D object at 0x7f6c7407a210>, <matplotlib.lines.Line2D object at 0x7f6c73b7fb90>, <matplotlib.lines.Line2D object at 0x7f6c73df3dd0>, <matplotlib.lines.Line2D object at 0x7f6c73df1f10>, <matplotlib.lines.Line2D object at 0x7f6c73df0090>, <matplotlib.lines.Line2D object at 0x7f6c731bef50>, <matplotlib.lines.Line2D object at 0x7f6c71ea0710>, <matplotlib.lines.Line2D object at 0x7f6c71b35f90>, <matplotlib.lines.Line2D object at 0x7f6c75faa810>, <matplotlib.lines.Line2D object at 0x7f6c74b29e90>, <matplotlib.lines.Line2D object at 0x7f6c73bebe50>, <matplotlib.lines.Line2D object at 0x7f6c722a0a10>, <matplotlib.lines.Line2D object at 0x7f6c73b632d0>, <matplotlib.lines.Line2D object at 0x7f6c71d66c90>, <matplotlib.lines.Line2D object at 0x7f6c71d678d0>, <matplotlib.lines.Line2D object at 0x7f6c71d67290>, <matplotlib.lines.Line2D object at 0x7f6c71d67bd0>, <matplotlib.lines.Line2D object at 0x7f6c71d67cd0>, <matplotlib.lines.Line2D object at 0x7f6c71e1fc50>, <matplotlib.lines.Line2D object at 0x7f6c75d03110>, <matplotlib.lines.Line2D object at 0x7f6c71d5ba90>, <matplotlib.lines.Line2D object at 0x7f6c71d58290>, <matplotlib.lines.Line2D object at 0x7f6c71d5bc90>, <matplotlib.lines.Line2D object at 0x7f6c71d5a490>, <matplotlib.lines.Line2D object at 0x7f6c7c8e2550>, <matplotlib.lines.Line2D object at 0x7f6c71d4fcd0>, <matplotlib.lines.Line2D object at 0x7f6c73e965d0>, <matplotlib.lines.Line2D object at 0x7f6c71d3af10>, <matplotlib.lines.Line2D object at 0x7f6c73a47210>, <matplotlib.lines.Line2D object at 0x7f6c74b2b990>, <matplotlib.lines.Line2D object at 0x7f6c73a45410>, <matplotlib.lines.Line2D object at 0x7f6c71ccdb50>, <matplotlib.lines.Line2D object at 0x7f6c71ccc1d0>, <matplotlib.lines.Line2D object at 0x7f6c71ccccd0>, <matplotlib.lines.Line2D object at 0x7f6c73b7cf90>, <matplotlib.lines.Line2D object at 0x7f6c73f85d90>, <matplotlib.lines.Line2D object at 0x7f6c720958d0>, <matplotlib.lines.Line2D object at 0x7f6c71d74b50>, <matplotlib.lines.Line2D object at 0x7f6c71d76b10>, <matplotlib.lines.Line2D object at 0x7f6c73b94490>, <matplotlib.lines.Line2D object at 0x7f6c73b96390>, <matplotlib.lines.Line2D object at 0x7f6c73b94250>, <matplotlib.lines.Line2D object at 0x7f6c74972c90>, <matplotlib.lines.Line2D object at 0x7f6c73b95050>, <matplotlib.lines.Line2D object at 0x7f6c73a46c10>, <matplotlib.lines.Line2D object at 0x7f6c73b1d510>, <matplotlib.lines.Line2D object at 0x7f6c73b97d10>, <matplotlib.lines.Line2D object at 0x7f6c73b61c50>, <matplotlib.lines.Line2D object at 0x7f6c73beb710>, <matplotlib.lines.Line2D object at 0x7f6c71d67dd0>, <matplotlib.lines.Line2D object at 0x7f6c73bc9f50>, <matplotlib.lines.Line2D object at 0x7f6c73df3290>, <matplotlib.lines.Line2D object at 0x7f6c73bcac90>, <matplotlib.lines.Line2D object at 0x7f6c73bcbed0>, <matplotlib.lines.Line2D object at 0x7f6c73beb410>, <matplotlib.lines.Line2D object at 0x7f6c73bc9510>, <matplotlib.lines.Line2D object at 0x7f6c73bcb310>, <matplotlib.lines.Line2D object at 0x7f6c71d11650>, <matplotlib.lines.Line2D object at 0x7f6c73c7c350>, <matplotlib.lines.Line2D object at 0x7f6c74cc0f90>, <matplotlib.lines.Line2D object at 0x7f6c73c7f910>, <matplotlib.lines.Line2D object at 0x7f6c73c7e7d0>, <matplotlib.lines.Line2D object at 0x7f6c73c7c0d0>, <matplotlib.lines.Line2D object at 0x7f6c73c7ca90>, <matplotlib.lines.Line2D object at 0x7f6c73cfc1d0>, <matplotlib.lines.Line2D object at 0x7f6c73dc7990>, <matplotlib.lines.Line2D object at 0x7f6c73dc4390>, <matplotlib.lines.Line2D object at 0x7f6c73dc6810>, <matplotlib.lines.Line2D object at 0x7f6c73dc4490>, <matplotlib.lines.Line2D object at 0x7f6c743996d0>, <matplotlib.lines.Line2D object at 0x7f6c73a55b90>, <matplotlib.lines.Line2D object at 0x7f6c73a57dd0>, <matplotlib.lines.Line2D object at 0x7f6c71d77190>, <matplotlib.lines.Line2D object at 0x7f6c73a57d50>, <matplotlib.lines.Line2D object at 0x7f6c73a55290>, <matplotlib.lines.Line2D object at 0x7f6c73a558d0>, <matplotlib.lines.Line2D object at 0x7f6c73b95bd0>, <matplotlib.lines.Line2D object at 0x7f6c73cfed50>, <matplotlib.lines.Line2D object at 0x7f6c721ed350>, <matplotlib.lines.Line2D object at 0x7f6c73bcb290>, <matplotlib.lines.Line2D object at 0x7f6c73d2a750>, <matplotlib.lines.Line2D object at 0x7f6c74cc3050>, <matplotlib.legend.Legend object at 0x7f6c73b96d10>]
Density Plot of All States
Plot a 2d density plot for all the states in the ground-truth. This is clearer, we can see a clear concentration 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
Plot the densities over time.
plotter = Plotter()
animation.FuncAnimation(plotter.fig, update, frames=range(1, n_time_steps))