Recently, I was working on a Python data visualization project where I needed to display the same dataset on two Y axes in a single Matplotlib chart.
At first, I thought this would be a simple task, but I quickly realized that setting up dual Y axes in Matplotlib can be a bit tricky if you haven’t done it before.
In this tutorial, I’ll show you exactly how I did it, step by step. I’ll use simple, real-world examples so that even if you’re new to Python or Matplotlib, you’ll be able to follow along easily.
What Are Dual Y Axes in Matplotlib?
In Python’s Matplotlib library, you can create a plot with two Y axes when you want to visualize different scales or simply emphasize the same dataset in two different ways.
While this technique is often used for comparing two datasets, it can also be handy when you want to highlight the same data but with different visual properties, for example, showing both a line and a bar for the same values.
Use Two Y Axes for the Same Data
Here are a few practical reasons why you might want to use two Y axes for the same dataset in Python:
- To emphasize trends using two different plot styles (like a line and a bar chart).
- To compare visual scales — for example, showing both Fahrenheit and Celsius on two Y axes.
- To enhance readability in dashboards or reports, where one side of the chart is easier to read.
Now, let’s dive into how to create this in Matplotlib using Python.
Method 1 – Use twinx() in Python Matplotlib
The simplest way to create two Y axes in Matplotlib is by using the twinx() function. This function creates a secondary Y-axis that shares the same X-axis, allowing you to plot the same or different data on both sides.
Let me show you how I did it in Python.
Example: Plot the Same Data on Two Y Axes
In this example, I’ll plot the average monthly temperatures in New York City using the same dataset on both Y axes.
This is a great way to highlight the same trend using two different visual styles — a line and a scatter plot.
# Importing necessary Python libraries
import matplotlib.pyplot as plt
import numpy as np
# Creating data for months and average temperature
months = np.arange(1, 13)
temperature_fahrenheit = np.array([32, 35, 45, 55, 65, 75, 80, 78, 70, 60, 50, 38])
# Creating the first plot
fig, ax1 = plt.subplots(figsize=(10, 6))
# Plotting temperature as a line chart
ax1.plot(months, temperature_fahrenheit, color='blue', marker='o', label='Temperature (°F)')
ax1.set_xlabel('Month')
ax1.set_ylabel('Temperature (°F)', color='blue')
ax1.tick_params(axis='y', labelcolor='blue')
# Creating a secondary Y-axis using twinx()
ax2 = ax1.twinx()
# Plotting the same data as a scatter plot
ax2.scatter(months, temperature_fahrenheit, color='red', label='Temperature (°F) - Scatter')
ax2.set_ylabel('Temperature (°F)', color='red')
ax2.tick_params(axis='y', labelcolor='red')
# Adding title and legend
plt.title('Average Monthly Temperature in New York City (Same Data on Two Y Axes)')
fig.tight_layout()
plt.show()I executed the above example code and added the screenshot below.

This Python code creates a clean and informative chart where the same temperature data is displayed both as a line and as points using two Y axes.
The left Y-axis is blue, and the right Y-axis is red, making it easy to compare the two visualizations side by side.
How It Works
The twinx() method in Matplotlib duplicates the Y-axis of the current plot and places it on the right side of the chart.
Even though both axes represent the same data, you can style them differently — change colors, labels, or even plot types, to make your visualization more engaging.
Method 2 – Use secondary_yaxis() for Custom Scaling
Sometimes, you may want to create a second Y-axis that represents the same data but in a different unit or scale. For example, you might want to show temperature in both Fahrenheit and Celsius on the same chart.
This is where the secondary_yaxis() method in Matplotlib becomes very useful.
Example: Fahrenheit and Celsius Conversion
Here’s how you can use Python to create a Matplotlib chart that displays the same data in Fahrenheit and Celsius.
import matplotlib.pyplot as plt
import numpy as np
# Data for average monthly temperature in Fahrenheit
months = np.arange(1, 13)
temperature_fahrenheit = np.array([32, 35, 45, 55, 65, 75, 80, 78, 70, 60, 50, 38])
# Function to convert Fahrenheit to Celsius
def fahrenheit_to_celsius(f):
return (f - 32) * 5 / 9
def celsius_to_fahrenheit(c):
return c * 9 / 5 + 32
# Create the figure and primary axis
fig, ax = plt.subplots(figsize=(10, 6))
# Plot the data in Fahrenheit
ax.plot(months, temperature_fahrenheit, color='blue', marker='o', label='Temperature (°F)')
ax.set_xlabel('Month')
ax.set_ylabel('Temperature (°F)', color='blue')
ax.tick_params(axis='y', labelcolor='blue')
# Add a secondary Y-axis for Celsius
ax_secondary = ax.secondary_yaxis('right', functions=(fahrenheit_to_celsius, celsius_to_fahrenheit))
ax_secondary.set_ylabel('Temperature (°C)', color='red')
ax_secondary.tick_params(axis='y', labelcolor='red')
# Add title and grid
plt.title('Average Monthly Temperature in New York City (°F and °C)')
plt.grid(True)
plt.tight_layout()
plt.show()I executed the above example code and added the screenshot below.

