How to plot time series with Python in Matplotlib and Pandas?

A common tasks we have as Data Analysts is to visualize data that was aggregated over time. In this tutorial we’ll learn how to quickly chart time series data with Pandas and Matplotlib libraries.

Creating example data

As always, we’ll start by importing the required libraries into our Jupyter Notebook/Lab namespace.


# Importing Python Data Analysis libraries
import pandas as pd
import numpy as np
np.random.seed(10)

We’ll now create a short chunk of time series data that we will use in our analysis.


periods = 8

# Create time series with weekly frequency
time = pd.date_range('2021-01-10', periods = periods, freq = 'w')

# Calculate the week number
week = time.isocalendar().week

#Random data
qty = np.random.randint(80,100,periods)

# Create DataFrame
interviews = pd.DataFrame(dict(week=week, qty = qty))

Plotting time series from Pandas DataFrames

We’ll now use the DataFrame that we just created to plot the time series data. We’ll use the DataFrame.plot() available as part of the Pandas library to render the graph.

Note that in our example we constructed the DataFrame from some random data that we generated, but we could obviously acquire the data from a csv file, excel, json, html or a SQL database.


bar = interviews.plot(kind='bar', x='week', y='qty', title ='Bar chart with Pandas',label='Quantity', color='Purple', rot=45)
bar.legend(bbox_to_anchor= (1.02, 1));

Here is our bar chart (we could as well use other chart types such as line, histogram and so forth).

The x-axis depicts the time series , displayed using a calendar week frequency.

Python time series charts with Matplotlib

We can generate a similar bar chart using the Matplotlib library.

import matplotlib.pyplot as plt

plt.bar(x=week, height = qty, label='Interviews')
plt.title('Bar chart with Matplotlib')
plt.xlabel('Week')
plt.ylabel('Interviews')
plt.legend(bbox_to_anchor= (1.015, 1));

Additional Learning