Bird
Raised Fist0
Matplotlibdata~20 mins

Overlaying data on images 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
🎖️
Overlay Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
What is the output of this code overlaying points on an image?

Consider the following Python code using matplotlib to overlay red points on a grayscale image. What will be the color of the points shown on the plot?

Matplotlib
import matplotlib.pyplot as plt
import numpy as np

image = np.ones((5,5)) * 0.5  # gray image
points_x = [1, 3]
points_y = [2, 4]

plt.imshow(image, cmap='gray')
plt.scatter(points_x, points_y, color='red')
plt.axis('off')
plt.show()
ARed points on a gray background image
BBlue points on a gray background image
CRed points on a white background image
DNo points visible, only gray image
Attempts:
2 left
💡 Hint

Check the color parameter in plt.scatter.

data_output
intermediate
1:30remaining
How many points are plotted on the image?

Given this code snippet overlaying points on an image, how many points will appear on the plot?

Matplotlib
import matplotlib.pyplot as plt
import numpy as np

image = np.zeros((10,10))
points_x = [2, 5, 7]
points_y = [3, 5, 8]

plt.imshow(image, cmap='gray')
plt.scatter(points_x, points_y, color='yellow')
plt.show()
A2 points
B3 points
C5 points
D0 points
Attempts:
2 left
💡 Hint

Count the number of coordinates in points_x and points_y.

visualization
advanced
2:30remaining
Which option produces a heatmap overlay on the image?

Which code snippet correctly overlays a heatmap with transparency on a grayscale image?

A
plt.imshow(image, cmap='gray')
plt.scatter(data, color='hot', alpha=0.5)
plt.show()
B
plt.imshow(image, cmap='gray')
plt.scatter(data, cmap='hot', alpha=0.5)
plt.show()
C
plt.imshow(image, cmap='gray')
plt.imshow(data, cmap='hot')
plt.scatter(alpha=0.5)
plt.show()
D
plt.imshow(image, cmap='gray')
plt.imshow(data, cmap='hot', alpha=0.5)
plt.show()
Attempts:
2 left
💡 Hint

Heatmaps use imshow with a colormap and alpha for transparency.

🔧 Debug
advanced
2:00remaining
What error does this code raise when overlaying points on an image?

Examine this code snippet. What error will it raise when run?

Matplotlib
import matplotlib.pyplot as plt
import numpy as np

image = np.ones((5,5))
points_x = [1, 2, 3]
points_y = [1, 2]

plt.imshow(image, cmap='gray')
plt.scatter(points_x, points_y, color='blue')
plt.show()
AIndexError: list index out of range
BTypeError: color must be a string or sequence
CValueError: x and y must be the same size
DNo error, plot displays correctly
Attempts:
2 left
💡 Hint

Check if points_x and points_y have the same length.

🚀 Application
expert
3:00remaining
What is the resulting shape of the overlayed image array after combining two images?

You have two numpy arrays representing images: img1 with shape (100, 100) and img2 with shape (100, 100, 3). You want to overlay img2 as a color layer on top of img1 grayscale image by stacking channels. What will be the shape of the resulting combined image array?

Matplotlib
import numpy as np

img1 = np.random.rand(100, 100)  # grayscale
img2 = np.random.rand(100, 100, 3)  # color

combined = np.dstack((img1, img2))
print(combined.shape)
A(100, 100, 4)
B(100, 100)
C(100, 100, 3)
D(100, 100, 2)
Attempts:
2 left
💡 Hint

Stacking a (100,100) array with a (100,100,3) array along the third axis adds channels.

Practice

(1/5)
1. What is the main purpose of using plt.imshow() in matplotlib when overlaying data on images?
easy
A. To save the plot as an image file
B. To display an image as the background for plotting data on top
C. To create a scatter plot of data points
D. To clear the current figure before plotting

Solution

  1. Step 1: Understand the role of plt.imshow()

    This function is used to display images in matplotlib, which can serve as a background for other plots.
  2. Step 2: Identify its use in overlaying data

    By showing an image first, you can then plot data points or lines on top to combine visual and numeric information.
  3. Final Answer:

    To display an image as the background for plotting data on top -> Option B
  4. Quick Check:

    plt.imshow() shows images [OK]
Hint: Remember: imshow shows images, not plots [OK]
Common Mistakes:
  • Confusing imshow with scatter plot functions
  • Thinking imshow saves images
  • Using imshow to clear figures
