All

Improving time series animations in matplotlib (from 2D to 3D)

Pablo Leo

03/02/2021

No Comments

Animating time series is a very powerful tool to show evolution over time, but matplotlib default animations are boring and they are not well suited for comparison purposes.

Along this blog, animations are widely used: from explaining how neural networks train, to showing synthetic time-series statistics or indicating which funds are selected by the low volatility anomaly.

Imagine that you want to know which stock market had the best evolution during the COVID-19 pandemic. Anyone can answer the question by looking directly at data, but not everybody has enough expertise to understand it.

Therefore, I propose to create animations that make the comparison easier and cleaner, starting in 2D, going up to 3D, and ending up in a 3D mesh representation.

Data: European stock indices

Since the aim of this post is to improve time-series animations, we will use as data the stock indices evolution of the 10 European countries with the highest GDP (Gross Domestic Product).

The European countries with the highest GDP in 2019 (excluding Russia and Turkey) are:

# Country 
Country code Stock Index
1GermanyGERDAX
2United KingdomUKUKX
3FranceFRCAC
4ItalyITFTSEMIB
5SpainESIBEX
6NetherlandsNLAEX
7SwitzerlandCHSMI
8Poland*PLWIG
9SwedenSEOMX
10BelgiumBEBEL20

*NOTE: Poland (PL) is not present in the animations.

To compare the fall and rise of European stock indices during 2020, all the animations show the Rolling Equity 1Y (261 days) of the indices from 01/01/2019 until 29/01/2021.

2D Animation

The first approach that anyone has is to create a line plot in 2 dimensions of all the rolling equities:

2D Time series animation of all the european stock indices during the COVID-19 pandemic
2D Time series animation of all the european stock indices during the COVID-19 pandemic

The animation looks very cool, but it is difficult to compare the indices when the lines overlap in time because they have the same colormap.

This can be solved by picking a different color for each index but makes the plots less readable.

Let’s add a new dimension

3D Animation

In the second approach, a 3rd dimension is added. It separates each index evolution in a given z coordinate, creating the following animation:

3D Time series animation of all the european stock indices during the COVID-19 pandemic
3D Time series animation of all the European stock indices during the COVID-19 pandemic

This animation looks cooler than the 2D version but it has a big drawback: The sense of depth is completely lost, making the comparison very difficult.

Let’s add a mesh

3D Mesh animation

Finally, a line that joins all the values for each time step is added, creating the illusion of a mesh:

3D Time series mesh animation of all the european stock indices during the COVID-19 pandemic
3D Time-series mesh animation of all the European stock indices during the COVID-19 pandemic

Looking at this animation, the mesh really helps to compare the indices along time in a much cleaner way, which can lead to better conclusions.

COVID-19 Conclusions

From the 3D mesh graph, one can extract the following conclusions:

  • UKX (UK) and IBEX (ES) are the weakest indices before the fall and during the recovery.
  • DAX (GER), OMX (SE), SMI (CH), and AEX (NL) are the strongest indices before the fall and during the recovery.
  • CAC (FR), FTSEMIB (IT), and BEL20 (BE) are the strongest before the fall they have a very small recovery.

Looking at the 2D animation, one might extract the same conclusions but it becomes harder.


The code to generate the animations is publicly available at the following link:

0 Comments
Inline Feedbacks
View all comments