The Wayback Machine - https://web.archive.org/web/20250110221539/https://www.geeksforgeeks.org/use-different-y-axes-on-the-left-and-right-of-a-matplotlib-plot/
Open In App

Use different y-axes on the left and right of a Matplotlib plot

Last Updated : 23 Jan, 2022
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow

In this article, we are going to discuss how to create y-axes of both sides of a Matplotlib plot.

Sometimes for quick data analysis, it is required to create a single graph having two data variables with different scales. For this purpose twin axes methods are used i.e. dual X or Y-axes. The matplotlib.axes.Axes.twinx() function in axes module of matplotlib library is used to create a twin Axes sharing the X-axis.

Syntax :

matplotlib.axes.Axes.twinx(self)

This method does not take any parameters, raise an error if provided. It returns the ax_twin object which indicates that a new Axes instance is created. Below examples illustrate the matplotlib.axes.Axes.twinx() function in matplotlib.axes:

Example 1:

Python3




# import libraries
import numpy as np
import matplotlib.pyplot as plt
 
# Creating dataset
x = np.arange(1.0, 100.0, 0.191)
dataset_1 = np.exp(x**0.25) - np.exp(x**0.5)
dataset_2 = np.sin(0.4 * np.pi * x**0.5) + np.cos(0.8 * np.pi * x**0.25)
 
# Creating plot with dataset_1
fig, ax1 = plt.subplots()
 
color = 'tab:red'
ax1.set_xlabel('X-axis')
ax1.set_ylabel('Y1-axis', color = color)
ax1.plot(x, dataset_1, color = color)
ax1.tick_params(axis ='y', labelcolor = color)
 
# Adding Twin Axes to plot using dataset_2
ax2 = ax1.twinx()
 
color = 'tab:green'
ax2.set_ylabel('Y2-axis', color = color)
ax2.plot(x, dataset_2, color = color)
ax2.tick_params(axis ='y', labelcolor = color)
 
# Adding title
plt.title('Use different y-axes on the left and right of a Matplotlib plot', fontweight ="bold")
 
# Show plot
plt.show()


Output:

Example 2:

Python3




# import libraries
import numpy as np
import matplotlib.pyplot as plt
from matplotlib import rc
rc('mathtext', default='regular')
 
# Creating dataset
x = np.arange(10)
dataset_1 = np.random.random(10)*30
dataset_2 = np.random.random(10)*60
dataset_3 = np.random.random(10)*100
 
# Creating figure
fig = plt.figure()
 
# Plotting dataset_2
ax = fig.add_subplot(111)
ax.plot(x, dataset_2, '-', label='dataset_2')
ax.plot(x, dataset_3, '-', label='dataset_3')
 
# Creating Twin axes for dataset_1
ax2 = ax.twinx()
ax2.plot(x, dataset_1, '-r', label='dataset_1')
 
# Adding title
plt.title('Use different y-axes on the left and right of a Matplotlib plot',
          fontweight="bold")
 
# Adding legend
ax.legend(loc=0)
ax2.legend(loc=0)
 
# adding grid
ax.grid()
 
# Adding labels
ax.set_xlabel("X-axis")
ax.set_ylabel(r"Y1-axis")
ax2.set_ylabel(r"Y2-axis")
 
# Setting Y limits
ax2.set_ylim(0, 35)
ax.set_ylim(-20, 100)
 
# Show plot
plt.show()


Output:


Enhance your coding skills with DSA Python, a comprehensive course focused on Data Structures and Algorithms using Python. Over 90 days, you'll explore essential algorithms, learn how to solve complex problems, and sharpen your Python programming skills. This course is perfect for anyone looking to level up their coding abilities and get ready for top tech interviews.
Join the Three 90 Challenge and commit to completing 90% of the course in 90 days to earn a 90% refund. It’s the perfect way to stay focused, track your progress, and reap the rewards of your hard work. Take the challenge and become a DSA expert in Python today


Similar Reads

three90RightbarBannerImg