0
0
Matplotlibdata~20 mins

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

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
3D Wireframe 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 wireframe plot code
What will be the output of the following code snippet that creates a 3D wireframe plot?
Matplotlib
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(-5, 5, 10)
y = np.linspace(-5, 5, 10)
X, Y = np.meshgrid(x, y)
Z = np.sin(np.sqrt(X**2 + Y**2))

ax.plot_wireframe(X, Y, Z)
plt.show()
AA 2D line plot of sine wave
BA bar chart with 3D bars
CA scatter plot with random points in 3D space
DA 3D wireframe plot showing a circular wave pattern with peaks and troughs
Attempts:
2 left
💡 Hint
Think about what plot_wireframe does with meshgrid data in 3D.
data_output
intermediate
2:00remaining
Number of lines in a 3D wireframe plot
Given the following code, how many wireframe lines will be drawn along the X and Y directions respectively?
Matplotlib
import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D

x = np.linspace(0, 1, 15)
y = np.linspace(0, 1, 10)
X, Y = np.meshgrid(x, y)
Z = X * Y

fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
ax.plot_wireframe(X, Y, Z)
plt.show()
A10 lines along X and 15 lines along Y
B14 lines along X and 9 lines along Y
C15 lines along X and 10 lines along Y
D16 lines along X and 11 lines along Y
Attempts:
2 left
💡 Hint
The number of lines corresponds to the number of points in each meshgrid dimension.
🔧 Debug
advanced
2:00remaining
Identify the error in 3D wireframe plot code
What error will this code produce when run, and why?
Matplotlib
import numpy as np
import matplotlib.pyplot as plt

fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')

x = np.linspace(-3, 3, 20)
y = np.linspace(-3, 3, 20)
X, Y = np.meshgrid(x, y)
Z = np.sin(X) + np.cos(Y)

ax.plot_wireframe(X, Y, Z)
plt.show()
ANo error, the code runs and shows a 3D wireframe plot
BNameError: name 'Axes3D' is not defined
CTypeError: plot_wireframe() missing 1 required positional argument
DAttributeError: 'AxesSubplot' object has no attribute 'plot_wireframe'
Attempts:
2 left
💡 Hint
Check if the projection='3d' is correctly set and if the imports are sufficient.
visualization
advanced
2:00remaining
Effect of changing stride parameters in wireframe plot
What is the visual effect of changing the stride parameters in the plot_wireframe method as shown below?
Matplotlib
import numpy as np
import matplotlib.pyplot as plt

fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')

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))

ax.plot_wireframe(X, Y, Z, rstride=10, cstride=5)
plt.show()
AThe wireframe lines are denser along rows and sparser along columns
BThe wireframe lines are spaced more sparsely along rows (rstride=10) and more densely along columns (cstride=5)
CThe plot shows a filled surface instead of wireframe
DThe plot will raise a ValueError due to invalid stride values
Attempts:
2 left
💡 Hint
rstride controls row spacing, cstride controls column spacing of wireframe lines.
🚀 Application
expert
3:00remaining
Using 3D wireframe plots to analyze terrain elevation data
You have elevation data of a terrain stored in a 2D numpy array `elevation` with shape (100, 100). Which code snippet correctly creates a 3D wireframe plot of this terrain assuming x and y coordinates range from 0 to 10?
A
import numpy as np
import matplotlib.pyplot as plt
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
x = np.linspace(0, 10, 100)
y = np.linspace(0, 10, 100)
X, Y = np.meshgrid(x, y)
ax.plot_wireframe(X, Y, elevation.T)
plt.show()
B
import numpy as np
import matplotlib.pyplot as plt
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
x = np.linspace(0, 10, 100)
y = np.linspace(0, 10, 100)
X, Y = np.meshgrid(y, x)
ax.plot_wireframe(X, Y, elevation)
plt.show()
C
import numpy as np
import matplotlib.pyplot as plt
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
x = np.linspace(0, 10, 100)
y = np.linspace(0, 10, 100)
X, Y = np.meshgrid(x, y)
ax.plot_wireframe(X, Y, elevation)
plt.show()
D
import numpy as np
import matplotlib.pyplot as plt
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
x = np.linspace(0, 10, 100)
y = np.linspace(0, 10, 100)
X, Y = np.meshgrid(x, y)
ax.plot_wireframe(Y, X, elevation)
plt.show()
Attempts:
2 left
💡 Hint
Remember that meshgrid creates X and Y with shape matching (len(y), len(x)) and elevation shape must align accordingly.