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

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 0x7fbbca6cff50>, <matplotlib.lines.Line2D object at 0x7fbbc6852120>, <matplotlib.lines.Line2D object at 0x7fbbc6851ca0>, <matplotlib.lines.Line2D object at 0x7fbbc68510a0>, <matplotlib.lines.Line2D object at 0x7fbbc6850e60>, <matplotlib.lines.Line2D object at 0x7fbbc6850530>, <matplotlib.lines.Line2D object at 0x7fbbc6852060>, <matplotlib.lines.Line2D object at 0x7fbbc6a96a20>, <matplotlib.lines.Line2D object at 0x7fbbc54bc440>, <matplotlib.lines.Line2D object at 0x7fbbc54bffe0>, <matplotlib.lines.Line2D object at 0x7fbbc54beab0>, <matplotlib.lines.Line2D object at 0x7fbbc54beb70>, <matplotlib.lines.Line2D object at 0x7fbbc54bd4c0>, <matplotlib.lines.Line2D object at 0x7fbbc54bedb0>, <matplotlib.lines.Line2D object at 0x7fbbc54bf380>, <matplotlib.lines.Line2D object at 0x7fbbc54bf4a0>, <matplotlib.lines.Line2D object at 0x7fbbc54bdb80>, <matplotlib.lines.Line2D object at 0x7fbbc54bc800>, <matplotlib.lines.Line2D object at 0x7fbbc54be690>, <matplotlib.lines.Line2D object at 0x7fbbc54bd430>, <matplotlib.lines.Line2D object at 0x7fbbc54bc200>, <matplotlib.lines.Line2D object at 0x7fbbc54bf410>, <matplotlib.lines.Line2D object at 0x7fbbc54bcb00>, <matplotlib.lines.Line2D object at 0x7fbbc54bf1a0>, <matplotlib.lines.Line2D object at 0x7fbbc54bd5b0>, <matplotlib.lines.Line2D object at 0x7fbbc54bd5e0>, <matplotlib.lines.Line2D object at 0x7fbbc54be660>, <matplotlib.lines.Line2D object at 0x7fbbc54bd280>, <matplotlib.lines.Line2D object at 0x7fbbc54be2d0>, <matplotlib.lines.Line2D object at 0x7fbbc54bf020>, <matplotlib.lines.Line2D object at 0x7fbbc65c7920>, <matplotlib.lines.Line2D object at 0x7fbbc65c7ad0>, <matplotlib.lines.Line2D object at 0x7fbbc65c7cb0>, <matplotlib.lines.Line2D object at 0x7fbbc6860f50>, <matplotlib.lines.Line2D object at 0x7fbbc6862f30>, <matplotlib.lines.Line2D object at 0x7fbbc6861f10>, <matplotlib.lines.Line2D object at 0x7fbbc6863770>, <matplotlib.lines.Line2D object at 0x7fbbc6861e80>, <matplotlib.lines.Line2D object at 0x7fbbc6863fe0>, <matplotlib.lines.Line2D object at 0x7fbbc68600e0>, <matplotlib.lines.Line2D object at 0x7fbbc6860b30>, <matplotlib.lines.Line2D object at 0x7fbbc68615b0>, <matplotlib.lines.Line2D object at 0x7fbbc6860e90>, <matplotlib.lines.Line2D object at 0x7fbbc68617f0>, <matplotlib.lines.Line2D object at 0x7fbbc6860080>, <matplotlib.lines.Line2D object at 0x7fbbc6863140>, <matplotlib.lines.Line2D object at 0x7fbbcc0245c0>, <matplotlib.lines.Line2D object at 0x7fbbcc027ef0>, <matplotlib.lines.Line2D object at 0x7fbbcc0253d0>, <matplotlib.lines.Line2D object at 0x7fbbcc024f80>, <matplotlib.lines.Line2D object at 0x7fbbcc025700>, <matplotlib.lines.Line2D object at 0x7fbbc68c5bb0>, <matplotlib.lines.Line2D object at 0x7fbbc68c78f0>, <matplotlib.lines.Line2D object at 0x7fbbc68c7e60>, <matplotlib.lines.Line2D object at 0x7fbbc68c5cd0>, <matplotlib.lines.Line2D object at 0x7fbbc68c4c50>, <matplotlib.lines.Line2D object at 0x7fbbc68c56a0>, <matplotlib.lines.Line2D object at 0x7fbbc68c69c0>, <matplotlib.lines.Line2D object at 0x7fbbc68c7200>, <matplotlib.lines.Line2D object at 0x7fbbc68c6450>, <matplotlib.lines.Line2D object at 0x7fbbc68c7830>, <matplotlib.lines.Line2D object at 0x7fbbc68c6db0>, <matplotlib.lines.Line2D object at 0x7fbbc68c6630>, <matplotlib.lines.Line2D object at 0x7fbbc68c63f0>, <matplotlib.lines.Line2D object at 0x7fbbc68c5be0>, <matplotlib.lines.Line2D object at 0x7fbbc68c4410>, <matplotlib.lines.Line2D object at 0x7fbbc68c4b30>, <matplotlib.lines.Line2D object at 0x7fbbc68c6b40>, <matplotlib.lines.Line2D object at 0x7fbbc68c45f0>, <matplotlib.lines.Line2D object at 0x7fbbc68c50d0>, <matplotlib.lines.Line2D object at 0x7fbbc68c6b70>, <matplotlib.lines.Line2D object at 0x7fbbc68c6900>, <matplotlib.lines.Line2D object at 0x7fbbc68c40b0>, <matplotlib.lines.Line2D object at 0x7fbbc68c4440>, <matplotlib.lines.Line2D object at 0x7fbbc6ab3bc0>, <matplotlib.lines.Line2D object at 0x7fbbc6ab04d0>, <matplotlib.lines.Line2D object at 0x7fbbc6ab2ae0>, <matplotlib.lines.Line2D object at 0x7fbbc6ab0aa0>, <matplotlib.lines.Line2D object at 0x7fbbc6ab3680>, <matplotlib.lines.Line2D object at 0x7fbbc6ab1f40>, <matplotlib.lines.Line2D object at 0x7fbbc6ab2630>, <matplotlib.lines.Line2D object at 0x7fbbc6ab2a80>, <matplotlib.lines.Line2D object at 0x7fbbc6ab2f30>, <matplotlib.lines.Line2D object at 0x7fbbc6ab0f80>, <matplotlib.lines.Line2D object at 0x7fbbc6ab3080>, <matplotlib.lines.Line2D object at 0x7fbbc6ab3320>, <matplotlib.lines.Line2D object at 0x7fbbc6ab0c80>, <matplotlib.lines.Line2D object at 0x7fbbc6ab1220>, <matplotlib.lines.Line2D object at 0x7fbbc6ab30b0>, <matplotlib.lines.Line2D object at 0x7fbbc6ab3a10>, <matplotlib.lines.Line2D object at 0x7fbbc6ab2090>, <matplotlib.lines.Line2D object at 0x7fbbc6ab28d0>, <matplotlib.lines.Line2D object at 0x7fbbc6ab0560>, <matplotlib.lines.Line2D object at 0x7fbbc6ab1b20>, <matplotlib.lines.Line2D object at 0x7fbbc6ab3fe0>, <matplotlib.lines.Line2D object at 0x7fbbc6ab2870>, <matplotlib.lines.Line2D object at 0x7fbbc6ab17f0>, <matplotlib.lines.Line2D object at 0x7fbbc6ab2840>, <matplotlib.lines.Line2D object at 0x7fbbc55c3590>, <matplotlib.lines.Line2D object at 0x7fbbc55c0260>, <matplotlib.legend.Legend object at 0x7fbbc6ab2120>]

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.