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 0x7de167e09400>, <matplotlib.legend.Legend object at 0x7de167e09be0>]
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 0x7de16c437b60>, <matplotlib.lines.Line2D object at 0x7de16c435e80>, <matplotlib.lines.Line2D object at 0x7de16c4378c0>, <matplotlib.lines.Line2D object at 0x7de16c436900>, <matplotlib.lines.Line2D object at 0x7de16c435fd0>, <matplotlib.lines.Line2D object at 0x7de164352f90>, <matplotlib.lines.Line2D object at 0x7de164350ad0>, <matplotlib.lines.Line2D object at 0x7de1643523c0>, <matplotlib.lines.Line2D object at 0x7de164352900>, <matplotlib.lines.Line2D object at 0x7de164350c20>, <matplotlib.lines.Line2D object at 0x7de1643530e0>, <matplotlib.lines.Line2D object at 0x7de164352e40>, <matplotlib.lines.Line2D object at 0x7de164352120>, <matplotlib.lines.Line2D object at 0x7de164353770>, <matplotlib.lines.Line2D object at 0x7de164351010>, <matplotlib.lines.Line2D object at 0x7de164350d70>, <matplotlib.lines.Line2D object at 0x7de1643527b0>, <matplotlib.lines.Line2D object at 0x7de164352510>, <matplotlib.lines.Line2D object at 0x7de164352cf0>, <matplotlib.lines.Line2D object at 0x7de164351fd0>, <matplotlib.lines.Line2D object at 0x7de1643538c0>, <matplotlib.lines.Line2D object at 0x7de164352270>, <matplotlib.lines.Line2D object at 0x7de1643506e0>, <matplotlib.lines.Line2D object at 0x7de164350590>, <matplotlib.lines.Line2D object at 0x7de1643516a0>, <matplotlib.lines.Line2D object at 0x7de164353cb0>, <matplotlib.lines.Line2D object at 0x7de164351940>, <matplotlib.lines.Line2D object at 0x7de164350050>, <matplotlib.lines.Line2D object at 0x7de164350440>, <matplotlib.lines.Line2D object at 0x7de1643517f0>, <matplotlib.lines.Line2D object at 0x7de164353620>, <matplotlib.lines.Line2D object at 0x7de164351be0>, <matplotlib.lines.Line2D object at 0x7de164351e80>, <matplotlib.lines.Line2D object at 0x7de1643501a0>, <matplotlib.lines.Line2D object at 0x7de164350980>, <matplotlib.lines.Line2D object at 0x7de164352a50>, <matplotlib.lines.Line2D object at 0x7de164352ba0>, <matplotlib.lines.Line2D object at 0x7de164353e00>, <matplotlib.lines.Line2D object at 0x7de164351d30>, <matplotlib.lines.Line2D object at 0x7de16cc08980>, <matplotlib.lines.Line2D object at 0x7de16cc0a3c0>, <matplotlib.lines.Line2D object at 0x7de16cc0b620>, <matplotlib.lines.Line2D object at 0x7de16cc09940>, <matplotlib.lines.Line2D object at 0x7de16cc08ad0>, <matplotlib.lines.Line2D object at 0x7de16cc09be0>, <matplotlib.lines.Line2D object at 0x7de16cc0bcb0>, <matplotlib.lines.Line2D object at 0x7de16cc0b770>, <matplotlib.lines.Line2D object at 0x7de16cc09d30>, <matplotlib.lines.Line2D object at 0x7de16cc0be00>, <matplotlib.lines.Line2D object at 0x7de16cc0a900>, <matplotlib.lines.Line2D object at 0x7de16cc09a90>, <matplotlib.lines.Line2D object at 0x7de16cc0a7b0>, <matplotlib.lines.Line2D object at 0x7de16cc09e80>, <matplotlib.lines.Line2D object at 0x7de16cc09400>, <matplotlib.lines.Line2D object at 0x7de16cc0acf0>, <matplotlib.lines.Line2D object at 0x7de16cc08d70>, <matplotlib.lines.Line2D object at 0x7de16cc096a0>, <matplotlib.lines.Line2D object at 0x7de16cc09fd0>, <matplotlib.lines.Line2D object at 0x7de16cc0b230>, <matplotlib.lines.Line2D object at 0x7de16cc08050>, <matplotlib.lines.Line2D object at 0x7de16d52b230>, <matplotlib.lines.Line2D object at 0x7de16d52b380>, <matplotlib.lines.Line2D object at 0x7de16d52b0e0>, <matplotlib.lines.Line2D object at 0x7de16d5281a0>, <matplotlib.lines.Line2D object at 0x7de16d52acf0>, <matplotlib.lines.Line2D object at 0x7de16d529550>, <matplotlib.lines.Line2D object at 0x7de16d529940>, <matplotlib.lines.Line2D object at 0x7de16d5286e0>, <matplotlib.lines.Line2D object at 0x7de16d52b770>, <matplotlib.lines.Line2D object at 0x7de16d52b620>, <matplotlib.lines.Line2D object at 0x7de16d52b4d0>, <matplotlib.lines.Line2D object at 0x7de16d528ec0>, <matplotlib.lines.Line2D object at 0x7de16d52bb60>, <matplotlib.lines.Line2D object at 0x7de16d52a660>, <matplotlib.lines.Line2D object at 0x7de16d52a7b0>, <matplotlib.lines.Line2D object at 0x7de16d52a900>, <matplotlib.lines.Line2D object at 0x7de16d529010>, <matplotlib.lines.Line2D object at 0x7de16d529160>, <matplotlib.lines.Line2D object at 0x7de16d528d70>, <matplotlib.lines.Line2D object at 0x7de16d52ba10>, <matplotlib.lines.Line2D object at 0x7de16d528440>, <matplotlib.lines.Line2D object at 0x7de16d5282f0>, <matplotlib.lines.Line2D object at 0x7de16d52be00>, <matplotlib.lines.Line2D object at 0x7de16d52aba0>, <matplotlib.lines.Line2D object at 0x7de16d528830>, <matplotlib.lines.Line2D object at 0x7de16d52aa50>, <matplotlib.lines.Line2D object at 0x7de16d529d30>, <matplotlib.lines.Line2D object at 0x7de16d529be0>, <matplotlib.lines.Line2D object at 0x7de16d529a90>, <matplotlib.lines.Line2D object at 0x7de16d52a120>, <matplotlib.lines.Line2D object at 0x7de16d529fd0>, <matplotlib.lines.Line2D object at 0x7de16d529e80>, <matplotlib.lines.Line2D object at 0x7de16d528ad0>, <matplotlib.lines.Line2D object at 0x7de16d52a3c0>, <matplotlib.lines.Line2D object at 0x7de16d52a270>, <matplotlib.lines.Line2D object at 0x7de16d528050>, <matplotlib.lines.Line2D object at 0x7de16d528590>, <matplotlib.lines.Line2D object at 0x7de16d52bcb0>, <matplotlib.lines.Line2D object at 0x7de16d529400>, <matplotlib.lines.Line2D object at 0x7de16d5292b0>, <matplotlib.legend.Legend object at 0x7de17d0e6cf0>]
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 15.812 seconds)