Plot multiple time series

1. Plot multiple time series

Plotting and visualizing data that contains multiple time series can be a challenging task. While the pandas library can still be leveraged to perform this task, there are a number of parameters that can be applied in order to optimize the readability of your graphs. In this chapter, we will discuss how multiple times series can be clearly displayed simultaneously in Python.

2. Clarity is key

When plotting multiple time series, matplotlib will iterate through its default color scheme until all columns in the DataFrame have been plotted. Therefore, the repetition of the default colors may make it difficult to distinguish some of the time series. For example, since there are seven time series in the meat dataset, some time series are assigned the same blue color. In addition, matplotlib does not consider the color of the background, which can also be an issue.

3. The colormap argument

To remedy this, the dot plot() method has an additional argument called colormap.This argument allows you to assign a wide range of color palettes with varying contrasts and intensities. You can either define your own Matplotlib colormap, or use a string that matches a colormap registered with matplotlib. In this example, we use the Dark2 color palette. "

4. Changing line colors with the colormap argument

Notice how colors do not repeat themselves now!

5. Enhancing your plot with information

When building slides for a presentation, or sharing plots with stakeholders, it can be more convenient for yourself and others to visualize both time series plots and numerical summaries on a single graph. In order to do so, first plot the columns of your DataFrame and return the matplotlib AxesSubplot object to the variable ax dot You can then pass any table information in pandas as a DataFrame or Series to the ax object. Here we obtain summary statistics of the DataFrame by using the dot describe() method and then pass this content as a table with the ax dot table command.

6. Adding Statistical summaries to your plots

Here is the output of that plot.

7. Dealing with different scales

In some circumstances, you may want to distinguish the time series in the dataset and have them plotted on individual graphs that are part of a larger figure. For example, you can see in this graph that the beef and veal time series have different amplitude and scale. A side-effect of this is that the y-axis is automatically scaled to the time series with the largest values, which means that the time series for beef prevents you from distinguishing some of the patterns in the veal time series that has smaller values.

8. Only veal

When plotting the veal time series alone, we can see some interesting patterns that are not easily detected otherwise. So how can you remedy this issue?

9. Facet plots

In order to overcome issues with visualizing datasets containing time series of different scales, you can leverage the subplots argument, which will plot each column of a DataFrame on a different subplot. In addition, the layout of your subplots can be specified using the layout keyword, which accepts two integers specifying the number of rows and columns to use. It is important to ensure that the total number of subplots is greater than or equal to the number of time series in your DataFrame. You can also specify if each subgraph should share the values of their x-axis and y-axis using the sharex and sharey arguments. Finally, you need to specify the total size of your graph (which will contain all subgraphs) using the figsize argument.

10. Facet plots

This parameter may need to experimented with until you find the optimal size, as it will depend on the number of time series you are working with, and the specific layout that you choose.

11. Time for some action!

Alright, let's try this ourselves!