Bird
Raised Fist0
Matplotlibdata~20 mins

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

Choose your learning style10 modes available

Start learning this pattern below

Jump into concepts and practice - no test required

or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
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.

Practice

(1/5)
1. What does a 3D wireframe plot in matplotlib primarily show?
easy
A. Only the color distribution of data points
B. A flat 2D scatter plot
C. The shape of data or functions in three dimensions using lines
D. A pie chart with 3D effects

Solution

  1. Step 1: Understand the purpose of 3D wireframe plots

    3D wireframe plots use a grid of lines to represent the shape of data or functions in three dimensions.
  2. Step 2: Compare with other plot types

    Unlike scatter or pie charts, wireframe plots focus on the surface structure, not just colors or flat points.
  3. Final Answer:

    The shape of data or functions in three dimensions using lines -> Option C
  4. Quick Check:

    3D wireframe = 3D shape with lines [OK]
Hint: Wireframe plots show 3D shapes with lines, not colors or points [OK]
Common Mistakes:
  • Confusing wireframe with scatter or surface plots
  • Thinking wireframe shows only colors
  • Assuming wireframe is 2D
2. Which of the following is the correct way to create a 3D wireframe plot using matplotlib?
easy
A. ax.plot_wireframe(X, Y, Z)
B. ax.plot_surface(X, Y, Z)
C. plt.plot_wireframe(X, Y, Z)
D. ax.scatter_wireframe(X, Y, Z)

Solution

  1. Step 1: Identify the correct method for wireframe plots

    The method plot_wireframe is called on the 3D axes object ax.
  2. Step 2: Eliminate incorrect options

    plot_surface creates a surface plot, not wireframe. plt.plot_wireframe is invalid because plt does not have this method. scatter_wireframe does not exist.
  3. Final Answer:

    ax.plot_wireframe(X, Y, Z) -> Option A
  4. Quick Check:

    Wireframe method is plot_wireframe on ax [OK]
Hint: Use ax.plot_wireframe for 3D wireframe plots [OK]
Common Mistakes:
  • Calling plot_wireframe on plt instead of ax
  • Using plot_surface instead of plot_wireframe
  • Using non-existent methods like scatter_wireframe
3. What will the following code output?
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.arange(-5, 6, 5)
Y = np.arange(-5, 6, 5)
X, Y = np.meshgrid(X, Y)
Z = X**2 - Y**2
ax.plot_wireframe(X, Y, Z, rstride=1, cstride=1)
plt.show()
medium
A. A 3D wireframe plot showing a saddle shape
B. A 2D line plot of X and Y
C. A scatter plot of points
D. An error due to incorrect meshgrid usage

Solution

  1. Step 1: Understand the meshgrid and function

    X and Y create a grid from -5 to 5 with step 5, so points at -5, 0, 5. Z = X^2 - Y^2 forms a saddle shape.
  2. Step 2: Analyze the plot_wireframe call

    Using rstride=1 and cstride=1 plots all grid lines, producing a wireframe of the saddle surface.
  3. Final Answer:

    A 3D wireframe plot showing a saddle shape -> Option A
  4. Quick Check:

    Wireframe of Z = X^2 - Y^2 = saddle shape [OK]
Hint: Z = X² - Y² creates a saddle; wireframe shows surface shape [OK]
Common Mistakes:
  • Thinking meshgrid creates error
  • Confusing wireframe with scatter or 2D plot
  • Ignoring the shape of Z function
4. 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()
medium
A. X and Y should be lists, not arrays
B. Missing import for Axes3D
C. plot_wireframe does not exist
D. Z is not a 2D array matching X and Y meshgrid shape

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]
Hint: 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
5. You want to plot a 3D wireframe of the function Z = sin(sqrt(X² + Y²)) over the range -6 to 6 for both X and Y with a grid spacing of 0.5. Which code snippet correctly creates this plot with a blue wireframe and stride of 5?
hard
A. import numpy as np import matplotlib.pyplot as plt fig = plt.figure() ax = fig.add_subplot(111, projection='3d') X = np.linspace(-6, 6, 25) Y = np.linspace(-6, 6, 25) Z = np.sin(np.sqrt(X**2 + Y**2)) ax.plot_wireframe(X, Y, Z, color='blue', stride=5) plt.show()
B. import numpy as np import matplotlib.pyplot as plt fig = plt.figure() ax = fig.add_subplot(111, projection='3d') X = np.arange(-6, 6.5, 0.5) Y = np.arange(-6, 6.5, 0.5) X, Y = np.meshgrid(X, Y) R = np.sqrt(X**2 + Y**2) Z = np.sin(R) ax.plot_wireframe(X, Y, Z, rstride=5, cstride=5, color='blue') plt.show()
C. import numpy as np import matplotlib.pyplot as plt fig = plt.figure() ax = fig.add_subplot(111, projection='3d') X = np.arange(-6, 6, 0.5) Y = np.arange(-6, 6, 0.5) X, Y = np.meshgrid(X, Y) Z = np.sin(np.sqrt(X**2 + Y**2)) ax.plot_wireframe(X, Y, Z, rstride=5, cstride=5, color='red') plt.show()
D. import numpy as np import matplotlib.pyplot as plt fig = plt.figure() ax = fig.add_subplot(111, projection='3d') X = np.arange(-6, 6, 0.5) Y = np.arange(-6, 6, 0.5) X, Y = np.meshgrid(X, Y) Z = np.sin(np.sqrt(X**2 + Y**2)) ax.plot_wireframe(X, Y, Z, rstride=0.5, cstride=0.5, color='blue') plt.show()

Solution

  1. Step 1: Create X and Y grids with correct range and spacing

    Using np.arange(-6, 6.5, 0.5) ensures points from -6 to 6 with 0.5 spacing. Then meshgrid creates 2D arrays.
  2. Step 2: Calculate Z and plot with correct stride and color

    Z is computed as sin(sqrt(X² + Y²)). The wireframe uses rstride=5 and cstride=5 for spacing lines, and color='blue' for blue lines.
  3. Final Answer:

    Code snippet A correctly creates the desired 3D wireframe plot -> Option B
  4. Quick Check:

    Correct meshgrid, stride=5, color='blue' = import numpy as np import matplotlib.pyplot as plt fig = plt.figure() ax = fig.add_subplot(111, projection='3d') X = np.arange(-6, 6.5, 0.5) Y = np.arange(-6, 6.5, 0.5) X, Y = np.meshgrid(X, Y) R = np.sqrt(X**2 + Y**2) Z = np.sin(R) ax.plot_wireframe(X, Y, Z, rstride=5, cstride=5, color='blue') plt.show() [OK]
Hint: Use meshgrid, rstride/cstride for spacing, color param for wireframe [OK]
Common Mistakes:
  • Using stride instead of rstride and cstride
  • Incorrect range or missing meshgrid
  • Wrong color or stride values