0
0
Matplotlibdata~10 mins

Image extent and aspect ratio in Matplotlib - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Image extent and aspect ratio
Create image data
Call imshow() with extent
Set aspect ratio
Render image on plot
Display axes with adjusted limits and ratio
This flow shows how image data is created, displayed with a specified extent, and how the aspect ratio controls the shape of the image on the plot.
Execution Sample
Matplotlib
import matplotlib.pyplot as plt
import numpy as np
img = np.arange(16).reshape(4,4)
plt.imshow(img, extent=[0,4,0,2], aspect='auto')
plt.show()
This code creates a 4x4 image and displays it stretched to fit the extent [0,4] in x and [0,2] in y with automatic aspect ratio.
Execution Table
StepActionParameter/ValueEffect on PlotResult
1Create image data4x4 array with values 0 to 15Image data readyImage array created
2Call imshow()img, extent=[0,4,0,2], aspect='auto'Image placed with x from 0 to 4, y from 0 to 2Image stretched to fit extent
3Set aspect ratio'auto'Image pixels scaled to fill extent areaImage shape distorted to fit axes limits
4Render imageN/AImage displayed on plotImage visible with stretched shape
5Show plotplt.show()Plot window opensUser sees image with specified extent and aspect
💡 Execution stops after plot window is displayed and user closes it.
Variable Tracker
VariableStartAfter Step 1After Step 2After Step 3Final
imgundefined4x4 numpy array with values 0-15Same array passed to imshowSame array displayed with extentDisplayed image data unchanged
extentundefinedundefined[0,4,0,2]Used to set axes limitsAxes limits set to extent
aspectundefinedundefined'auto'Controls pixel scalingAspect ratio applied
Key Moments - 2 Insights
Why does the image look stretched when using extent with aspect='auto'?
Because aspect='auto' scales the image to fill the axes limits set by extent, the pixels are not forced to be square, causing stretching as shown in execution_table step 3.
What does the extent parameter control in imshow?
Extent sets the data limits for the image axes (x and y ranges), changing where the image is placed and how large it appears, as seen in execution_table step 2.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the extent value used in step 2?
A[0,4,0,2]
B[0,2,0,4]
C[0,1,0,1]
D[1,4,1,2]
💡 Hint
Check the 'Parameter/Value' column in step 2 of the execution_table.
At which step does the image get stretched to fit the axes limits?
AStep 1
BStep 3
CStep 4
DStep 5
💡 Hint
Look at the 'Effect on Plot' column describing aspect ratio application.
If aspect='equal' was used instead of 'auto', how would the image shape change?
AImage would be stretched more
BImage would disappear
CImage pixels would be square, preserving shape
DExtent would be ignored
💡 Hint
Aspect ratio controls pixel scaling; 'equal' keeps pixels square.
Concept Snapshot
imshow(image, extent=[xmin,xmax,ymin,ymax], aspect='auto'|'equal')
- extent sets image axes limits
- aspect='auto' stretches image to fill extent
- aspect='equal' keeps pixel shape square
- Changing extent changes image size on plot
- Aspect ratio controls image distortion
Full Transcript
This lesson shows how matplotlib's imshow function uses the extent parameter to set the image's position and size on the plot axes. The aspect parameter controls whether the image pixels keep their shape or stretch to fill the axes. We create a 4x4 image array, display it with extent [0,4,0,2], and aspect='auto', which stretches the image to fill the rectangular area. The execution table traces each step from data creation to rendering. Key moments clarify why the image stretches and what extent does. The quiz tests understanding of extent values, when stretching happens, and the effect of aspect ratio settings.