Bird
0
0

The code below is intended to show an image with the 'hot' colormap and a colorbar, but it raises an error. What is the problem?

medium📝 Debug Q14 of 15
Matplotlib - Image Display
The code below is intended to show an image with the 'hot' colormap and a colorbar, but it raises an error. What is the problem?
import matplotlib.pyplot as plt
import numpy as np
image = np.random.rand(3,3)
plt.imshow(image, cmap=hot)
plt.colorbar()
plt.show()
Aplt.colorbar() must be called before plt.imshow().
BThe image array shape is invalid for imshow.
CThe colormap name 'hot' should be a string: cmap='hot'.
Dnp.random.rand() cannot be used for images.
Step-by-Step Solution
Solution:
  1. Step 1: Check the cmap parameter usage

    The colormap name must be a string. Here, hot is used without quotes, causing a NameError.
  2. Step 2: Verify other code parts

    The image shape is valid, and colorbar usage is correct. np.random.rand() is valid for image data.
  3. Final Answer:

    The colormap name 'hot' should be a string: cmap='hot'. -> Option C
  4. Quick Check:

    Colormap names need quotes [OK]
Quick Trick: Always put colormap names in quotes [OK]
Common Mistakes:
  • Forgetting quotes around colormap name
  • Thinking image shape is wrong
  • Misordering colorbar and imshow

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Matplotlib Quizzes