- Trending Categories
- Data Structure
- Operating System
- C Programming
- 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 change the scale of imshow in matplotlib without stretching the image?
To change the scale of imshow in matplotlib without stretching the image, we can take the following steps.
Set the figure size and adjust the padding between and around the subplots.
Create random data points with 4×4 dimension.
Display the data as an image, i.e., on a 2D regular raster.
Use the extent parameter of imshow to map the image buffer pixel coordinates to a data space coordinate system.
Next, set the aspect ratio of the image manually by supplying a value such as "aspect=4" or let it auto-scale by using aspect='auto'. This will prevent stretching of the image. By default, imshow sets the aspect of the plot to 1.
To display the figure, use Show() method.
import numpy as np from matplotlib import pyplot as plt plt.rcParams["figure.figsize"] = [7.00, 3.50] plt.rcParams["figure.autolayout"] = True data = np.random.rand(4, 4) plt.imshow(data, origin='lower', extent=[-4, 4, -1, 1], aspect=4) plt.show()
It will produce the following output −
- Set two Matplotlib imshow plots to have the same colormap scale
- How to change the scale of an existing table in Matplotlib?
- How do I change the font size of the scale in Matplotlib plots?
- Change x axes scale in matplotlib
- Plotting an imshow() image in 3d in Matplotlib
- Change values on matplotlib imshow() graph axis
- How to label and change the scale of a Seaborn kdeplot's axes? (Matplotlib)
- How to alter the brightness of a grey scale image?
- How to exponentially scale the Y axis with matplotlib?
- How to apply a mask on the matrix in Matplotlib imshow?
- How to add legend to imshow() in Matplotlib?
- How to change the strength of antialiasing in Matplotlib?
- How to plot an image with non-linear Y-axis with Matplotlib using imshow?
- How to show two different colored colormaps in the same imshow Matplotlib?
- How to change the color of the ticks in the colorbar in Matplotlib?