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

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 0x785ed6208c20>, <matplotlib.lines.Line2D object at 0x785ed8d43650>, <matplotlib.lines.Line2D object at 0x785ed05f9550>, <matplotlib.lines.Line2D object at 0x785ed05fbb90>, <matplotlib.lines.Line2D object at 0x785ed05f84a0>, <matplotlib.lines.Line2D object at 0x785ed05fbf20>, <matplotlib.lines.Line2D object at 0x785ed05f9ca0>, <matplotlib.lines.Line2D object at 0x785ed05f82f0>, <matplotlib.lines.Line2D object at 0x785ed05fb440>, <matplotlib.lines.Line2D object at 0x785ed05fa660>, <matplotlib.lines.Line2D object at 0x785ed1d07020>, <matplotlib.lines.Line2D object at 0x785ed1d05370>, <matplotlib.lines.Line2D object at 0x785ed1d06180>, <matplotlib.lines.Line2D object at 0x785ed1d05010>, <matplotlib.lines.Line2D object at 0x785ed1d051c0>, <matplotlib.lines.Line2D object at 0x785ed1d05100>, <matplotlib.lines.Line2D object at 0x785ed1d04200>, <matplotlib.lines.Line2D object at 0x785ed1d04320>, <matplotlib.lines.Line2D object at 0x785ed1d043b0>, <matplotlib.lines.Line2D object at 0x785ed1d06330>, <matplotlib.lines.Line2D object at 0x785ed1d071a0>, <matplotlib.lines.Line2D object at 0x785ed1d07980>, <matplotlib.lines.Line2D object at 0x785ed1d06cf0>, <matplotlib.lines.Line2D object at 0x785ed1d078c0>, <matplotlib.lines.Line2D object at 0x785ed1d07a10>, <matplotlib.lines.Line2D object at 0x785ed1d072c0>, <matplotlib.lines.Line2D object at 0x785ed1d07260>, <matplotlib.lines.Line2D object at 0x785ed1d073b0>, <matplotlib.lines.Line2D object at 0x785ed1d07b30>, <matplotlib.lines.Line2D object at 0x785ed1d042f0>, <matplotlib.lines.Line2D object at 0x785ed1d07f20>, <matplotlib.lines.Line2D object at 0x785ed1d04080>, <matplotlib.lines.Line2D object at 0x785ed1d06e70>, <matplotlib.lines.Line2D object at 0x785ed1d067b0>, <matplotlib.lines.Line2D object at 0x785ed1d04a40>, <matplotlib.lines.Line2D object at 0x785ed06b3590>, <matplotlib.lines.Line2D object at 0x785ed06b3e00>, <matplotlib.lines.Line2D object at 0x785ed06b1ca0>, <matplotlib.lines.Line2D object at 0x785ed06b12b0>, <matplotlib.lines.Line2D object at 0x785ed06b17f0>, <matplotlib.lines.Line2D object at 0x785ed06b1fa0>, <matplotlib.lines.Line2D object at 0x785ed06b2240>, <matplotlib.lines.Line2D object at 0x785ed06b05c0>, <matplotlib.lines.Line2D object at 0x785ed06b3b30>, <matplotlib.lines.Line2D object at 0x785ed06b2db0>, <matplotlib.lines.Line2D object at 0x785ed06b3230>, <matplotlib.lines.Line2D object at 0x785ed06b2450>, <matplotlib.lines.Line2D object at 0x785ed06b18e0>, <matplotlib.lines.Line2D object at 0x785ed06b26f0>, <matplotlib.lines.Line2D object at 0x785ed06b0560>, <matplotlib.lines.Line2D object at 0x785ed06b1d60>, <matplotlib.lines.Line2D object at 0x785ed06b0470>, <matplotlib.lines.Line2D object at 0x785ed06b3a40>, <matplotlib.lines.Line2D object at 0x785ed06b2900>, <matplotlib.lines.Line2D object at 0x785ed06b0380>, <matplotlib.lines.Line2D object at 0x785ed06b2420>, <matplotlib.lines.Line2D object at 0x785ed06b1a60>, <matplotlib.lines.Line2D object at 0x785ed06b38c0>, <matplotlib.lines.Line2D object at 0x785ed06b2840>, <matplotlib.lines.Line2D object at 0x785ed06b28d0>, <matplotlib.lines.Line2D object at 0x785ed1bc6b70>, <matplotlib.lines.Line2D object at 0x785ed1bc6ba0>, <matplotlib.lines.Line2D object at 0x785ed1bc5610>, <matplotlib.lines.Line2D object at 0x785ed1bc6cf0>, <matplotlib.lines.Line2D object at 0x785ed1bc6bd0>, <matplotlib.lines.Line2D object at 0x785ed1bc40b0>, <matplotlib.lines.Line2D object at 0x785ed1bc4260>, <matplotlib.lines.Line2D object at 0x785ed1bc4b30>, <matplotlib.lines.Line2D object at 0x785ed1bc53d0>, <matplotlib.lines.Line2D object at 0x785ed1bc6c30>, <matplotlib.lines.Line2D object at 0x785ed1bc6390>, <matplotlib.lines.Line2D object at 0x785ed1bc6870>, <matplotlib.lines.Line2D object at 0x785ed1bc4500>, <matplotlib.lines.Line2D object at 0x785ed1bc4320>, <matplotlib.lines.Line2D object at 0x785ed1bc65a0>, <matplotlib.lines.Line2D object at 0x785ed1bc6420>, <matplotlib.lines.Line2D object at 0x785ed1bc6ea0>, <matplotlib.lines.Line2D object at 0x785ed1bc5d90>, <matplotlib.lines.Line2D object at 0x785ed1bc77d0>, <matplotlib.lines.Line2D object at 0x785ed1bc7a70>, <matplotlib.lines.Line2D object at 0x785ed1bc7ce0>, <matplotlib.lines.Line2D object at 0x785ed1bc7f80>, <matplotlib.lines.Line2D object at 0x785ed1bc7230>, <matplotlib.lines.Line2D object at 0x785ed1bc5b80>, <matplotlib.lines.Line2D object at 0x785ed1bc6ae0>, <matplotlib.lines.Line2D object at 0x785ed058a0f0>, <matplotlib.lines.Line2D object at 0x785ed058ac90>, <matplotlib.lines.Line2D object at 0x785ed8cf8f80>, <matplotlib.lines.Line2D object at 0x785eda409460>, <matplotlib.lines.Line2D object at 0x785eda4091c0>, <matplotlib.lines.Line2D object at 0x785eda4090a0>, <matplotlib.lines.Line2D object at 0x785eda40b260>, <matplotlib.lines.Line2D object at 0x785eda40af90>, <matplotlib.lines.Line2D object at 0x785eda4096d0>, <matplotlib.lines.Line2D object at 0x785eda40a3c0>, <matplotlib.lines.Line2D object at 0x785eda40b050>, <matplotlib.lines.Line2D object at 0x785eda4096a0>, <matplotlib.lines.Line2D object at 0x785eda408320>, <matplotlib.lines.Line2D object at 0x785eda40bdd0>, <matplotlib.lines.Line2D object at 0x785eda4099d0>, <matplotlib.legend.Legend object at 0x785eda408770>]

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.214 seconds)

Gallery generated by Sphinx-Gallery