This Python example clearly shows how to use secondary_yaxis() to represent the same dataset in two different scales.
It’s particularly useful when you want to present data to audiences who use different measurement systems (for example, Fahrenheit in the USA and Celsius in Europe).
How It Works
The secondary_yaxis() method lets you define conversion functions between the two axes.
In this example, I used simple mathematical formulas to convert Fahrenheit to Celsius and vice versa. Matplotlib automatically adjusts the secondary Y-axis labels according to these functions.
Method 3 – Customize the Dual Y-Axis Chart
Once you’ve created a dual Y-axis chart in Python, you can easily customize it to make it more visually appealing or publication-ready.
Here’s how you can add gridlines, legends, and annotations to your Matplotlib chart.
import matplotlib.pyplot as plt
import numpy as np
months = np.arange(1, 13)
temperature_fahrenheit = np.array([32, 35, 45, 55, 65, 75, 80, 78, 70, 60, 50, 38])
fig, ax1 = plt.subplots(figsize=(10, 6))
# Primary Y-axis (line)
ax1.plot(months, temperature_fahrenheit, color='blue', marker='o', label='Temperature (°F)')
ax1.set_xlabel('Month')
ax1.set_ylabel('Temperature (°F)', color='blue')
ax1.tick_params(axis='y', labelcolor='blue')
# Secondary Y-axis (bar)
ax2 = ax1.twinx()
ax2.bar(months, temperature_fahrenheit, alpha=0.3, color='orange', label='Temperature (°F) - Bar')
ax2.set_ylabel('Temperature (°F)', color='orange')
ax2.tick_params(axis='y', labelcolor='orange')
# Adding title, legends, and grid
plt.title('Dual Y Axes with Same Data in Python Matplotlib')
ax1.legend(loc='upper left')
ax2.legend(loc='upper right')
plt.grid(True, linestyle='--', alpha=0.5)
plt.tight_layout()
plt.show()I executed the above example code and added the screenshot below.

This Python code adds a bar chart overlay to the same data, combining two visualization types for a more dynamic presentation.
You can use this approach for business dashboards, weather reports, or any analytics visualization where visual clarity matters.
Tips for Using Two Y Axes in Python Matplotlib
Here are a few things I’ve learned from experience when using dual Y axes in Python:
- Keep the chart simple — too many axes can confuse viewers.
- Always label both Y axes clearly with units.
- Use distinct colors for each axis to improve readability.
- Be consistent with scales if you’re using the same data on both axes.
Common Use Cases in the USA
Here are a few real-world examples where I’ve used this technique in Python projects for U.S.-based clients:
- Comparing sales performance in dollars and units sold.
- Displaying temperature data in Fahrenheit and Celsius.
- Showing stock market trends as both a line and a bar chart for the same dataset.
These visualizations help make reports more interactive and easier to interpret.
Wrapping Up
While Matplotlib doesn’t have a direct “dual Y-axis for same data” button, using Python’s twinx() and secondary_yaxis() functions makes it incredibly easy to achieve.
Whether you’re visualizing temperatures, sales, or any other metric, this approach helps you present data in a more insightful and visually appealing way.
You may also like to read:
- Create Two Y Axes Bar Plot in Matplotlib
- Invert the Y-Axis in 3D Plot using Matplotlib
- Invert Secondary Y-Axis in Matplotlib using Python
- Matplotlib Two Y Axes: Plot with Same and Different Scales

I am Bijay Kumar, a Microsoft MVP in SharePoint. Apart from SharePoint, I started working on Python, Machine learning, and artificial intelligence for the last 5 years. During this time I got expertise in various Python libraries also like Tkinter, Pandas, NumPy, Turtle, Django, Matplotlib, Tensorflow, Scipy, Scikit-Learn, etc… for various clients in the United States, Canada, the United Kingdom, Australia, New Zealand, etc. Check out my profile.