Bird
0
0

Identify the error in this code snippet:

medium📝 Debug Q6 of 15
Matplotlib - 3D Plotting
Identify the error in this code snippet:
import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D

fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
X = np.linspace(-2, 2, 10)
Y = np.linspace(-2, 2, 10)
Z = X**2 + Y**2
ax.plot_wireframe(X, Y, Z)
plt.show()
AZ is not a 2D array matching the shape of meshgrid X and Y
BMissing import for Axes3D
CIncorrect projection argument in add_subplot
Dplt.show() is missing
Step-by-Step Solution
Solution:
  1. Step 1: Check array shapes

    X and Y are 1D arrays; Z is computed element-wise but is also 1D.
  2. Step 2: plot_wireframe requires 2D arrays

    Must create meshgrid for X and Y, then compute Z on that grid.
  3. Step 3: Error cause

    Passing 1D arrays causes shape mismatch error.
  4. Final Answer:

    Z is not a 2D array matching the shape of meshgrid X and Y -> Option A
  5. Quick Check:

    plot_wireframe needs 2D meshgrid arrays [OK]
Quick Trick: Use meshgrid to create 2D arrays for wireframe plots [OK]
Common Mistakes:
  • Using 1D arrays directly without meshgrid
  • Ignoring shape mismatch errors
  • Assuming plt.show() omission causes error

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Matplotlib Quizzes