- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
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
How to set same scale for subplots in Python using Matplotlib?
To set the same scale for subplot in Python using Matplotlib, we can take the following steps −
- Set the figure size and adjust the padding between and around the subplots.
- Create a new figure or activate an existing figure.
- Add an 'ax1' to the figure as part of a subplot arrangement with nrows=2, ncols=1 and index=1.
- Add another axis 'ax2' to the figure as part of a subplot arrangement with nrows=2, ncols=1 and index=2, with shared X-axis (to set same scale for subplots)
- Create "t" data points to plot sine and cosine curves on axes ax1 and ax2.
- To display the figure, use show() method.
Example
import matplotlib.pyplot as plt import numpy as np # Set the figure size plt.rcParams["figure.figsize"] = [7.00, 3.50] plt.rcParams["figure.autolayout"] = True # Plot the figure fig = plt.figure() # Add the axes ax1 = fig.add_subplot(2, 1, 1) ax2 = fig.add_subplot(2, 1, 2, sharex=ax1) # Create data points t = np.linspace(-5, 5, 100) # Plot sine and cosine curves on ax1 and ax2 ax1.plot(t, np.sin(2 * np.pi * t), color='red', lw=4) ax2.plot(t, np.cos(2 * np.pi * t), color='orange', lw=4) plt.show()
Output
It will produce the following output
- Related Articles
- Setting the same axis limits for all subplots in Matplotlib
- Set two Matplotlib imshow plots to have the same colormap scale
- How to make more than 10 subplots in a figure using Matplotlib?
- How to zoom subplots together in Matplotlib/Pyplot?
- Plotting grids across the subplots in Python Matplotlib
- Animation using Matplotlib with subplots and ArtistAnimation
- How to set same color for markers and lines in a Matplotlib plot loop?
- How to adjust the space between Matplotlib/Seaborn subplots for multi-plot layouts?
- Adjusting the heights of individual subplots in Matplotlib in Python
- How to share secondary Y-axis between subplots in Matplotlib?
- How to increase the spacing between subplots in Matplotlib with subplot2grid?
- Annotate Subplots in a Figure with A, B, C using Matplotlib
- How to combine several matplotlib axes subplots into one figure?
- How to decrease the density of tick labels in subplots in Matplotlib?
- How to set the xticklabels for date in matplotlib?

Advertisements