- Trending Categories
- Data Structure
- Operating System
- MS Excel
- C Programming
- 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
How do I plot multiple X or Y axes in Matplotlib?
To plot multiple X or Y axis, we can use twinx() or twiny() methods, we can take the following Steps −
Using subplots() method, create a figure and a set of subplots.
Plot [1, 2, 3, 4, 5] data points on the left Y-axis scales.
Using twinx() method, create a twin of Axes with a shared X-axis but independent Y-axis, ax2.
Plot [11, 12, 31, 41, 15] data points on the right Y-axis scale, with blue color.
Using tight_layout() method, adjust the padding between and around the subplots.
To display the figure, use show() method.
import matplotlib.pyplot as plt plt.rcParams["figure.figsize"] = [7.00, 3.50] plt.rcParams["figure.autolayout"] = True fig, ax1 = plt.subplots() ax1.plot([1, 2, 3, 4, 5], color='red') ax2 = ax1.twinx() ax2.plot([11, 12, 31, 41, 15], color='blue') fig.tight_layout() plt.show()
- Related Articles
- How do I let my Matplotlib plot go beyond the axes?
- How to plot single data with two Y-axes (two units) in Matplotlib?
- How do I plot only a table in Matplotlib?
- Plot 3D bars without axes in Matplotlib
- Multiple axes in Matplotlib with different scales
- Show the origin axis (x,y) in Matplotlib plot
- How to plot a point on 3D axes in Matplotlib?
- Plot multiple boxplots in one graph in Pandas or Matplotlib
- How to plot a smooth 2D color plot for z = f(x, y) in Matplotlib?
- Change x axes scale in matplotlib
- How to plot multiple graphs in Matplotlib?
- How do I plot a step function with Matplotlib in Python?
- How do I plot Shapely polygons and objects using Matplotlib?
- How do I plot hatched bars using Pandas and Matplotlib?
- What is the difference between drawing plots using plot, axes or figure in matplotlib?