Plot multiple time-series DataFrames into a single plot using Pandas (Matplotlib)

To plot multiple time-series DataFrames into a single plot using Pandas and Matplotlib, you can overlay different series on the same axes or use secondary y-axes for different scales.

Steps to Create Multiple Time-Series Plot

  • Set the figure size and adjust the padding between subplots
  • Create a Pandas DataFrame with time series data
  • Set the datetime column as the index
  • Plot multiple series using plot() method
  • Use secondary_y=True for different scales
  • Display the figure using show() method

Example

Here's how to plot multiple time-series data with different currencies ?

import numpy as np
import pandas as pd
from matplotlib import pyplot as plt

plt.rcParams["figure.figsize"] = [7.50, 3.50]
plt.rcParams["figure.autolayout"] = True

# Create DataFrame with time series data
df = pd.DataFrame({
    'date': list(pd.date_range("2021-01-01", periods=10)),
    'rupees': np.linspace(1, 10, 10),
    'dollar': np.linspace(10, 20, 10)
})

# Set datetime index
df = df.set_index(pd.to_datetime(df['date']), drop=True)

# Plot both series
df['rupees'].plot(grid=True, label="Rupees", legend=True, color='blue')
df['dollar'].plot(secondary_y=True, label="Dollar", legend=True, color='red')

plt.title('Multiple Time-Series Plot')
plt.show()

Alternative Method - Single Axis

You can also plot both series on the same y-axis if they have similar scales ?

import numpy as np
import pandas as pd
from matplotlib import pyplot as plt

# Create sample data with similar scales
df = pd.DataFrame({
    'date': pd.date_range("2021-01-01", periods=10),
    'series_a': np.random.randn(10).cumsum(),
    'series_b': np.random.randn(10).cumsum()
})

df = df.set_index('date')

# Plot both series on same axis
df.plot(figsize=(10, 6), grid=True, title='Multiple Time-Series on Same Axis')
plt.ylabel('Values')
plt.legend()
plt.show()

Key Parameters

Parameter Description Usage
secondary_y Creates secondary y-axis For different scales
grid=True Adds grid lines Better readability
legend=True Shows legend Identifies series
figsize Sets plot dimensions Controls plot size

Conclusion

Use secondary_y=True when plotting time-series with different scales. For similar scales, plot on the same axis with different colors and labels for better comparison.

Updated on: 2026-03-25T22:08:18+05:30

2K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements