Bird
0
0

Identify the error in this code snippet for a 3D wireframe plot:

medium📝 Debug Q14 of 15
Matplotlib - 3D Plotting
Identify the error in this code snippet for a 3D wireframe plot:
import numpy as np
import matplotlib.pyplot as plt
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
X = np.linspace(-3, 3, 10)
Y = np.linspace(-3, 3, 10)
Z = np.sin(X) * np.cos(Y)
ax.plot_wireframe(X, Y, Z)
plt.show()
AX and Y should be lists, not arrays
BMissing import for Axes3D
Cplot_wireframe does not exist
DZ is not a 2D array matching X and Y meshgrid shape
Step-by-Step Solution
Solution:
  1. Step 1: Check shapes of X, Y, and Z

    X and Y are 1D arrays; Z is computed element-wise but is also 1D, not 2D grid.
  2. Step 2: Understand plot_wireframe requirements

    plot_wireframe requires X, Y, Z to be 2D arrays from meshgrid to plot a surface grid.
  3. Final Answer:

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

    plot_wireframe needs 2D X, Y, Z arrays [OK]
Quick Trick: Use meshgrid to make X, Y, Z 2D arrays for wireframe [OK]
Common Mistakes:
  • Passing 1D arrays instead of meshgrid 2D arrays
  • Ignoring shape mismatch errors
  • Assuming plot_wireframe works with 1D inputs

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Matplotlib Quizzes