Bird
0
0

What is wrong with this code?

medium📝 Debug Q7 of 15
Matplotlib - Image Display
What is wrong with this code?
import matplotlib.pyplot as plt
import numpy as np
image = np.array([[0, 1], [2, 3]])
plt.imshow(image, cmap='jet')
plt.colorbar
plt.show()
AThe image array contains invalid values
BThe 'jet' colormap is deprecated and cannot be used
Cplt.colorbar is missing parentheses, so colorbar won't display
Dplt.show() should come before plt.colorbar()
Step-by-Step Solution
Solution:
  1. Step 1: Check plt.colorbar usage

    plt.colorbar is a function and must be called with parentheses to display the colorbar.
  2. Step 2: Confirm impact of missing parentheses

    Without parentheses, no colorbar appears, but no error is raised.
  3. Final Answer:

    plt.colorbar is missing parentheses, so colorbar won't display -> Option C
  4. Quick Check:

    Call plt.colorbar() with () to show colorbar [OK]
Quick Trick: Always use plt.colorbar() with parentheses [OK]
Common Mistakes:
  • Forgetting parentheses on plt.colorbar
  • Thinking 'jet' colormap is invalid
  • Misordering plt.show() and plt.colorbar()

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Matplotlib Quizzes