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 0x7f93b7bf1850>, <matplotlib.legend.Legend object at 0x7f93b7bf0e90>]
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 0x7f93b2d93bf0>, <matplotlib.lines.Line2D object at 0x7f93b2d467e0>, <matplotlib.lines.Line2D object at 0x7f93b7fba810>, <matplotlib.lines.Line2D object at 0x7f93b7fbb200>, <matplotlib.lines.Line2D object at 0x7f93b7fbb890>, <matplotlib.lines.Line2D object at 0x7f93b73fca10>, <matplotlib.lines.Line2D object at 0x7f93b73fdbb0>, <matplotlib.lines.Line2D object at 0x7f93b2bea930>, <matplotlib.lines.Line2D object at 0x7f93b73fd2b0>, <matplotlib.lines.Line2D object at 0x7f93b73fe900>, <matplotlib.lines.Line2D object at 0x7f93b73fd6a0>, <matplotlib.lines.Line2D object at 0x7f93b73fea20>, <matplotlib.lines.Line2D object at 0x7f93b73fc320>, <matplotlib.lines.Line2D object at 0x7f93b73fd910>, <matplotlib.lines.Line2D object at 0x7f93b73fea80>, <matplotlib.lines.Line2D object at 0x7f93b73fdd90>, <matplotlib.lines.Line2D object at 0x7f93b73fef30>, <matplotlib.lines.Line2D object at 0x7f93b73fe4e0>, <matplotlib.lines.Line2D object at 0x7f93b73feb70>, <matplotlib.lines.Line2D object at 0x7f93b73fcef0>, <matplotlib.lines.Line2D object at 0x7f93b73fcf50>, <matplotlib.lines.Line2D object at 0x7f93b7efe330>, <matplotlib.lines.Line2D object at 0x7f93b362b380>, <matplotlib.lines.Line2D object at 0x7f93b3629010>, <matplotlib.lines.Line2D object at 0x7f93b3629ee0>, <matplotlib.lines.Line2D object at 0x7f93b3b1f320>, <matplotlib.lines.Line2D object at 0x7f93b3b1ea50>, <matplotlib.lines.Line2D object at 0x7f93b2ce32f0>, <matplotlib.lines.Line2D object at 0x7f93b2ce1b20>, <matplotlib.lines.Line2D object at 0x7f93b2ce1790>, <matplotlib.lines.Line2D object at 0x7f93b2ce3650>, <matplotlib.lines.Line2D object at 0x7f93b2ce0e00>, <matplotlib.lines.Line2D object at 0x7f93b2ce2de0>, <matplotlib.lines.Line2D object at 0x7f93b2ce26f0>, <matplotlib.lines.Line2D object at 0x7f93b2ce1700>, <matplotlib.lines.Line2D object at 0x7f93b2ce3980>, <matplotlib.lines.Line2D object at 0x7f93b2ce11f0>, <matplotlib.lines.Line2D object at 0x7f93b2ce0b90>, <matplotlib.lines.Line2D object at 0x7f93b2ce17c0>, <matplotlib.lines.Line2D object at 0x7f93b2ce3710>, <matplotlib.lines.Line2D object at 0x7f93b2ce0950>, <matplotlib.lines.Line2D object at 0x7f93b2ce1580>, <matplotlib.lines.Line2D object at 0x7f93b2ce3c50>, <matplotlib.lines.Line2D object at 0x7f93b2ce0800>, <matplotlib.lines.Line2D object at 0x7f93b2ce23f0>, <matplotlib.lines.Line2D object at 0x7f93b2ce02f0>, <matplotlib.lines.Line2D object at 0x7f93b2ce3b00>, <matplotlib.lines.Line2D object at 0x7f93b2ce2d80>, <matplotlib.lines.Line2D object at 0x7f93b2ce1a90>, <matplotlib.lines.Line2D object at 0x7f93b2ce33b0>, <matplotlib.lines.Line2D object at 0x7f93b2ce1460>, <matplotlib.lines.Line2D object at 0x7f93b3855940>, <matplotlib.lines.Line2D object at 0x7f93b3856c90>, <matplotlib.lines.Line2D object at 0x7f93b3856a50>, <matplotlib.lines.Line2D object at 0x7f93b3855a90>, <matplotlib.lines.Line2D object at 0x7f93b38551c0>, <matplotlib.lines.Line2D object at 0x7f93b3855f70>, <matplotlib.lines.Line2D object at 0x7f93b3855cd0>, <matplotlib.lines.Line2D object at 0x7f93b3856240>, <matplotlib.lines.Line2D object at 0x7f93b3857230>, <matplotlib.lines.Line2D object at 0x7f93b3857f80>, <matplotlib.lines.Line2D object at 0x7f93b3857f20>, <matplotlib.lines.Line2D object at 0x7f93b38555b0>, <matplotlib.lines.Line2D object at 0x7f93b3857e00>, <matplotlib.lines.Line2D object at 0x7f93b3855c70>, <matplotlib.lines.Line2D object at 0x7f93b3856d50>, <matplotlib.lines.Line2D object at 0x7f93b38562a0>, <matplotlib.lines.Line2D object at 0x7f93b3854c50>, <matplotlib.lines.Line2D object at 0x7f93b87b9fd0>, <matplotlib.lines.Line2D object at 0x7f93b87ba330>, <matplotlib.lines.Line2D object at 0x7f93b85a0980>, <matplotlib.lines.Line2D object at 0x7f93b85a32f0>, <matplotlib.lines.Line2D object at 0x7f93b85a0a10>, <matplotlib.lines.Line2D object at 0x7f93b85a3830>, <matplotlib.lines.Line2D object at 0x7f93b85a3020>, <matplotlib.lines.Line2D object at 0x7f93b85a12e0>, <matplotlib.lines.Line2D object at 0x7f93b85a3bc0>, <matplotlib.lines.Line2D object at 0x7f93b85a3770>, <matplotlib.lines.Line2D object at 0x7f93b85a3b30>, <matplotlib.lines.Line2D object at 0x7f93b85a1610>, <matplotlib.lines.Line2D object at 0x7f93b85a0f80>, <matplotlib.lines.Line2D object at 0x7f93b85a1eb0>, <matplotlib.lines.Line2D object at 0x7f93b85a2900>, <matplotlib.lines.Line2D object at 0x7f93b85a2ae0>, <matplotlib.lines.Line2D object at 0x7f93b85a26c0>, <matplotlib.lines.Line2D object at 0x7f93b85a3290>, <matplotlib.lines.Line2D object at 0x7f93b85a3ef0>, <matplotlib.lines.Line2D object at 0x7f93b85a0ef0>, <matplotlib.lines.Line2D object at 0x7f93b85a2360>, <matplotlib.lines.Line2D object at 0x7f93b85a0500>, <matplotlib.lines.Line2D object at 0x7f93b85a0260>, <matplotlib.lines.Line2D object at 0x7f93b85a1520>, <matplotlib.lines.Line2D object at 0x7f93b85a33e0>, <matplotlib.lines.Line2D object at 0x7f93b85e06e0>, <matplotlib.lines.Line2D object at 0x7f93b85e0fe0>, <matplotlib.lines.Line2D object at 0x7f93b85e0fb0>, <matplotlib.lines.Line2D object at 0x7f93b85e0470>, <matplotlib.lines.Line2D object at 0x7f93b85e19a0>, <matplotlib.lines.Line2D object at 0x7f93b85e04d0>, <matplotlib.lines.Line2D object at 0x7f93b240ae40>, <matplotlib.legend.Legend object at 0x7f93b7bf2de0>]
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))