- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Multiple axes in Matplotlib with different scales
In the following code, we will see how to create a shared Y-axis.
Steps
Create fig and ax variables using subplots method, where default nrows and ncols are 1.
Plot line with lists passed in the argument of plot() method with color="red".
Create a twin of Axes with a shared X-axis but independent Y-axis.
Plot the line on ax2 that is created in step 3.
Adjust the padding between and around subplots.
To show the figure use plt.show() method.
Example
import matplotlib.pyplot as plt fig, ax1 = plt.subplots() ax1.plot([1, 2, 3, 4, 5], [3, 5, 7, 1, 9], color='red') ax2 = ax1.twinx() ax2.plot([11, 12, 31, 41, 15], [13, 51, 17, 11, 76], color='blue') fig.tight_layout() plt.show()
Output
- Related Articles
- How to plot with different scales in Matplotlib?
- Different X and Y scales in zoomed inset in Matplotlib
- Drawing multiple legends on the same axes in Matplotlib
- How do I plot multiple X or Y axes in Matplotlib?
- Change x axes scale in matplotlib
- How to switch axes in Matplotlib?
- Plot 3D bars without axes in Matplotlib
- Rotating axes label text in 3D Matplotlib
- How to make axes transparent in Matplotlib?
- Matplotlib histogram with multiple legend entries
- How to plot single data with two Y-axes (two units) in Matplotlib?
- How to hide axes but keep axis-labels in 3D Plot with Matplotlib?
- Drawing multiple figures in parallel in Python with Matplotlib
- Setting active subplot using axes object in Matplotlib
- How to change axes background color in Matplotlib?

Advertisements