Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Learn Visualization | Time Series: Let's Start
Time Series Analysis

book
Visualization

In the 3rd section, you will get acquainted with the visualization of time series with only one dependent variable and multiple time series with many variables that must be visualized on the same plot.
Time series visualization is one of the most important stages of data processing, allowing the expert to evaluate the big picture and roughly understand the nature of the data (stationarity, seasonality, cyclicality, etc.)

The matplotlib library is used for visualization. An example of visualizing time series with one variable:

python
import matplotlib.pyplot as plt
import pandas as pd

data = pd.read_csv("example.csv")

fig, ax = plt.subplots(figsize=(11, 9))
ax.plot(data["Date"], data["Price"])
ax.set_xlabel("Date")
ax.set_ylabel("Price")
plt.tight_layout()

What problems can arise when rendering multiple time series on the same plot?

If the number of time series you want to visualize does not exceed 5, then creating one plot with them can be a good idea, but when there are more than 10, it can become difficult:

In the following chapters, you will learn how to visualize a large number of time series in a way that makes it easier for you, as an expert, to explore the data:

Task

Swipe to start coding

Use the time series from the co2 dataset and visualize it.

  1. Create Figure and Axes objects assigned to the fig, ax variables.

  2. Initialize a line plot with dates (.index of the data) on the x-axis and level of CO2 (the "co2" column of the data) on the y-axis.

  3. Add the label "Time" on the x-axis.

Once you've completed this task, click the button below the code to check your solution.

Solution

# Loading libraries
import numpy as np
import pandas as pd
import statsmodels.api as sm
import matplotlib.pyplot as plt

# Reading data
data = sm.datasets.co2.load_pandas().data

# Creating Figure and Axes objects with specified sie
fig, ax = plt.subplots(figsize=(12, 6))

# Initialize the plot
ax.plot(data.index, data["co2"])

# Add axis labels
ax.set_xlabel("Time")
ax.set_ylabel("CO2 concentration")

# Adjust the padding between and around subplots, and display the plot
plt.tight_layout()
plt.show()

Everything was clear?

How can we improve it?

Thanks for your feedback!

Section 1. Chapter 5
# Loading libraries
import numpy as np
import pandas as pd
import statsmodels.api as sm
import matplotlib.pyplot as plt

# Reading data
data = sm.datasets.co2.load_pandas().data

# Creating Figure and Axes objects with specified sie
fig, ax = plt.___(figsize=(12, 6))

# Initialize the plot
ax.plot(___, data["___"])

# Add axis labels
ax.set_xlabel("___")
ax.set_ylabel("CO2 concentration")

# Adjust the padding between and around subplots, and display the plot
plt.tight_layout()
plt.show()
toggle bottom row
We use cookies to make your experience better!
some-alt