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 0x7b9e5d267380>, <matplotlib.legend.Legend object at 0x7b9e5d267770>]

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 0x7b9e4d1e2270>, <matplotlib.lines.Line2D object at 0x7b9e4d1e16a0>, <matplotlib.lines.Line2D object at 0x7b9e4d1e2a50>, <matplotlib.lines.Line2D object at 0x7b9e4d1e12b0>, <matplotlib.lines.Line2D object at 0x7b9e4d1e1a90>, <matplotlib.lines.Line2D object at 0x7b9e4d1e1940>, <matplotlib.lines.Line2D object at 0x7b9e4d1e1400>, <matplotlib.lines.Line2D object at 0x7b9e4d1e0ad0>, <matplotlib.lines.Line2D object at 0x7b9e4d1e2f90>, <matplotlib.lines.Line2D object at 0x7b9e4d1e1be0>, <matplotlib.lines.Line2D object at 0x7b9e4d1e3b60>, <matplotlib.lines.Line2D object at 0x7b9e4d1e1e80>, <matplotlib.lines.Line2D object at 0x7b9e4ebcdfd0>, <matplotlib.lines.Line2D object at 0x7b9e4ebcc590>, <matplotlib.lines.Line2D object at 0x7b9e4ebcc2f0>, <matplotlib.lines.Line2D object at 0x7b9e4ebce900>, <matplotlib.lines.Line2D object at 0x7b9e4ebce660>, <matplotlib.lines.Line2D object at 0x7b9e4ebcecf0>, <matplotlib.lines.Line2D object at 0x7b9e4ebceba0>, <matplotlib.lines.Line2D object at 0x7b9e4ebce120>, <matplotlib.lines.Line2D object at 0x7b9e4ebce510>, <matplotlib.lines.Line2D object at 0x7b9e4ebcdbe0>, <matplotlib.lines.Line2D object at 0x7b9e4ebcea50>, <matplotlib.lines.Line2D object at 0x7b9e4ebcd2b0>, <matplotlib.lines.Line2D object at 0x7b9e4ebcdd30>, <matplotlib.lines.Line2D object at 0x7b9e4ebcd940>, <matplotlib.lines.Line2D object at 0x7b9e4ebce7b0>, <matplotlib.lines.Line2D object at 0x7b9e4ebcd7f0>, <matplotlib.lines.Line2D object at 0x7b9e4ebcde80>, <matplotlib.lines.Line2D object at 0x7b9e4ebcd6a0>, <matplotlib.lines.Line2D object at 0x7b9e4ebcc830>, <matplotlib.lines.Line2D object at 0x7b9e4ebcda90>, <matplotlib.lines.Line2D object at 0x7b9e4ebce270>, <matplotlib.lines.Line2D object at 0x7b9e4ebce3c0>, <matplotlib.lines.Line2D object at 0x7b9e81392ba0>, <matplotlib.lines.Line2D object at 0x7b9e81392660>, <matplotlib.lines.Line2D object at 0x7b9e81392a50>, <matplotlib.lines.Line2D object at 0x7b9e4ca196a0>, <matplotlib.lines.Line2D object at 0x7b9e4ca197f0>, <matplotlib.lines.Line2D object at 0x7b9e4ca19010>, <matplotlib.lines.Line2D object at 0x7b9e4ca18980>, <matplotlib.lines.Line2D object at 0x7b9e4ca19e80>, <matplotlib.lines.Line2D object at 0x7b9e4ca19940>, <matplotlib.lines.Line2D object at 0x7b9e4ca1b0e0>, <matplotlib.lines.Line2D object at 0x7b9e4ca1a660>, <matplotlib.lines.Line2D object at 0x7b9e4ca181a0>, <matplotlib.lines.Line2D object at 0x7b9e4ca19550>, <matplotlib.lines.Line2D object at 0x7b9e4ca18440>, <matplotlib.lines.Line2D object at 0x7b9e4ca1a7b0>, <matplotlib.lines.Line2D object at 0x7b9e4ca1a510>, <matplotlib.lines.Line2D object at 0x7b9e4ca1b620>, <matplotlib.lines.Line2D object at 0x7b9e4ca1a3c0>, <matplotlib.lines.Line2D object at 0x7b9e4ca1be00>, <matplotlib.lines.Line2D object at 0x7b9e4ca1bb60>, <matplotlib.lines.Line2D object at 0x7b9e4ca1b380>, <matplotlib.lines.Line2D object at 0x7b9e4ca1b4d0>, <matplotlib.lines.Line2D object at 0x7b9e4ca1a900>, <matplotlib.lines.Line2D object at 0x7b9e4ca19160>, <matplotlib.lines.Line2D object at 0x7b9e4ca1aa50>, <matplotlib.lines.Line2D object at 0x7b9e4ca18590>, <matplotlib.lines.Line2D object at 0x7b9e4ca18c20>, <matplotlib.lines.Line2D object at 0x7b9e4ca1af90>, <matplotlib.lines.Line2D object at 0x7b9e4ca186e0>, <matplotlib.lines.Line2D object at 0x7b9e4ca1a270>, <matplotlib.lines.Line2D object at 0x7b9e4ca192b0>, <matplotlib.lines.Line2D object at 0x7b9e4ca1aba0>, <matplotlib.lines.Line2D object at 0x7b9e4ca1acf0>, <matplotlib.lines.Line2D object at 0x7b9e4ca1ae40>, <matplotlib.lines.Line2D object at 0x7b9e4ca18ec0>, <matplotlib.lines.Line2D object at 0x7b9e4ca19d30>, <matplotlib.lines.Line2D object at 0x7b9e4ca19fd0>, <matplotlib.lines.Line2D object at 0x7b9e4ca1a120>, <matplotlib.lines.Line2D object at 0x7b9e4ca1ba10>, <matplotlib.lines.Line2D object at 0x7b9e4ca1b230>, <matplotlib.lines.Line2D object at 0x7b9e4ca182f0>, <matplotlib.lines.Line2D object at 0x7b9e4ca18050>, <matplotlib.lines.Line2D object at 0x7b9e4ca1b770>, <matplotlib.lines.Line2D object at 0x7b9e4ca18ad0>, <matplotlib.lines.Line2D object at 0x7b9e4ca18d70>, <matplotlib.lines.Line2D object at 0x7b9e4ca18830>, <matplotlib.lines.Line2D object at 0x7b9e4ca19400>, <matplotlib.lines.Line2D object at 0x7b9e4ca1bcb0>, <matplotlib.lines.Line2D object at 0x7b9e4483c050>, <matplotlib.lines.Line2D object at 0x7b9e4483c1a0>, <matplotlib.lines.Line2D object at 0x7b9e4483c2f0>, <matplotlib.lines.Line2D object at 0x7b9e4483c440>, <matplotlib.lines.Line2D object at 0x7b9e4483c590>, <matplotlib.lines.Line2D object at 0x7b9e4483c6e0>, <matplotlib.lines.Line2D object at 0x7b9e4483c830>, <matplotlib.lines.Line2D object at 0x7b9e4483c980>, <matplotlib.lines.Line2D object at 0x7b9e4483cad0>, <matplotlib.lines.Line2D object at 0x7b9e4483cc20>, <matplotlib.lines.Line2D object at 0x7b9e4483cd70>, <matplotlib.lines.Line2D object at 0x7b9e4483cec0>, <matplotlib.lines.Line2D object at 0x7b9e4483d010>, <matplotlib.lines.Line2D object at 0x7b9e4483d160>, <matplotlib.lines.Line2D object at 0x7b9e4483d2b0>, <matplotlib.lines.Line2D object at 0x7b9e4483d400>, <matplotlib.lines.Line2D object at 0x7b9e4483d550>, <matplotlib.lines.Line2D object at 0x7b9e4483d6a0>, <matplotlib.legend.Legend object at 0x7b9e4d1e0440>]

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 15.963 seconds)

Gallery generated by Sphinx-Gallery