Challenge - 5 Problems
3D Surface Plot Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of a simple 3D surface plot code
What will be the shape of the Z array used in this 3D surface plot code?
Matplotlib
import numpy as np from mpl_toolkits.mplot3d import Axes3D import matplotlib.pyplot as plt x = np.linspace(-5, 5, 50) y = np.linspace(-5, 5, 50) X, Y = np.meshgrid(x, y) Z = np.sin(np.sqrt(X**2 + Y**2)) fig = plt.figure() ax = fig.add_subplot(111, projection='3d') ax.plot_surface(X, Y, Z) plt.show()
Attempts:
2 left
💡 Hint
Remember how meshgrid works with two 1D arrays of length 50.
✗ Incorrect
The meshgrid creates two 2D arrays X and Y of shape (50, 50). The function applied element-wise produces Z of the same shape.
❓ data_output
intermediate2:00remaining
Number of faces in a 3D surface plot
Given the following code, how many quadrilateral faces will the 3D surface plot have?
Matplotlib
import numpy as np x = np.linspace(0, 1, 20) y = np.linspace(0, 1, 30) X, Y = np.meshgrid(x, y) Z = X * Y
Attempts:
2 left
💡 Hint
Number of faces = (rows - 1) * (columns - 1)
✗ Incorrect
X and Y have shape (30, 20). Number of faces = (30-1)*(20-1) = 29*19 = 551.
❓ visualization
advanced2:30remaining
Effect of stride parameters on 3D surface plot
Which option shows the correct effect of setting rstride=5 and cstride=5 in ax.plot_surface(X, Y, Z, rstride=5, cstride=5)?
Matplotlib
import numpy as np import matplotlib.pyplot as plt from mpl_toolkits.mplot3d import Axes3D x = np.linspace(-3, 3, 60) y = np.linspace(-3, 3, 60) X, Y = np.meshgrid(x, y) Z = np.exp(-0.1*(X**2 + Y**2)) fig = plt.figure() ax = fig.add_subplot(111, projection='3d') ax.plot_surface(X, Y, Z, rstride=5, cstride=5) plt.show()
Attempts:
2 left
💡 Hint
Stride controls the step size for sampling the grid points.
✗ Incorrect
Higher stride values skip points, resulting in fewer grid lines and a blockier mesh.
🔧 Debug
advanced2:00remaining
Identify the error in 3D surface plot code
What error will this code raise when executed?
Matplotlib
import numpy as np import matplotlib.pyplot as plt from mpl_toolkits.mplot3d import Axes3D x = np.linspace(-2, 2, 40) y = np.linspace(-2, 2, 40) X, Y = np.meshgrid(x, y) Z = np.sin(X) + np.cos(Y) fig = plt.figure() ax = fig.add_subplot(111, projection='3d') ax.plot_surface(X, Y, Z, cmap='nonexistent_cmap') plt.show()
Attempts:
2 left
💡 Hint
Check if the colormap name is valid in matplotlib.
✗ Incorrect
Using an invalid colormap name causes a ValueError when plot_surface tries to apply it.
🚀 Application
expert3:00remaining
Interpreting 3D surface plot data output
Given the code below, what is the maximum Z value computed?
Matplotlib
import numpy as np x = np.linspace(-1, 1, 100) y = np.linspace(-1, 1, 100) X, Y = np.meshgrid(x, y) Z = np.exp(-(X**2 + Y**2)) * np.cos(5 * (X**2 + Y**2))
Attempts:
2 left
💡 Hint
Consider the function behavior at the origin (0,0).
✗ Incorrect
At (0,0), Z = exp(0)*cos(0) = 1*1 = 1, but due to discretization and floating point, max is slightly less than 1.