0
0
Matplotlibdata~20 mins

Image extent and aspect ratio in Matplotlib - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Image Extent and Aspect Ratio Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of image with extent and aspect set to 'auto'
What will be the shape of the displayed image when using extent=[0, 4, 0, 2] and aspect='auto' in matplotlib's imshow?
Matplotlib
import matplotlib.pyplot as plt
import numpy as np

img = np.array([[1, 2], [3, 4]])
plt.imshow(img, extent=[0, 4, 0, 2], aspect='auto')
plt.axis('on')
plt.show()
AThe image will stretch to fill the rectangle from x=0 to 4 and y=0 to 2, ignoring the original pixel aspect ratio.
BThe image will keep square pixels and fit within the extent, possibly leaving empty space.
CThe image will not display because aspect='auto' is invalid.
DThe image will be distorted to a square shape regardless of extent values.
Attempts:
2 left
💡 Hint
Think about how 'aspect=auto' affects image scaling inside the given extent.
Predict Output
intermediate
2:00remaining
Effect of aspect='equal' with extent on image display
What happens when you set aspect='equal' and extent=[0, 4, 0, 2] in matplotlib's imshow for a 2x2 image?
Matplotlib
import matplotlib.pyplot as plt
import numpy as np

img = np.array([[1, 2], [3, 4]])
plt.imshow(img, extent=[0, 4, 0, 2], aspect='equal')
plt.axis('on')
plt.show()
AThe image pixels will be square, so the image will not fill the entire extent rectangle, leaving extra space.
BThe image will stretch to fill the extent rectangle, distorting pixel shapes.
CThe image will be clipped and only part of it will show.
DThe image will flip vertically due to aspect='equal'.
Attempts:
2 left
💡 Hint
Consider what 'aspect=equal' means for pixel shape and how it interacts with extent size.
visualization
advanced
3:00remaining
Visualize difference between aspect='auto' and aspect='equal'
Which option shows the correct matplotlib code to plot the same 3x2 image twice side by side, left with aspect='auto' and right with aspect='equal', both using extent=[0,6,0,4]?
Matplotlib
import matplotlib.pyplot as plt
import numpy as np

img = np.arange(6).reshape(3, 2)
fig, axs = plt.subplots(1, 2, figsize=(10, 5))

# Left plot
axs[0].imshow(img, extent=[0, 6, 0, 4], aspect='auto')
axs[0].set_title('aspect=auto')

# Right plot
axs[1].imshow(img, extent=[0, 6, 0, 4], aspect='equal')
axs[1].set_title('aspect=equal')

plt.show()
ABoth images keep pixel shape, but the left image is clipped.
BBoth images stretch to fill the 6x4 area, ignoring pixel shape.
CThe left image stretches to fill the 6x4 area; the right image keeps pixel shape, leaving space.
DThe left image is flipped horizontally; the right image is flipped vertically.
Attempts:
2 left
💡 Hint
Recall how aspect='auto' and aspect='equal' affect image scaling.
🔧 Debug
advanced
2:00remaining
Identify the error in image extent usage
What error occurs when running this code snippet? import matplotlib.pyplot as plt import numpy as np img = np.ones((3,3)) plt.imshow(img, extent=[0, 2, 3]) plt.show()
Matplotlib
import matplotlib.pyplot as plt
import numpy as np
img = np.ones((3,3))
plt.imshow(img, extent=[0, 2, 3])
plt.show()
ATypeError: 'extent' must be a tuple, not list
BValueError: 'extent' must be a sequence of length 4
CNo error, image displays normally
DIndexError: list index out of range
Attempts:
2 left
💡 Hint
Check the length of the extent parameter.
🚀 Application
expert
3:00remaining
Calculate displayed image width in data units with aspect='equal'
Given a 4x2 pixel image displayed with extent=[0, 8, 0, 4] and aspect='equal', what is the width in data units of one pixel on the x-axis?
A0.5 units
B1 unit
C4 units
D2 units
Attempts:
2 left
💡 Hint
With aspect='equal', pixels are square. Use extent and image shape to find pixel size.