0
0
Matplotlibdata~20 mins

Overlaying data on images in Matplotlib - Practice Problems & Coding Challenges

Choose your learning style9 modes available
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.