2. Which of the following is the correct way to overlay a red scatter plot on an image using matplotlib?
easy
A.
plt.scatter(x, y)
plt.show()
plt.imshow(image)
B.
plt.scatter(x, y, color='red')
plt.imshow(image)
plt.show()
C.
plt.imshow(image)
plt.scatter(x, y, color='red')
plt.show()
D.
plt.imshow(image, color='red')
plt.scatter(x, y)
plt.show()

Solution

  1. Step 1: Order of plotting matters

    The image must be shown first with plt.imshow() so that scatter points appear on top.
  2. Step 2: Correct syntax for scatter color

    Use color='red' inside plt.scatter() to make points red.
  3. Final Answer:

    plt.imshow(image) then plt.scatter(x, y, color='red') -> Option C
  4. Quick Check:

    Image first, then scatter with color [OK]
Hint: Show image before scatter to overlay correctly [OK]
Common Mistakes:
  • Plotting scatter before image hides points
  • Passing color to imshow instead of scatter
  • Calling plt.show() too early
3. What will be the output of the following code?
import matplotlib.pyplot as plt
import numpy as np

image = np.zeros((5,5))
x = [1, 3]
y = [2, 4]

plt.imshow(image, cmap='gray')
plt.scatter(x, y, color='blue')
plt.show()
medium
A. A white 5x5 image with two blue points at coordinates (1,2) and (3,4)
B. An error because x and y coordinates are swapped
C. A black 5x5 image with two red points at coordinates (2,1) and (4,3)
D. A black 5x5 image with two blue points at coordinates (1,2) and (3,4)

Solution

  1. Step 1: Understand the image array

    The image is a 5x5 array of zeros, so it appears black with cmap='gray'.
  2. Step 2: Plot scatter points

    Points at (x=1, y=2) and (x=3, y=4) are plotted in blue on top of the image.
  3. Final Answer:

    A black 5x5 image with two blue points at coordinates (1,2) and (3,4) -> Option D
  4. Quick Check:

    Zeros = black image, scatter color blue [OK]
Hint: Remember: imshow shows array as image, scatter uses x,y coords [OK]
Common Mistakes:
  • Confusing x and y coordinates
  • Assuming zeros array is white
  • Mixing up scatter point colors
4. The following code is intended to overlay a green line on an image, but the line does not appear. What is the error?
import matplotlib.pyplot as plt
import numpy as np

image = np.ones((10,10))
plt.imshow(image)
plt.plot([1, 8], [1, 8], color='green')
plt.show()
medium
A. The image is white and the green line is not visible due to default alpha
B. The plot command should be called before imshow
C. The color argument should be 'c' instead of 'color'
D. The coordinates for the line are outside the image bounds

Solution

  1. Step 1: Analyze the image color

    The image is an array of ones, which appears white by default.
  2. Step 2: Check line visibility

    A green line on a white background may be hard to see if the line is thin and no linewidth is set.
  3. Final Answer:

    The image is white and the green line is not visible due to default alpha -> Option A
  4. Quick Check:

    White background hides thin green line [OK]
Hint: Check background and line colors for visibility [OK]
Common Mistakes:
  • Plotting line before image hides image
  • Using wrong color argument name
  • Assuming coordinates are out of bounds
5. You want to overlay a heatmap of data values on top of a grayscale image using matplotlib. Which approach correctly combines the image and heatmap with transparency so both are visible?
hard
A.
plt.imshow(image, cmap='gray')
plt.imshow(data, cmap='hot', alpha=0.5)
plt.show()
B.
plt.imshow(data, cmap='hot')
plt.imshow(image, cmap='gray', alpha=0.5)
plt.show()
C.
plt.imshow(image, cmap='gray', alpha=0.5)
plt.imshow(data, cmap='hot')
plt.show()
D.
plt.imshow(image, cmap='hot')
plt.imshow(data, cmap='gray', alpha=0.5)
plt.show()

Solution

  1. Step 1: Display base grayscale image first

    Use plt.imshow(image, cmap='gray') to show the background image.
  2. Step 2: Overlay heatmap with transparency

    Plot data with cmap='hot' and alpha=0.5 to make it semi-transparent over the image.
  3. Final Answer:

    Show grayscale image first, then heatmap with alpha=0.5 -> Option A
  4. Quick Check:

    Base image first, overlay with alpha [OK]
Hint: Show base image first, overlay heatmap with alpha transparency [OK]
Common Mistakes:
  • Plotting heatmap before image hides heatmap
  • Not using alpha causes full coverage
  • Swapping colormaps between image and data