Bird
Raised Fist0
Matplotlibdata~10 mins

Image extent and aspect ratio in Matplotlib - Interactive Code Practice

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
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to display an image using matplotlib's imshow function.

Matplotlib
import matplotlib.pyplot as plt
import numpy as np

image = np.random.rand(5,5)
plt.imshow(image, extent=[1])
plt.show()
Drag options to blanks, or click blank then click option'
A[1, 5, 1, 5]
B[0, 5, 0, 5]
C[0, 1, 0, 1]
D[0, 10, 0, 10]
Attempts:
3 left
💡 Hint
Common Mistakes
Using extent values that don't match the image size, causing distortion.
Forgetting that extent expects [xmin, xmax, ymin, ymax].
2fill in blank
medium

Complete the code to set the aspect ratio of the image to 'equal' so pixels are square.

Matplotlib
import matplotlib.pyplot as plt
import numpy as np

image = np.random.rand(5,10)
plt.imshow(image, extent=[0, 10, 0, 5])
plt.gca().set_aspect([1])
plt.show()
Drag options to blanks, or click blank then click option'
A'equal'
B'auto'
C'square'
D'free'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'auto' which lets matplotlib stretch the image to fill the axes.
Using invalid aspect values like 'square' or 'free'.
3fill in blank
hard

Fix the error in the code to correctly display the image with the specified extent and aspect ratio.

Matplotlib
import matplotlib.pyplot as plt
import numpy as np

image = np.random.rand(3,6)
plt.imshow(image, extent=[0, 6, 0, 3])
plt.gca().set_aspect([1])
plt.show()
Drag options to blanks, or click blank then click option'
A'none'
B'auto'
C'equal'
D1.0
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'none' which is not a valid aspect value.
Using 'auto' which stretches the image.
4fill in blank
hard

Fill both blanks to create a dictionary comprehension that maps words to their lengths only if the length is greater than 3.

Matplotlib
words = ['data', 'science', 'ai', 'ml', 'python']
lengths = {word: [1] for word in words if [2] }
Drag options to blanks, or click blank then click option'
Alen(word)
Blen(word) > 3
Cword
Dlen(word) < 3
Attempts:
3 left
💡 Hint
Common Mistakes
Using the word itself as the value instead of its length.
Using the wrong condition like length less than 3.
5fill in blank
hard

Fill all three blanks to create a dictionary comprehension that maps uppercase words to their lengths only if length is greater than 2.

Matplotlib
words = ['cat', 'dog', 'a', 'elephant']
[1] = { [2]: [3] for word in words if len(word) > 2 }
print([1])
Drag options to blanks, or click blank then click option'
Alengths
Bword.upper()
Clen(word)
Dwords
Attempts:
3 left
💡 Hint
Common Mistakes
Using the wrong variable name or missing the assignment.
Using the word itself instead of uppercase for keys.
Using the word instead of length for values.

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