Bird
Raised Fist0
Matplotlibdata~5 mins

Image extent and aspect ratio in Matplotlib

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
Introduction

We use image extent and aspect ratio to control how an image fits inside a plot. This helps show the image clearly without stretching or squishing it.

When you want to place an image inside a graph with specific coordinates.
When you want to keep the image's original shape without distortion.
When you want to compare images or data side by side with correct scaling.
When you want to zoom or crop a part of an image in a plot.
When you want to make sure the image fits well with other plot elements.
Syntax
Matplotlib
plt.imshow(image, extent=[xmin, xmax, ymin, ymax], aspect='auto'|'equal'|number)

extent sets the bounding box of the image in data coordinates.

aspect controls the shape ratio: 'auto' stretches, 'equal' keeps original ratio, or a number sets height/width ratio.

Examples
Image stretched to fit the box from x=0 to 10 and y=0 to 5.
Matplotlib
plt.imshow(img, extent=[0, 10, 0, 5], aspect='auto')
Image keeps its original shape inside the box, no stretching.
Matplotlib
plt.imshow(img, extent=[0, 10, 0, 5], aspect='equal')
Image height is half the width, controlled by aspect ratio number.
Matplotlib
plt.imshow(img, extent=[-1, 1, -1, 1], aspect=0.5)
Sample Program

This code creates a simple 5x5 gradient image. It shows the image twice: once stretched to fit the extent box, and once keeping the original shape. You can see how aspect changes the image look.

Matplotlib
import matplotlib.pyplot as plt
import numpy as np

# Create a simple 5x5 image with gradient
img = np.arange(25).reshape(5,5)

plt.figure(figsize=(8,4))

# Show image with extent and auto aspect
plt.subplot(1,2,1)
plt.title('aspect="auto"')
plt.imshow(img, extent=[0, 10, 0, 5], aspect='auto')
plt.xlabel('X axis')
plt.ylabel('Y axis')

# Show image with extent and equal aspect
plt.subplot(1,2,2)
plt.title('aspect="equal"')
plt.imshow(img, extent=[0, 10, 0, 5], aspect='equal')
plt.xlabel('X axis')
plt.ylabel('Y axis')

plt.tight_layout()
plt.show()
OutputSuccess
Important Notes

If you don't set extent, the image uses pixel coordinates by default.

Setting aspect='equal' is useful to avoid distortion in images like maps or photos.

You can use numbers for aspect to fine-tune the height-to-width ratio.

Summary

Image extent sets where the image appears on the plot in data units.

Aspect ratio controls if the image is stretched or keeps its shape.

Use these to make your images look right and fit well with other plot parts.

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