Note
Go to the end to download the full example code. or to run this example in your browser via Binder
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])
[<matplotlib.lines.Line2D object at 0x7f1f59102ed0>, <matplotlib.legend.Legend object at 0x7f1f59103380>]
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])
[<matplotlib.lines.Line2D object at 0x7f1f57ec00e0>, <matplotlib.lines.Line2D object at 0x7f1f5cf35bb0>, <matplotlib.lines.Line2D object at 0x7f1f5cf34380>, <matplotlib.lines.Line2D object at 0x7f1f5cf35520>, <matplotlib.lines.Line2D object at 0x7f1f5cf36000>, <matplotlib.lines.Line2D object at 0x7f1f5e594d40>, <matplotlib.lines.Line2D object at 0x7f1f5e594da0>, <matplotlib.lines.Line2D object at 0x7f1f5e594110>, <matplotlib.lines.Line2D object at 0x7f1f5e5952b0>, <matplotlib.lines.Line2D object at 0x7f1f5e5979b0>, <matplotlib.lines.Line2D object at 0x7f1f5e595e50>, <matplotlib.lines.Line2D object at 0x7f1f5e595d90>, <matplotlib.lines.Line2D object at 0x7f1f5e5942f0>, <matplotlib.lines.Line2D object at 0x7f1f5e595af0>, <matplotlib.lines.Line2D object at 0x7f1f5e596ed0>, <matplotlib.lines.Line2D object at 0x7f1f5e594cb0>, <matplotlib.lines.Line2D object at 0x7f1f5e5975f0>, <matplotlib.lines.Line2D object at 0x7f1f5e5969c0>, <matplotlib.lines.Line2D object at 0x7f1f5e596b10>, <matplotlib.lines.Line2D object at 0x7f1f5e595c40>, <matplotlib.lines.Line2D object at 0x7f1f5e5974a0>, <matplotlib.lines.Line2D object at 0x7f1f5e597d40>, <matplotlib.lines.Line2D object at 0x7f1f5e597230>, <matplotlib.lines.Line2D object at 0x7f1f5d7fd400>, <matplotlib.lines.Line2D object at 0x7f1f5d7feb70>, <matplotlib.lines.Line2D object at 0x7f1f5d7ffc20>, <matplotlib.lines.Line2D object at 0x7f1f5d7fe1b0>, <matplotlib.lines.Line2D object at 0x7f1f5d7fe030>, <matplotlib.lines.Line2D object at 0x7f1f5d7fc830>, <matplotlib.lines.Line2D object at 0x7f1f5d7fd3a0>, <matplotlib.lines.Line2D object at 0x7f1f5d7fe0c0>, <matplotlib.lines.Line2D object at 0x7f1f5d7fdb80>, <matplotlib.lines.Line2D object at 0x7f1f5d7ff950>, <matplotlib.lines.Line2D object at 0x7f1f5d7fd550>, <matplotlib.lines.Line2D object at 0x7f1f5d7fc8f0>, <matplotlib.lines.Line2D object at 0x7f1f5d7fcb00>, <matplotlib.lines.Line2D object at 0x7f1f5d7ff710>, <matplotlib.lines.Line2D object at 0x7f1f5d7fff80>, <matplotlib.lines.Line2D object at 0x7f1f5d7ff4a0>, <matplotlib.lines.Line2D object at 0x7f1f5d7fe750>, <matplotlib.lines.Line2D object at 0x7f1f5d7fd190>, <matplotlib.lines.Line2D object at 0x7f1f5d7fdd90>, <matplotlib.lines.Line2D object at 0x7f1f5d7fd7f0>, <matplotlib.lines.Line2D object at 0x7f1f5d7fef30>, <matplotlib.lines.Line2D object at 0x7f1f5d7fc3b0>, <matplotlib.lines.Line2D object at 0x7f1f5d7fee40>, <matplotlib.lines.Line2D object at 0x7f1f57fe0ef0>, <matplotlib.lines.Line2D object at 0x7f1f57fe31a0>, <matplotlib.lines.Line2D object at 0x7f1f57fe3200>, <matplotlib.lines.Line2D object at 0x7f1f57fe3740>, <matplotlib.lines.Line2D object at 0x7f1f57fe1bb0>, <matplotlib.lines.Line2D object at 0x7f1f57fe1130>, <matplotlib.lines.Line2D object at 0x7f1f57fe01a0>, <matplotlib.lines.Line2D object at 0x7f1f57fe0260>, <matplotlib.lines.Line2D object at 0x7f1f57fe1670>, <matplotlib.lines.Line2D object at 0x7f1f57fe2a50>, <matplotlib.lines.Line2D object at 0x7f1f57fe0e30>, <matplotlib.lines.Line2D object at 0x7f1f57fe0110>, <matplotlib.lines.Line2D object at 0x7f1f57fe0500>, <matplotlib.lines.Line2D object at 0x7f1f57fe19d0>, <matplotlib.lines.Line2D object at 0x7f1f57fe3500>, <matplotlib.lines.Line2D object at 0x7f1f57fe02c0>, <matplotlib.lines.Line2D object at 0x7f1f57fe3b00>, <matplotlib.lines.Line2D object at 0x7f1f57fe3e00>, <matplotlib.lines.Line2D object at 0x7f1f57fe3470>, <matplotlib.lines.Line2D object at 0x7f1f57fe0b30>, <matplotlib.lines.Line2D object at 0x7f1f57fe3da0>, <matplotlib.lines.Line2D object at 0x7f1f57efe960>, <matplotlib.lines.Line2D object at 0x7f1f57efd3a0>, <matplotlib.lines.Line2D object at 0x7f1f57eff860>, <matplotlib.lines.Line2D object at 0x7f1f57efcfe0>, <matplotlib.lines.Line2D object at 0x7f1f57efce30>, <matplotlib.lines.Line2D object at 0x7f1f57efe780>, <matplotlib.lines.Line2D object at 0x7f1f57efff50>, <matplotlib.lines.Line2D object at 0x7f1f57eff200>, <matplotlib.lines.Line2D object at 0x7f1f57efeea0>, <matplotlib.lines.Line2D object at 0x7f1f57efcfb0>, <matplotlib.lines.Line2D object at 0x7f1f57efd040>, <matplotlib.lines.Line2D object at 0x7f1f57efd640>, <matplotlib.lines.Line2D object at 0x7f1f591968a0>, <matplotlib.lines.Line2D object at 0x7f1f59194680>, <matplotlib.lines.Line2D object at 0x7f1f591958b0>, <matplotlib.lines.Line2D object at 0x7f1f59195550>, <matplotlib.lines.Line2D object at 0x7f1f591978c0>, <matplotlib.lines.Line2D object at 0x7f1f59197770>, <matplotlib.lines.Line2D object at 0x7f1f59197620>, <matplotlib.lines.Line2D object at 0x7f1f59194320>, <matplotlib.lines.Line2D object at 0x7f1f591943b0>, <matplotlib.lines.Line2D object at 0x7f1f591977d0>, <matplotlib.lines.Line2D object at 0x7f1f59197c80>, <matplotlib.lines.Line2D object at 0x7f1f59195310>, <matplotlib.lines.Line2D object at 0x7f1f591962d0>, <matplotlib.lines.Line2D object at 0x7f1f59196f00>, <matplotlib.lines.Line2D object at 0x7f1f59196b10>, <matplotlib.lines.Line2D object at 0x7f1f59197710>, <matplotlib.lines.Line2D object at 0x7f1f59195e80>, <matplotlib.lines.Line2D object at 0x7f1f591946b0>, <matplotlib.lines.Line2D object at 0x7f1f59194b60>, <matplotlib.lines.Line2D object at 0x7f1f59194860>, <matplotlib.lines.Line2D object at 0x7f1f59194110>, <matplotlib.legend.Legend object at 0x7f1f59194e30>]
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.
plotter = Plotter()
plotter.plot_density(truths, index=None)
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.
plotter = Plotter()
plotter.plot_density(truths, index=-1)
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.
plotter = Plotter()
animation.FuncAnimation(plotter.fig, update, frames=range(1, n_time_steps))