# 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 0x7f14c87c6e70>, <matplotlib.legend.Legend object at 0x7f14ca5a8740>]
```

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 0x7f14c8e4d580>, <matplotlib.lines.Line2D object at 0x7f14c6faf200>, <matplotlib.lines.Line2D object at 0x7f14c6faf440>, <matplotlib.lines.Line2D object at 0x7f14c6faf830>, <matplotlib.lines.Line2D object at 0x7f14c6fafe00>, <matplotlib.lines.Line2D object at 0x7f14c6faec00>, <matplotlib.lines.Line2D object at 0x7f14c862ef00>, <matplotlib.lines.Line2D object at 0x7f14c862e330>, <matplotlib.lines.Line2D object at 0x7f14c862cd70>, <matplotlib.lines.Line2D object at 0x7f14c862cc20>, <matplotlib.lines.Line2D object at 0x7f14c862ee70>, <matplotlib.lines.Line2D object at 0x7f14c862fc20>, <matplotlib.lines.Line2D object at 0x7f14c862d3d0>, <matplotlib.lines.Line2D object at 0x7f14c862ce30>, <matplotlib.lines.Line2D object at 0x7f14c862e360>, <matplotlib.lines.Line2D object at 0x7f14c862c050>, <matplotlib.lines.Line2D object at 0x7f14c862dd30>, <matplotlib.lines.Line2D object at 0x7f14c862dbe0>, <matplotlib.lines.Line2D object at 0x7f14c84ba2a0>, <matplotlib.lines.Line2D object at 0x7f14c84b94f0>, <matplotlib.lines.Line2D object at 0x7f14c84ba150>, <matplotlib.lines.Line2D object at 0x7f14c84ba270>, <matplotlib.lines.Line2D object at 0x7f14c84ba600>, <matplotlib.lines.Line2D object at 0x7f14c84bbda0>, <matplotlib.lines.Line2D object at 0x7f14c84baff0>, <matplotlib.lines.Line2D object at 0x7f14c84b8f20>, <matplotlib.lines.Line2D object at 0x7f14c84bbfe0>, <matplotlib.lines.Line2D object at 0x7f14c84b80b0>, <matplotlib.lines.Line2D object at 0x7f14c84ba8d0>, <matplotlib.lines.Line2D object at 0x7f14c67804a0>, <matplotlib.lines.Line2D object at 0x7f14c6781e20>, <matplotlib.lines.Line2D object at 0x7f14c67802f0>, <matplotlib.lines.Line2D object at 0x7f14c6781a60>, <matplotlib.lines.Line2D object at 0x7f14c6783530>, <matplotlib.lines.Line2D object at 0x7f14c6783950>, <matplotlib.lines.Line2D object at 0x7f14c6783e00>, <matplotlib.lines.Line2D object at 0x7f14c6783a70>, <matplotlib.lines.Line2D object at 0x7f14c6782b10>, <matplotlib.lines.Line2D object at 0x7f14c6783650>, <matplotlib.lines.Line2D object at 0x7f14c6783770>, <matplotlib.lines.Line2D object at 0x7f14c67831d0>, <matplotlib.lines.Line2D object at 0x7f14c6782720>, <matplotlib.lines.Line2D object at 0x7f14c67824b0>, <matplotlib.lines.Line2D object at 0x7f14c6782180>, <matplotlib.lines.Line2D object at 0x7f14c83a5190>, <matplotlib.lines.Line2D object at 0x7f14c83a6d20>, <matplotlib.lines.Line2D object at 0x7f14c83a7b00>, <matplotlib.lines.Line2D object at 0x7f14c83a4f80>, <matplotlib.lines.Line2D object at 0x7f14c83a5520>, <matplotlib.lines.Line2D object at 0x7f14c83a6390>, <matplotlib.lines.Line2D object at 0x7f14c83a6870>, <matplotlib.lines.Line2D object at 0x7f14c83a6780>, <matplotlib.lines.Line2D object at 0x7f14c83a5010>, <matplotlib.lines.Line2D object at 0x7f14ca3adf70>, <matplotlib.lines.Line2D object at 0x7f14ca3ad640>, <matplotlib.lines.Line2D object at 0x7f14ca3adeb0>, <matplotlib.lines.Line2D object at 0x7f14ca3ac350>, <matplotlib.lines.Line2D object at 0x7f14c88b8b30>, <matplotlib.lines.Line2D object at 0x7f14c88b9b50>, <matplotlib.lines.Line2D object at 0x7f14c88b89b0>, <matplotlib.lines.Line2D object at 0x7f14c85bb710>, <matplotlib.lines.Line2D object at 0x7f14c85b9940>, <matplotlib.lines.Line2D object at 0x7f14c85b8c20>, <matplotlib.lines.Line2D object at 0x7f14c85b8d10>, <matplotlib.lines.Line2D object at 0x7f14c85b9ca0>, <matplotlib.lines.Line2D object at 0x7f14c85bbcb0>, <matplotlib.lines.Line2D object at 0x7f14c6d99970>, <matplotlib.lines.Line2D object at 0x7f14c6d98230>, <matplotlib.lines.Line2D object at 0x7f14c6d991f0>, <matplotlib.lines.Line2D object at 0x7f14c6d9a4e0>, <matplotlib.lines.Line2D object at 0x7f14c6d9b9b0>, <matplotlib.lines.Line2D object at 0x7f14c6d9af00>, <matplotlib.lines.Line2D object at 0x7f14c6d9a240>, <matplotlib.lines.Line2D object at 0x7f14c6d9b440>, <matplotlib.lines.Line2D object at 0x7f14c6d980b0>, <matplotlib.lines.Line2D object at 0x7f14c6d98680>, <matplotlib.lines.Line2D object at 0x7f14c6d98830>, <matplotlib.lines.Line2D object at 0x7f14c6d98c80>, <matplotlib.lines.Line2D object at 0x7f14c6d99250>, <matplotlib.lines.Line2D object at 0x7f14c6d999d0>, <matplotlib.lines.Line2D object at 0x7f14c6d9a150>, <matplotlib.lines.Line2D object at 0x7f14c6d9a360>, <matplotlib.lines.Line2D object at 0x7f14c6d9a840>, <matplotlib.lines.Line2D object at 0x7f14c6d9ac90>, <matplotlib.lines.Line2D object at 0x7f14c6d9b2f0>, <matplotlib.lines.Line2D object at 0x7f14c6d9b530>, <matplotlib.lines.Line2D object at 0x7f14c6d98b30>, <matplotlib.lines.Line2D object at 0x7f14c6d9bf80>, <matplotlib.lines.Line2D object at 0x7f14c6e8c9e0>, <matplotlib.lines.Line2D object at 0x7f14c6e8efc0>, <matplotlib.lines.Line2D object at 0x7f14c6e8d040>, <matplotlib.lines.Line2D object at 0x7f14c6e8dc70>, <matplotlib.lines.Line2D object at 0x7f14c6e8ef00>, <matplotlib.lines.Line2D object at 0x7f14c6e8dbe0>, <matplotlib.lines.Line2D object at 0x7f14c6e8f0e0>, <matplotlib.lines.Line2D object at 0x7f14c6e8f3b0>, <matplotlib.lines.Line2D object at 0x7f14c6e8d7c0>, <matplotlib.lines.Line2D object at 0x7f14c6e8d550>, <matplotlib.lines.Line2D object at 0x7f14c6e8e090>, <matplotlib.lines.Line2D object at 0x7f14c6e8e4b0>, <matplotlib.legend.Legend object at 0x7f14c862fda0>]
```

### 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))
```