Consider this Python code using matplotlib to display an image:
import matplotlib.pyplot as plt import numpy as np image = np.array([[0, 1], [2, 3]]) plt.imshow(image, cmap='gray') plt.colorbar() plt.show()
What will the displayed image look like?
import matplotlib.pyplot as plt import numpy as np image = np.array([[0, 1], [2, 3]]) plt.imshow(image, cmap='gray') plt.colorbar() plt.show()
Think about how imshow maps numbers to colors using the grayscale colormap.
The imshow function shows the 2x2 array as a grayscale image. The smallest value (0) is black, the largest (3) is white, and values in between are shades of gray. So top-left is darkest, bottom-right is brightest.
Given this code snippet:
import matplotlib.pyplot as plt import numpy as np image = np.random.rand(100, 150, 3) plt.imshow(image) plt.show()
What is the shape of the image array being displayed?
import matplotlib.pyplot as plt import numpy as np image = np.random.rand(100, 150, 3) plt.imshow(image) plt.show()
Remember the shape format for color images is (height, width, color_channels).
The image array has 100 rows (height), 150 columns (width), and 3 color channels (RGB), so its shape is (100, 150, 3).
Which code snippet correctly displays a 10x10 heatmap with a colorbar using imshow?
Check how colorbar() is called and how colormaps are assigned.
Option D correctly sets the colormap in imshow and calls plt.colorbar() without arguments to add the colorbar. Other options misuse colorbar arguments causing errors or ignoring the colormap.
Analyze this code snippet:
import matplotlib.pyplot as plt import numpy as np image = np.array([1, 2, 3, 4]) plt.imshow(image) plt.show()
What error will occur when running this?
import matplotlib.pyplot as plt import numpy as np image = np.array([1, 2, 3, 4]) plt.imshow(image) plt.show()
Check the shape requirements for imshow input arrays.
imshow requires a 2D array (grayscale) or 3D array (color). A 1D array like [1,2,3,4] is invalid and raises a ValueError about shape.
You have an RGB image stored as a NumPy array img with shape (100, 100, 3). You want to display it with 50% transparency over a white background using imshow. Which code snippet achieves this?
Check how to set transparency and background color in matplotlib.
Option A correctly sets the alpha parameter for transparency and sets the axes background color to white. Other options use invalid parameters or functions.