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])
density plot example
[<matplotlib.lines.Line2D object at 0x7bfbd48daba0>, <matplotlib.legend.Legend object at 0x7bfbd461af60>]

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])
density plot example
[<matplotlib.lines.Line2D object at 0x7bfbd8ce7f50>, <matplotlib.lines.Line2D object at 0x7bfbebd5cbc0>, <matplotlib.lines.Line2D object at 0x7bfbd978a030>, <matplotlib.lines.Line2D object at 0x7bfbd978ae70>, <matplotlib.lines.Line2D object at 0x7bfbd97890d0>, <matplotlib.lines.Line2D object at 0x7bfbd9788f80>, <matplotlib.lines.Line2D object at 0x7bfbd978a960>, <matplotlib.lines.Line2D object at 0x7bfbd978a6f0>, <matplotlib.lines.Line2D object at 0x7bfbd9788350>, <matplotlib.lines.Line2D object at 0x7bfbd9789190>, <matplotlib.lines.Line2D object at 0x7bfbd97892b0>, <matplotlib.lines.Line2D object at 0x7bfbd978a990>, <matplotlib.lines.Line2D object at 0x7bfbd9789640>, <matplotlib.lines.Line2D object at 0x7bfbd978a810>, <matplotlib.lines.Line2D object at 0x7bfbd9789e20>, <matplotlib.lines.Line2D object at 0x7bfbd97885c0>, <matplotlib.lines.Line2D object at 0x7bfbd978b440>, <matplotlib.lines.Line2D object at 0x7bfbd9789700>, <matplotlib.lines.Line2D object at 0x7bfbd9788470>, <matplotlib.lines.Line2D object at 0x7bfbd978b1a0>, <matplotlib.lines.Line2D object at 0x7bfbd978bda0>, <matplotlib.lines.Line2D object at 0x7bfbd978a900>, <matplotlib.lines.Line2D object at 0x7bfbd978bfe0>, <matplotlib.lines.Line2D object at 0x7bfbec09f4d0>, <matplotlib.lines.Line2D object at 0x7bfbec09e690>, <matplotlib.lines.Line2D object at 0x7bfbec09ef30>, <matplotlib.lines.Line2D object at 0x7bfbec09e210>, <matplotlib.lines.Line2D object at 0x7bfbec09d2e0>, <matplotlib.lines.Line2D object at 0x7bfbec09f0e0>, <matplotlib.lines.Line2D object at 0x7bfbec09fbc0>, <matplotlib.lines.Line2D object at 0x7bfbec09fa10>, <matplotlib.lines.Line2D object at 0x7bfbec09e0f0>, <matplotlib.lines.Line2D object at 0x7bfbec09d250>, <matplotlib.lines.Line2D object at 0x7bfbec09d730>, <matplotlib.lines.Line2D object at 0x7bfbec09d5b0>, <matplotlib.lines.Line2D object at 0x7bfbec09c3b0>, <matplotlib.lines.Line2D object at 0x7bfbec09fd10>, <matplotlib.lines.Line2D object at 0x7bfbec09e450>, <matplotlib.lines.Line2D object at 0x7bfbec09dca0>, <matplotlib.lines.Line2D object at 0x7bfbd46c6f90>, <matplotlib.lines.Line2D object at 0x7bfc0e0d9370>, <matplotlib.lines.Line2D object at 0x7bfc0e0d9970>, <matplotlib.lines.Line2D object at 0x7bfc0e0daed0>, <matplotlib.lines.Line2D object at 0x7bfc0e0d9940>, <matplotlib.lines.Line2D object at 0x7bfc0e0d92e0>, <matplotlib.lines.Line2D object at 0x7bfc0e0dad20>, <matplotlib.lines.Line2D object at 0x7bfc0e0d8b60>, <matplotlib.lines.Line2D object at 0x7bfc0e0d9850>, <matplotlib.lines.Line2D object at 0x7bfc0e0d87d0>, <matplotlib.lines.Line2D object at 0x7bfc0e0dae40>, <matplotlib.lines.Line2D object at 0x7bfc0e0d9010>, <matplotlib.lines.Line2D object at 0x7bfc0e0d98b0>, <matplotlib.lines.Line2D object at 0x7bfc0e0daf30>, <matplotlib.lines.Line2D object at 0x7bfc0e0d83b0>, <matplotlib.lines.Line2D object at 0x7bfc0e0d8950>, <matplotlib.lines.Line2D object at 0x7bfc0e0d9640>, <matplotlib.lines.Line2D object at 0x7bfc0e0db2f0>, <matplotlib.lines.Line2D object at 0x7bfbec076de0>, <matplotlib.lines.Line2D object at 0x7bfbec076ab0>, <matplotlib.lines.Line2D object at 0x7bfbec024ec0>, <matplotlib.lines.Line2D object at 0x7bfbec026540>, <matplotlib.lines.Line2D object at 0x7bfbec026a50>, <matplotlib.lines.Line2D object at 0x7bfbec024920>, <matplotlib.lines.Line2D object at 0x7bfbec026f60>, <matplotlib.lines.Line2D object at 0x7bfbec026b10>, <matplotlib.lines.Line2D object at 0x7bfbec027fe0>, <matplotlib.lines.Line2D object at 0x7bfbec025cd0>, <matplotlib.lines.Line2D object at 0x7bfbd475d5e0>, <matplotlib.lines.Line2D object at 0x7bfbd475f0b0>, <matplotlib.lines.Line2D object at 0x7bfbd475c8c0>, <matplotlib.lines.Line2D object at 0x7bfbd475ea50>, <matplotlib.lines.Line2D object at 0x7bfbd475e480>, <matplotlib.lines.Line2D object at 0x7bfbd475e900>, <matplotlib.lines.Line2D object at 0x7bfbd475d820>, <matplotlib.lines.Line2D object at 0x7bfbd475dd60>, <matplotlib.lines.Line2D object at 0x7bfbd475ffb0>, <matplotlib.lines.Line2D object at 0x7bfbd475fec0>, <matplotlib.lines.Line2D object at 0x7bfbd475cda0>, <matplotlib.lines.Line2D object at 0x7bfbd475da00>, <matplotlib.lines.Line2D object at 0x7bfbd475fc20>, <matplotlib.lines.Line2D object at 0x7bfbd475e030>, <matplotlib.lines.Line2D object at 0x7bfbd475f0e0>, <matplotlib.lines.Line2D object at 0x7bfbd475e8d0>, <matplotlib.lines.Line2D object at 0x7bfbd475caa0>, <matplotlib.lines.Line2D object at 0x7bfbd475f260>, <matplotlib.lines.Line2D object at 0x7bfbd475f740>, <matplotlib.lines.Line2D object at 0x7bfbd475e150>, <matplotlib.lines.Line2D object at 0x7bfbd4826000>, <matplotlib.lines.Line2D object at 0x7bfbd4826fc0>, <matplotlib.lines.Line2D object at 0x7bfbd4826f90>, <matplotlib.lines.Line2D object at 0x7bfbd4826930>, <matplotlib.lines.Line2D object at 0x7bfbd4827530>, <matplotlib.lines.Line2D object at 0x7bfbd4826330>, <matplotlib.lines.Line2D object at 0x7bfbd4827590>, <matplotlib.lines.Line2D object at 0x7bfbd48269f0>, <matplotlib.lines.Line2D object at 0x7bfbd4827080>, <matplotlib.lines.Line2D object at 0x7bfbd4827350>, <matplotlib.lines.Line2D object at 0x7bfbd4825df0>, <matplotlib.lines.Line2D object at 0x7bfbd4827e90>, <matplotlib.lines.Line2D object at 0x7bfbd48265a0>, <matplotlib.legend.Legend object at 0x7bfbd8813140>]

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.ax.clear()
    plotter.plot_density(truths, index=i)
    return plotter.ax

Finally, plot the densities over time.

Total running time of the script: (0 minutes 10.374 seconds)

Gallery generated by Sphinx-Gallery