Open In App

Plot multiple time series DataFrame into a single plot - Pandas

Last Updated : 16 Jun, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

In this article, we’ll explore how to plot multiple time series from Pandas DataFrames into a single plot. When working with multiple time series, the most important factor is ensuring that their indexes (usually DateTime indexes) are aligned. We’ll cover two common scenarios:

  • Time series with the same DateTime index
  • Time series with different DateTime indexes

Time Series with the Same DateTime Index

When multiple time series share the same DateTime index, plotting them together is straightforward. Since their timestamps are already aligned, you can directly call .plot() on the DataFrame or individual columns to visualize them on a single graph.

Step 1: Importing Libraries

First, import the required libraries. pandas manages the data, while matplotlib.pyplot handles plotting. plt.style.use('default') sets a clean style and %matplotlib inline (for Jupyter) ensures plots render within the notebook.

Python
import pandas as pd
import matplotlib.pyplot as plt
plt.style.use('default')
%matplotlib inline  # if using Jupyter Notebook

Step 2: Load Stock Data

We will be plotting open prices of three stocks Tesla, Ford, and general motors, You can download the data from here or yfinance library.

Tesla file:

Python
tesla = pd.read_csv('Tesla_Stock.csv', index_col='Date',parse_dates=True) 
tesla.head(10) 

Output

Ford_stock:

Python
ford = pd.read_csv('Ford_Stock.csv', index_col='Date', parse_dates=True)
ford.head(10)

Output

GM_Stock:

Python
gm = pd.read_csv('GM_Stock.csv', index_col='Date', parse_dates=True)
gm.head(10)

Output

Step 3: Now Plotting Open Prices of the Stocks

Since the time series share the same DateTime index, plotting is straightforward. You can directly call .plot() on the 'Open' column of each DataFrame to overlay them.

Python
plt.figure(figsize=(16, 8), dpi=150)

tesla['Open'].plot(label='Tesla', color='orange')
gm['Open'].plot(label='GM', color='blue')
ford['Open'].plot(label='Ford', color='green')

plt.title('Open Price Plot')
plt.xlabel('Years')
plt.ylabel('Open Price')
plt.legend()
plt.grid(True)
plt.show()

Output

Explanation: Since all three stocks have the same DateTime index, their data aligns perfectly across time. This allows us to directly call .plot() on each DataFrame’s 'Open' column without any preprocessing.

Plotting DataFrames with different DateTime Index

When time series DataFrames have different DateTime indexes, direct plotting can result in misaligned or incomplete data. To visualize them together accurately, you first need to align their indexes, typically by reindexing one DataFrame to match the other before plotting.

Step 1: Importing Libraries

First, import the required libraries. pandas manages the data, while matplotlib.pyplot handles plotting. plt.style.use('default') sets a clean style and %matplotlib inline (for Jupyter) ensures plots render within the notebook.

Python
import pandas as pd
import matplotlib.pyplot as plt
plt.style.use('default')

# If using Jupyter Notebook:
%matplotlib inline

Step 2: Importing Data

Here we load Apple (AAPL) and Microsoft (MSFT) stock price data, which have different date ranges.

Python
aapl = pd.read_csv('aapl.csv', index_col='Date', parse_dates=True)
aapl.head(10)

Output

msft file:

Python
msft = pd.read_csv('msft.csv', index_col='Date', parse_dates=True)
msft.head(10)

Output

Step 3: Align Index

Since their DateTime indexes do not completely overlap, we merge Microsoft’s prices into Apple’s DataFrame based on the Date index. This creates missing values where dates do not match.

Python
aapl["MSFT"] = msft["MSFT"]  # Merge MSFT prices by Date
aapl.dropna(inplace=True)    # Clean missing data

aapl.head(10)

Output

Step 4: Plot Aligned Time Series

Once the DateTime indexes of both time series are aligned and missing values handled, we can now plot them together. This visual representation allows for easy comparison of stock performance over time.

Python
plt.figure(figsize=(16, 8), dpi=150)
aapl.plot(label='aapl', color=['orange', 'green'])

# adding title
plt.title('Price Plot')

# adding label to x-axis
plt.xlabel('Years')

# adding legend.
plt.legend()

Output

Explanation: Apple and Microsoft data have different date ranges. When we merge them by index, gaps (NaNs) appear where one stock has data but the other does not. We handle this by using dropna() before plotting, ensuring clean, aligned visuals.

In some cases we can't afford to lose data, so we can also plot without removing missing values, plot for the same will look like:


Next Article
Practice Tags :

Similar Reads