- 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
Remove white border when using subplot and imshow in Python Matplotlib
To remove white border when using subplot and imshow(), we can take the following steps −
- Set the figure size and adjust the padding between and around the subplots.
- Create random data points using numpy.
- Get the size of the data.
- Set the figure sizes in inches.
- Get the axes instance that contains most of the figure element.
- Turn off the axes.
- Add axes to the figure.
- Display the data as an image, i.e., on a 2D regular raster.
- To display the figure, use show() method.
Example
import numpy as np import matplotlib.pyplot as plt plt.rcParams["figure.figsize"] = [7.00, 3.50] plt.rcParams["figure.autolayout"] = True data = np.random.randint(0, 50, (50, 50)) sizes = np.shape(data) fig = plt.figure() fig.set_size_inches(1. * sizes[0] / sizes[1], 1, forward=False) ax = plt.Axes(fig, [0., 0., 1., 1.]) ax.set_axis_off() fig.add_axes(ax) ax.imshow(data) plt.show()
Output
It will produce the following output
- Related Articles
- Setting active subplot using axes object in Matplotlib
- Matplotlib legends in subplot
- How can a simple bivariate distribution be shown using ‘imshow’ in Matplotlib Python?
- Adjusting gridlines and ticks in Matplotlib imshow
- Remove or adapt the border of the frame of legend using matplotlib
- How to plot multiple Seaborn Jointplot in Subplot using Matplotlib?
- How to remove the first and last ticks label of each Y-axis subplot in Matplotlib?
- Remove border from IFrame using CSS
- Plotting an imshow() image in 3d in Matplotlib
- Rotating axis text for each subplot in Matplotlib
- How to add legend to imshow() in Matplotlib?
- Defining a discrete colormap for imshow in Matplotlib
- What is the difference between plt.show and cv2.imshow in Matplotlib?
- How can the ‘subplot’ function be used to create two graphs in Matplotlib Python?
- How to make longer subplot tick marks in Matplotlib?

Advertisements