Unlock the Power of Quantitative Strategies: Explore Our Cutting-Edge Website Today!

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

### Pablo Leo

#### 03/02/2021

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 1 Germany GER DAX 2 United Kingdom UK UKX 3 France FR CAC 4 Italy IT FTSEMIB 5 Spain ES IBEX 6 Netherlands NL AEX 7 Switzerland CH SMI 8 Poland* PL WIG 9 Sweden SE OMX 10 Belgium BE BEL20

*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:

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.

### 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:

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.

### 3D Mesh animation

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

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: