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?
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()
Check the color parameter in plt.scatter.
The color='red' argument in plt.scatter draws red points on top of the gray image.
Given this code snippet overlaying points on an image, how many points will appear on the plot?
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()
Count the number of coordinates in points_x and points_y.
There are three x and y coordinates, so three points are plotted.
Which code snippet correctly overlays a heatmap with transparency on a grayscale image?
Heatmaps use imshow with a colormap and alpha for transparency.
Option D overlays the heatmap using imshow with alpha=0.5 for transparency on top of the grayscale image.
Examine this code snippet. What error will it raise when run?
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()
Check if points_x and points_y have the same length.
The x and y coordinate lists have different lengths, causing a ValueError in plt.scatter.
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?
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)
Stacking a (100,100) array with a (100,100,3) array along the third axis adds channels.
np.dstack stacks arrays depth-wise (along the third axis). Adding a single channel to a 3-channel image results in 4 channels.