Bird
0
0

Identify the error in this 3D plot code snippet:

medium📝 Debug Q14 of 15
Matplotlib - 3D Plotting
Identify the error in this 3D plot code snippet:
import matplotlib.pyplot as plt
fig = plt.figure()
ax = fig.add_subplot(111)
x = [1, 2, 3]
y = [4, 5, 6]
z = [7, 8, 9]
ax.scatter(x, y, z)
plt.show()
Ascatter does not accept three arguments
Bz should be a 2D array
CMissing projection='3d' in add_subplot
Dplt.show() is missing
Step-by-Step Solution
Solution:
  1. Step 1: Check subplot creation

    The subplot is created without specifying projection='3d', so it is a 2D plot.
  2. Step 2: Understand scatter usage

    scatter with three arguments requires a 3D axes, which is missing here, causing an error.
  3. Final Answer:

    Missing projection='3d' in add_subplot -> Option C
  4. Quick Check:

    3D plot needs projection='3d' = C [OK]
Quick Trick: Always add projection='3d' for 3D axes [OK]
Common Mistakes:
  • Assuming scatter accepts 3 args on 2D axes
  • Thinking z must be 2D array
  • Forgetting plt.show() is present

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Matplotlib Quizzes