Bird
Raised Fist0
Matplotlibdata~20 mins

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

Choose your learning style10 modes available

Start learning this pattern below

Jump into concepts and practice - no test required

or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
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.

Practice

(1/5)
1. What does the extent parameter control when displaying an image with matplotlib.pyplot.imshow()?
easy
A. The color map used for the image
B. The position and size of the image on the plot axes
C. The resolution of the image
D. The file format of the image

Solution

  1. Step 1: Understand the role of extent

    The extent parameter defines the bounding box in data coordinates that the image will fill on the axes.
  2. Step 2: Compare with other options

    Color map, resolution, and file format are unrelated to extent. They control different aspects of image display or file handling.
  3. Final Answer:

    The position and size of the image on the plot axes -> Option B
  4. Quick Check:

    Extent = position and size [OK]
Hint: Extent sets image box on axes, not colors or file type [OK]
Common Mistakes:
  • Confusing extent with color map
  • Thinking extent changes image resolution
  • Assuming extent controls file format
2. Which of the following is the correct way to keep the image aspect ratio fixed when using imshow()?
easy
A. plt.imshow(img, aspect='equal')
B. plt.imshow(img, cmap='gray')
C. plt.imshow(img, extent=[0,1,0,1])
D. plt.imshow(img, aspect='auto')

Solution

  1. Step 1: Identify aspect ratio options

    The aspect parameter controls image stretching. 'equal' keeps the aspect ratio fixed.
  2. Step 2: Check other options

    'auto' allows stretching, extent sets position, and cmap sets colors, not aspect ratio.
  3. Final Answer:

    plt.imshow(img, aspect='equal') -> Option A
  4. Quick Check:

    Aspect='equal' fixes ratio [OK]
Hint: Use aspect='equal' to keep image shape correct [OK]
Common Mistakes:
  • Using aspect='auto' which stretches image
  • Confusing extent with aspect ratio
  • Setting cmap instead of aspect
3. What will be the effect of this code snippet?
import matplotlib.pyplot as plt
import numpy as np
img = np.ones((10, 20))
plt.imshow(img, extent=[0, 5, 0, 10], aspect='auto')
plt.show()
medium
A. Image will be shown with default extent and fixed aspect ratio
B. Image will keep original shape and size ignoring extent
C. Code will raise an error due to wrong extent format
D. Image will stretch to fill x from 0 to 5 and y from 0 to 10, possibly distorted

Solution

  1. Step 1: Analyze extent parameter

    The extent=[0,5,0,10] sets the image to cover x-axis 0 to 5 and y-axis 0 to 10 on the plot.
  2. Step 2: Analyze aspect='auto'

    Aspect='auto' allows the image to stretch to fill the extent box, so the image shape may distort.
  3. Final Answer:

    Image will stretch to fill x from 0 to 5 and y from 0 to 10, possibly distorted -> Option D
  4. Quick Check:

    Extent sets size, aspect='auto' allows stretch [OK]
Hint: Extent sets size; aspect='auto' allows distortion [OK]
Common Mistakes:
  • Assuming extent is ignored
  • Expecting fixed aspect ratio with aspect='auto'
  • Thinking code raises error
4. Identify the error in this code that tries to display an image with fixed aspect ratio:
import matplotlib.pyplot as plt
import numpy as np
img = np.random.rand(5,5)
plt.imshow(img, extent=[0,5,0], aspect='equal')
plt.show()
medium
A. The aspect='equal' is invalid and causes error
B. The image array shape is incompatible with imshow
C. The extent list has incorrect length; it should have 4 values
D. Missing plt.axis('equal') to fix aspect ratio

Solution

  1. Step 1: Check extent parameter format

    Extent must be a list of 4 numbers: [xmin, xmax, ymin, ymax]. Here it has only 3 values, causing an error.
  2. Step 2: Verify other parameters

    Aspect='equal' is valid. Image shape is fine. plt.axis('equal') is optional when aspect is set.
  3. Final Answer:

    The extent list has incorrect length; it should have 4 values -> Option C
  4. Quick Check:

    Extent needs 4 numbers [OK]
Hint: Extent must have 4 numbers: xmin, xmax, ymin, ymax [OK]
Common Mistakes:
  • Using extent with less than 4 values
  • Confusing aspect parameter validity
  • Thinking plt.axis('equal') is required
5. You want to overlay a heatmap image on a scatter plot with x values from 0 to 10 and y values from 0 to 5. Which extent and aspect settings correctly align the image without distortion?
hard
A. extent=[0,10,0,5], aspect='equal'
B. extent=[0,5,0,10], aspect='auto'
C. extent=[0,10,0,5], aspect='auto'
D. extent=[0,5,0,10], aspect='equal'

Solution

  1. Step 1: Match extent to data range

    The scatter plot x ranges 0-10 and y ranges 0-5, so extent must be [0,10,0,5] to align image correctly.
  2. Step 2: Choose aspect to avoid distortion

    Aspect='equal' keeps the image shape correct, preventing distortion when overlaying.
  3. Final Answer:

    extent=[0,10,0,5], aspect='equal' -> Option A
  4. Quick Check:

    Extent matches data, aspect='equal' fixes shape [OK]
Hint: Match extent to data limits and use aspect='equal' [OK]
Common Mistakes:
  • Swapping x and y in extent
  • Using aspect='auto' causing distortion
  • Ignoring data range when setting extent