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

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 0x7fd209ed5670>, <matplotlib.lines.Line2D object at 0x7fd2065f9520>, <matplotlib.lines.Line2D object at 0x7fd2065fb680>, <matplotlib.lines.Line2D object at 0x7fd2065f9490>, <matplotlib.lines.Line2D object at 0x7fd2065fba10>, <matplotlib.lines.Line2D object at 0x7fd2065f8440>, <matplotlib.lines.Line2D object at 0x7fd2065fa990>, <matplotlib.lines.Line2D object at 0x7fd2065f8050>, <matplotlib.lines.Line2D object at 0x7fd205796660>, <matplotlib.lines.Line2D object at 0x7fd2065f83b0>, <matplotlib.lines.Line2D object at 0x7fd2065f8830>, <matplotlib.lines.Line2D object at 0x7fd20572f650>, <matplotlib.lines.Line2D object at 0x7fd20572db80>, <matplotlib.lines.Line2D object at 0x7fd20572dd00>, <matplotlib.lines.Line2D object at 0x7fd20572e690>, <matplotlib.lines.Line2D object at 0x7fd20572e0c0>, <matplotlib.lines.Line2D object at 0x7fd20572d5b0>, <matplotlib.lines.Line2D object at 0x7fd20572d280>, <matplotlib.lines.Line2D object at 0x7fd20572d370>, <matplotlib.lines.Line2D object at 0x7fd20572d010>, <matplotlib.lines.Line2D object at 0x7fd20572cb30>, <matplotlib.lines.Line2D object at 0x7fd20572c080>, <matplotlib.lines.Line2D object at 0x7fd20572e480>, <matplotlib.lines.Line2D object at 0x7fd20572e3c0>, <matplotlib.lines.Line2D object at 0x7fd20572e6c0>, <matplotlib.lines.Line2D object at 0x7fd20572d550>, <matplotlib.lines.Line2D object at 0x7fd20572db20>, <matplotlib.lines.Line2D object at 0x7fd20572f170>, <matplotlib.lines.Line2D object at 0x7fd20572f4d0>, <matplotlib.lines.Line2D object at 0x7fd20572ed50>, <matplotlib.lines.Line2D object at 0x7fd20572c980>, <matplotlib.lines.Line2D object at 0x7fd20572f050>, <matplotlib.lines.Line2D object at 0x7fd20572fe60>, <matplotlib.lines.Line2D object at 0x7fd20572d160>, <matplotlib.lines.Line2D object at 0x7fd205052e10>, <matplotlib.lines.Line2D object at 0x7fd2045a1c10>, <matplotlib.lines.Line2D object at 0x7fd2045a1010>, <matplotlib.lines.Line2D object at 0x7fd2045a25d0>, <matplotlib.lines.Line2D object at 0x7fd2045a0800>, <matplotlib.lines.Line2D object at 0x7fd20589d6a0>, <matplotlib.lines.Line2D object at 0x7fd20589f050>, <matplotlib.lines.Line2D object at 0x7fd20589ff80>, <matplotlib.lines.Line2D object at 0x7fd20589e9c0>, <matplotlib.lines.Line2D object at 0x7fd20589c2c0>, <matplotlib.lines.Line2D object at 0x7fd20589f500>, <matplotlib.lines.Line2D object at 0x7fd20589eea0>, <matplotlib.lines.Line2D object at 0x7fd20589f0b0>, <matplotlib.lines.Line2D object at 0x7fd20589e4e0>, <matplotlib.lines.Line2D object at 0x7fd20589de50>, <matplotlib.lines.Line2D object at 0x7fd20589d1c0>, <matplotlib.lines.Line2D object at 0x7fd20589d040>, <matplotlib.lines.Line2D object at 0x7fd20589d1f0>, <matplotlib.lines.Line2D object at 0x7fd20589f380>, <matplotlib.lines.Line2D object at 0x7fd20589d850>, <matplotlib.lines.Line2D object at 0x7fd20589cbc0>, <matplotlib.lines.Line2D object at 0x7fd20589d580>, <matplotlib.lines.Line2D object at 0x7fd20589d070>, <matplotlib.lines.Line2D object at 0x7fd20589d2b0>, <matplotlib.lines.Line2D object at 0x7fd20589c260>, <matplotlib.lines.Line2D object at 0x7fd20589ea50>, <matplotlib.lines.Line2D object at 0x7fd20589f800>, <matplotlib.lines.Line2D object at 0x7fd205aa2b70>, <matplotlib.lines.Line2D object at 0x7fd205aa2db0>, <matplotlib.lines.Line2D object at 0x7fd205aa3350>, <matplotlib.lines.Line2D object at 0x7fd205aa1b80>, <matplotlib.lines.Line2D object at 0x7fd205aa1b50>, <matplotlib.lines.Line2D object at 0x7fd205aa1ca0>, <matplotlib.lines.Line2D object at 0x7fd205aa1dc0>, <matplotlib.lines.Line2D object at 0x7fd205aa3110>, <matplotlib.lines.Line2D object at 0x7fd205aa0620>, <matplotlib.lines.Line2D object at 0x7fd205aa0140>, <matplotlib.lines.Line2D object at 0x7fd205aa1370>, <matplotlib.lines.Line2D object at 0x7fd205aa1760>, <matplotlib.lines.Line2D object at 0x7fd205aa23f0>, <matplotlib.lines.Line2D object at 0x7fd205aa0440>, <matplotlib.lines.Line2D object at 0x7fd205aa3ef0>, <matplotlib.lines.Line2D object at 0x7fd205aa08c0>, <matplotlib.lines.Line2D object at 0x7fd205aa15e0>, <matplotlib.lines.Line2D object at 0x7fd205aa2870>, <matplotlib.lines.Line2D object at 0x7fd205aa12e0>, <matplotlib.lines.Line2D object at 0x7fd20a9e5100>, <matplotlib.lines.Line2D object at 0x7fd20a9e6e40>, <matplotlib.lines.Line2D object at 0x7fd20a9e4ef0>, <matplotlib.lines.Line2D object at 0x7fd20a9e4890>, <matplotlib.lines.Line2D object at 0x7fd20a9e45c0>, <matplotlib.lines.Line2D object at 0x7fd20a9e7da0>, <matplotlib.lines.Line2D object at 0x7fd20a9e6810>, <matplotlib.lines.Line2D object at 0x7fd20a9e7ef0>, <matplotlib.lines.Line2D object at 0x7fd20a9e5c10>, <matplotlib.lines.Line2D object at 0x7fd20a9e7620>, <matplotlib.lines.Line2D object at 0x7fd20a9e44a0>, <matplotlib.lines.Line2D object at 0x7fd20a9e5ac0>, <matplotlib.lines.Line2D object at 0x7fd20a9e58b0>, <matplotlib.lines.Line2D object at 0x7fd2064064b0>, <matplotlib.lines.Line2D object at 0x7fd2064047a0>, <matplotlib.lines.Line2D object at 0x7fd206405f10>, <matplotlib.lines.Line2D object at 0x7fd2064043b0>, <matplotlib.lines.Line2D object at 0x7fd206404ad0>, <matplotlib.lines.Line2D object at 0x7fd206405970>, <matplotlib.lines.Line2D object at 0x7fd206404710>, <matplotlib.legend.Legend object at 0x7fd205a9b560>]

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.