- 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 the margins of a Matplotlib figure?
To set the margins of a matplotlib figure, we can use margins() method.
Steps
- Set the figure size and adjust the padding between and around the subplots.
- Create t and y data points using numpy.
- Add a subplot to the current figure at index 1.
- Plot t and y data points using plot() method.
- Set the title of the plot.
- Add a subplot to the current figure at index 2.
- Plot t and y data points using plot() method.
- Set the title of the plot.
- Set margins of the plot using margins(x=0, y=0).
- To display the figure, use show() method.
Example
import numpy as np from matplotlib import pyplot as plt plt.rcParams["figure.figsize"] = [7.50, 3.50] plt.rcParams["figure.autolayout"] = True t = np.linspace(-2, 2, 10) y = np.exp(-t) plt.subplot(121) plt.plot(t, y) plt.title("Without margins") plt.subplot(122) plt.plot(t, y) plt.title("With x=0 and y=0 margins") plt.margins(x=0, y=0) plt.show()
Output
- Related Articles
- How to set the current figure in Matplotlib?
- How to set the margins of an element with JavaScript?
- How to set the matplotlib figure default size in ipython notebook?
- How to set margins in an Android LinearLayout programmatically?
- How can I set the background color on specific areas of a Pyplot figure using Matplotlib?
- How to set local rcParams or rcParams for one figure in matplotlib?
- What is the preferred way to set Matplotlib figure/axes properties?
- How to set margins in an Android LinearLayout programmatically using Kotlin?
- How to draw axis in the middle of a figure in Matplotlib?
- How to put the title at the bottom of a figure in Matplotlib?
- How to set "step" on axis X in my figure in Matplotlib Python 2.6.6?
- How to add a 3d subplot to a matplotlib figure?
- How to retrieve XY data from a Matplotlib figure?
- How to position and align a Matplotlib figure legend?
- How to set the background color of a column in a matplotlib table?

Advertisements