In today’s data visualization we will learn how to plot multiple time series on the same chart with pandas. Time series is basically information that is time dependent such as prices, inventory, sales etc’.
In our example, we would like to plot revenue, expenses and ultimately profit for a fictitious business. We’ll use a line chart to better display the time series data.
Step #1: Create example data
Let’s start by creating a DataFrame containing our time series data.
import pandas as pd %matplotlib inline # define DataFrame time_stamps = pd.Series(pd.date_range(start='01/06/23', end = '01/17/23', freq='B')) revenue = [110, 130, 123, 189, 165, 232, 210, 324] expense = [67, 54, 120, 132, 100, 56, 45, 89] performance = pd.DataFrame(dict(revenue=revenue, expense=expense), index = time_stamps) #calculate the daily profit performance['profit'] = performance['revenue'] - performance['expense']
Step #2: Plotting multiple daily time series columns
As pandas has significant amount of the matplotlib library embedded, we can it to render some nice charts. In this example we’ll render our numeric columns in one figure.
ts_plot = performance.plot(kind='line', \ title = 'Revenue vs Expense January 23'); ts_plot.grid() # define the legend location ts_plot.legend(loc='upper left');
Here’s our chart:
Step #3: Draw multiple time series plots on one figure
Next case is to draw multiple plots on one figure. In order to do so, we will first import matplotlib, then define a figure (fig) object with two axes (0 and 1) then draw our time series on each.
import matplotlib.pyplot as plt fig, ax = plt.subplots(1,2, figsize= (10,6),sharex=True) fig.autofmt_xdate(rotation=45) ax.plot(time_stamps,revenue); ax.set_xlabel ('time') ax.set_title ('Revenue over time') ax.plot(time_stamps,expense, color='orange'); ax.set_title ('Expenses over time') ax.set_xlabel ('time');
Here’s our chart: