Bird
0
0

What will be the output of this code?

medium📝 Predict Output Q4 of 15
Matplotlib - Image Display
What will be the output of this code?
import matplotlib.pyplot as plt
import numpy as np
img = np.array([[5, 10], [15, 20]])
plt.imshow(img, cmap='hot')
plt.colorbar()
plt.show()
AA 2x2 heatmap image with colors representing values from 5 to 20 and a colorbar
BA grayscale image with pixel values 5, 10, 15, 20
CAn error because 'hot' is not a valid colormap
DA blank plot with no image displayed
Step-by-Step Solution
Solution:
  1. Step 1: Understand the array and colormap

    The array contains values from 5 to 20. The 'hot' colormap maps these values to colors from black to red to yellow.
  2. Step 2: Effect of plt.colorbar()

    This adds a color scale legend showing the mapping of values to colors.
  3. Final Answer:

    A 2x2 heatmap image with colors representing values from 5 to 20 and a colorbar -> Option A
  4. Quick Check:

    Check colormap validity and colorbar presence [OK]
Quick Trick: Colormaps map values to colors; colorbar shows scale [OK]
Common Mistakes:
  • Assuming grayscale without specifying cmap='gray'
  • Thinking 'hot' colormap is invalid
  • Ignoring the colorbar effect

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Matplotlib Quizzes