0
0
Matplotlibdata~20 mins

3D surface plots in Matplotlib - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
3D Surface Plot Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2: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()
A(50, 50)
B(50,)
C(100, 100)
D(25, 25)
Attempts:
2 left
💡 Hint
Remember how meshgrid works with two 1D arrays of length 50.
data_output
intermediate
2: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
A552
B551
C570
D550
Attempts:
2 left
💡 Hint
Number of faces = (rows - 1) * (columns - 1)
visualization
advanced
2: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()
AThe surface mesh is colored randomly due to stride parameters.
BThe surface mesh is finer with more grid lines, showing a smooth appearance.
CThe surface mesh is invisible because stride values are too large.
DThe surface mesh is coarser with fewer grid lines, showing a blocky appearance.
Attempts:
2 left
💡 Hint
Stride controls the step size for sampling the grid points.
🔧 Debug
advanced
2: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()
AValueError: 'nonexistent_cmap' is not a valid colormap name
BTypeError: plot_surface() got an unexpected keyword argument 'cmap'
CAttributeError: 'Axes3D' object has no attribute 'plot_surface'
DNo error, plot displays normally
Attempts:
2 left
💡 Hint
Check if the colormap name is valid in matplotlib.
🚀 Application
expert
3: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))
A1.0
B0.0
CApproximately 0.98
D-1.0
Attempts:
2 left
💡 Hint
Consider the function behavior at the origin (0,0).