Bird
0
0

You want to plot a 3D surface of the function z = sin(sqrt(x² + y²)) over the range -6 to 6 for both x and y. Which code snippet correctly prepares the data for ax.plot_surface?

hard📝 Application Q8 of 15
Matplotlib - 3D Plotting
You want to plot a 3D surface of the function z = sin(sqrt(x² + y²)) over the range -6 to 6 for both x and y. Which code snippet correctly prepares the data for ax.plot_surface?
Aimport numpy as np\nx = np.linspace(-6, 6, 50)\ny = np.linspace(-6, 6, 50)\nX, Y = np.meshgrid(x, y)\nZ = np.sin(np.sqrt(x + y))
Bimport numpy as np\nx = np.arange(-6, 6, 0.1)\ny = np.arange(-6, 6, 0.1)\nZ = np.sin(np.sqrt(x**2 + y**2))
Cimport numpy as np\nx = np.linspace(-6, 6, 50)\ny = np.linspace(-6, 6, 50)\nX, Y = np.meshgrid(x, y)\nZ = np.sin(np.sqrt(X**2 + Y**2))
Dimport numpy as np\nx = np.linspace(-6, 6, 50)\ny = np.linspace(-6, 6, 50)\nZ = np.sin(np.sqrt(x**2 + y**2))
Step-by-Step Solution
Solution:
  1. Step 1: Create grid arrays X and Y

    Use np.meshgrid on x and y arrays to create 2D grids for X and Y.
  2. Step 2: Calculate Z using the function on X and Y

    Compute Z as sin of sqrt of X squared plus Y squared, element-wise on 2D arrays.
  3. Final Answer:

    Code snippet D correctly creates X, Y meshgrid and computes Z -> Option C
  4. Quick Check:

    Meshgrid + element-wise function = correct surface data [OK]
Quick Trick: Always use meshgrid for X, Y before computing Z [OK]
Common Mistakes:
  • Not using meshgrid for X and Y
  • Applying function on 1D arrays only
  • Incorrect formula for Z

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Matplotlib Quizzes