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 0x7fea5f559910>, <matplotlib.legend.Legend object at 0x7fea5f55b080>]
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 0x7fea5a3358e0>, <matplotlib.lines.Line2D object at 0x7fea5a482f00>, <matplotlib.lines.Line2D object at 0x7fea5a480fe0>, <matplotlib.lines.Line2D object at 0x7fea5a482300>, <matplotlib.lines.Line2D object at 0x7fea5a480da0>, <matplotlib.lines.Line2D object at 0x7fea5a4815b0>, <matplotlib.lines.Line2D object at 0x7fea5a481910>, <matplotlib.lines.Line2D object at 0x7fea5a4813a0>, <matplotlib.lines.Line2D object at 0x7fea5a4810d0>, <matplotlib.lines.Line2D object at 0x7fea5a481100>, <matplotlib.lines.Line2D object at 0x7fea5a482240>, <matplotlib.lines.Line2D object at 0x7fea5a483110>, <matplotlib.lines.Line2D object at 0x7fea5a482ed0>, <matplotlib.lines.Line2D object at 0x7fea5a481f10>, <matplotlib.lines.Line2D object at 0x7fea5c31b800>, <matplotlib.lines.Line2D object at 0x7fea5c3192e0>, <matplotlib.lines.Line2D object at 0x7fea5c319100>, <matplotlib.lines.Line2D object at 0x7fea5c319820>, <matplotlib.lines.Line2D object at 0x7fea5c31b1d0>, <matplotlib.lines.Line2D object at 0x7fea5c31ba40>, <matplotlib.lines.Line2D object at 0x7fea5c31b4d0>, <matplotlib.lines.Line2D object at 0x7fea5b8ad8b0>, <matplotlib.lines.Line2D object at 0x7fea5b8afb00>, <matplotlib.lines.Line2D object at 0x7fea5b8ac170>, <matplotlib.lines.Line2D object at 0x7fea5b8add60>, <matplotlib.lines.Line2D object at 0x7fea5b8ad010>, <matplotlib.lines.Line2D object at 0x7fea5b8adb50>, <matplotlib.lines.Line2D object at 0x7fea5b8ade80>, <matplotlib.lines.Line2D object at 0x7fea5b8af200>, <matplotlib.lines.Line2D object at 0x7fea5b8acb30>, <matplotlib.lines.Line2D object at 0x7fea5b8afa70>, <matplotlib.lines.Line2D object at 0x7fea5b8ae5d0>, <matplotlib.lines.Line2D object at 0x7fea5b8af710>, <matplotlib.lines.Line2D object at 0x7fea5b8ac110>, <matplotlib.lines.Line2D object at 0x7fea5b8ad9a0>, <matplotlib.lines.Line2D object at 0x7fea5b8af410>, <matplotlib.lines.Line2D object at 0x7fea5b8ae390>, <matplotlib.lines.Line2D object at 0x7fea5b8ae900>, <matplotlib.lines.Line2D object at 0x7fea5b8afb30>, <matplotlib.lines.Line2D object at 0x7fea5b8ac1a0>, <matplotlib.lines.Line2D object at 0x7fea5b8ad8e0>, <matplotlib.lines.Line2D object at 0x7fea5b8ad730>, <matplotlib.lines.Line2D object at 0x7fea5b8adc70>, <matplotlib.lines.Line2D object at 0x7fea5b8af1d0>, <matplotlib.lines.Line2D object at 0x7fea5b8acb90>, <matplotlib.lines.Line2D object at 0x7fea5b8adfd0>, <matplotlib.lines.Line2D object at 0x7fea5fcfa7b0>, <matplotlib.lines.Line2D object at 0x7fea5fcfa1b0>, <matplotlib.lines.Line2D object at 0x7fea5fcfb350>, <matplotlib.lines.Line2D object at 0x7fea5fcf9f40>, <matplotlib.lines.Line2D object at 0x7fea5fcfb470>, <matplotlib.lines.Line2D object at 0x7fea5fcf8890>, <matplotlib.lines.Line2D object at 0x7fea5fcf8f50>, <matplotlib.lines.Line2D object at 0x7fea5fcf82c0>, <matplotlib.lines.Line2D object at 0x7fea5fcfb0b0>, <matplotlib.lines.Line2D object at 0x7fea5fcfa180>, <matplotlib.lines.Line2D object at 0x7fea5fcf8ef0>, <matplotlib.lines.Line2D object at 0x7fea5fcfab70>, <matplotlib.lines.Line2D object at 0x7fea5fcfbf50>, <matplotlib.lines.Line2D object at 0x7fea5fcfa540>, <matplotlib.lines.Line2D object at 0x7fea5fcf9250>, <matplotlib.lines.Line2D object at 0x7fea5fcfbd70>, <matplotlib.lines.Line2D object at 0x7fea5c3d40e0>, <matplotlib.lines.Line2D object at 0x7fea5fca2ff0>, <matplotlib.lines.Line2D object at 0x7fea5fca3cb0>, <matplotlib.lines.Line2D object at 0x7fea5fca0a70>, <matplotlib.lines.Line2D object at 0x7fea5fca2450>, <matplotlib.lines.Line2D object at 0x7fea5fca1f10>, <matplotlib.lines.Line2D object at 0x7fea5fca2f00>, <matplotlib.lines.Line2D object at 0x7fea5fca2390>, <matplotlib.lines.Line2D object at 0x7fea5fca24b0>, <matplotlib.lines.Line2D object at 0x7fea5fca1cd0>, <matplotlib.lines.Line2D object at 0x7fea5fca1f40>, <matplotlib.lines.Line2D object at 0x7fea5fca2c30>, <matplotlib.lines.Line2D object at 0x7fea5fca24e0>, <matplotlib.lines.Line2D object at 0x7fea5fca18e0>, <matplotlib.lines.Line2D object at 0x7fea5fca3fb0>, <matplotlib.lines.Line2D object at 0x7fea5fca07a0>, <matplotlib.lines.Line2D object at 0x7fea5fca33b0>, <matplotlib.lines.Line2D object at 0x7fea5fca2ba0>, <matplotlib.lines.Line2D object at 0x7fea5fca0950>, <matplotlib.lines.Line2D object at 0x7fea5fca3770>, <matplotlib.lines.Line2D object at 0x7fea5fca1370>, <matplotlib.lines.Line2D object at 0x7fea5fca1e80>, <matplotlib.lines.Line2D object at 0x7fea5fca1010>, <matplotlib.lines.Line2D object at 0x7fea5b6f1220>, <matplotlib.lines.Line2D object at 0x7fea5b6f1430>, <matplotlib.lines.Line2D object at 0x7fea5b6f33e0>, <matplotlib.lines.Line2D object at 0x7fea5b6f2d80>, <matplotlib.lines.Line2D object at 0x7fea5b6f0200>, <matplotlib.lines.Line2D object at 0x7fea5b6f26f0>, <matplotlib.lines.Line2D object at 0x7fea5b6f0f50>, <matplotlib.lines.Line2D object at 0x7fea5b6f2c90>, <matplotlib.lines.Line2D object at 0x7fea5b6f10d0>, <matplotlib.lines.Line2D object at 0x7fea5b6f1a00>, <matplotlib.lines.Line2D object at 0x7fea5b6f24e0>, <matplotlib.lines.Line2D object at 0x7fea5b6f1e50>, <matplotlib.lines.Line2D object at 0x7fea5b6f1be0>, <matplotlib.lines.Line2D object at 0x7fea5b6f3440>, <matplotlib.lines.Line2D object at 0x7fea5b6f3950>, <matplotlib.legend.Legend object at 0x7fea5b6f0560>]
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))