Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
How to get multiple overlapping plots with independent scaling in Matplotlib?
When creating visualizations with multiple data series that have different scales, you need overlapping plots with independent Y-axis scaling. Matplotlib's twinx() method allows you to create twin axes that share the same X-axis but have separate Y-axis scales.
Basic Approach
The key steps are ?
Create the primary subplot with
plt.subplots()Plot the first dataset on the primary Y-axis
Create a twin axis using
twinx()that shares the X-axisPlot the second dataset on the twin Y-axis
Customize colors and labels for clarity
Example
import matplotlib.pyplot as plt
# Set figure size
plt.rcParams["figure.figsize"] = [7.50, 3.50]
plt.rcParams["figure.autolayout"] = True
# Create the primary subplot
fig, ax1 = plt.subplots()
# Plot first dataset on primary Y-axis (left)
x_data = [1, 2, 3, 4, 5]
y1_data = [10, 20, 15, 25, 30]
ax1.plot(x_data, y1_data, color='red', label='Dataset 1')
ax1.set_ylabel('Primary Y-axis', color='red')
ax1.tick_params(axis='y', labelcolor='red')
# Create twin axis sharing the same X-axis
ax2 = ax1.twinx()
# Plot second dataset on secondary Y-axis (right)
y2_data = [100, 150, 300, 400, 200]
ax2.plot(x_data, y2_data, color='blue', label='Dataset 2')
ax2.set_ylabel('Secondary Y-axis', color='blue')
ax2.tick_params(axis='y', labelcolor='blue')
# Add title and show
plt.title('Multiple Overlapping Plots with Independent Scaling')
plt.show()
Output
Advanced Example with Different Data Types
Here's a more practical example showing temperature and rainfall data ?
import matplotlib.pyplot as plt
# Sample data
months = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun']
temperature = [5, 7, 12, 18, 22, 25] # Celsius
rainfall = [80, 60, 45, 30, 40, 20] # mm
fig, ax1 = plt.subplots(figsize=(10, 6))
# Temperature plot (left Y-axis)
color = 'tab:red'
ax1.set_xlabel('Months')
ax1.set_ylabel('Temperature (°C)', color=color)
ax1.plot(months, temperature, color=color, marker='o', linewidth=2)
ax1.tick_params(axis='y', labelcolor=color)
# Rainfall plot (right Y-axis)
ax2 = ax1.twinx()
color = 'tab:blue'
ax2.set_ylabel('Rainfall (mm)', color=color)
ax2.bar(months, rainfall, color=color, alpha=0.6, width=0.4)
ax2.tick_params(axis='y', labelcolor=color)
plt.title('Temperature vs Rainfall - Independent Scaling')
plt.tight_layout()
plt.show()
Key Points
twinx()creates a twin axis sharing the X-axis but with independent Y-axis scalingEach axis can have different data ranges without affecting the other
Use different colors for each axis to avoid confusion
Set
tick_params(axis='y', labelcolor=color)to match axis colorsBoth line plots and bar charts can be overlapped effectively
Conclusion
Use twinx() to create overlapping plots with independent Y-axis scaling. This technique is perfect for comparing datasets with different units or scales on the same timeline or category axis.
