Bird
Raised Fist0
Matplotlibdata~20 mins

3D axes with projection='3d' 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 Plot Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of 3D scatter plot code
What will be the output of this code snippet that creates a 3D scatter plot using matplotlib?
Matplotlib
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
import numpy as np

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

x = np.array([1, 2, 3])
y = np.array([4, 5, 6])
z = np.array([7, 8, 9])

ax.scatter(x, y, z)
plt.show()
AA 2D scatter plot with points at (1,4), (2,5), and (3,6) ignoring z values
BA 3D scatter plot with points at coordinates (1,4,7), (2,5,8), and (3,6,9) displayed
CSyntaxError due to incorrect import of Axes3D
DRuntimeError because projection='3d' is not supported
Attempts:
2 left
💡 Hint
Check how the projection='3d' argument affects the plot type.
data_output
intermediate
1:30remaining
Number of plotted points in 3D line plot
How many points will be connected by the line in this 3D plot code?
Matplotlib
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
import numpy as np

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

x = np.linspace(0, 1, 5)
y = np.linspace(0, 1, 5)
z = np.linspace(0, 1, 5)

ax.plot(x, y, z)
plt.show()
A5
B3
C1
D0
Attempts:
2 left
💡 Hint
Count the number of points generated by np.linspace.
🔧 Debug
advanced
2:00remaining
Identify the error in 3D scatter plot code
What error will this code produce when run?
Matplotlib
import matplotlib.pyplot as plt
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')

x = [1, 2, 3]
y = [4, 5]
z = [7, 8, 9]

ax.scatter(x, y, z)
plt.show()
ANo error; plot displays correctly
BTypeError because lists cannot be used in scatter
CNameError because 'projection' is not a valid argument
DValueError due to mismatched array lengths
Attempts:
2 left
💡 Hint
Check if all coordinate arrays have the same length.
visualization
advanced
2:00remaining
Effect of changing projection parameter
What visual difference occurs when changing projection='3d' to projection='2d' in this code?
Matplotlib
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
import numpy as np

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

x = np.arange(3)
y = np.arange(3)
z = np.arange(3)

ax.plot(x, y, z)
plt.show()
AChanging to projection='2d' causes a TypeError because '2d' is invalid
BChanging to projection='2d' creates a scatter plot instead of a line plot
CChanging to projection='2d' creates a 2D line plot ignoring z values
DChanging to projection='2d' creates a 3D plot with default projection
Attempts:
2 left
💡 Hint
Consider how matplotlib handles 2D vs 3D axes.
🧠 Conceptual
expert
2:30remaining
Understanding 3D axes creation in matplotlib
Which statement correctly describes how matplotlib creates 3D axes with projection='3d'?
Aprojection='3d' creates a special Axes3D object that supports 3D plotting methods
Bprojection='3d' is a style setting that changes colors but keeps 2D axes
Cprojection='3d' automatically imports mpl_toolkits.mplot3d and adds 3D data support
Dprojection='3d' converts 2D plots into 3D by adding a z-axis with zeros
Attempts:
2 left
💡 Hint
Think about what kind of object is created with projection='3d'.

Practice

(1/5)
1. What does setting projection='3d' do when creating axes in matplotlib?
easy
A. It creates a 3D plot area to visualize data in three dimensions.
B. It changes the plot color to 3D style automatically.
C. It enables animation features in the plot.
D. It exports the plot as a 3D model file.

Solution

  1. Step 1: Understand the role of projection parameter

    The projection parameter in matplotlib axes defines the type of plot. Setting it to '3d' enables three-dimensional plotting.
  2. Step 2: Identify the effect of projection='3d'

    This setting creates a 3D plot area where data can be visualized along x, y, and z axes.
  3. Final Answer:

    It creates a 3D plot area to visualize data in three dimensions. -> Option A
  4. Quick Check:

    projection='3d' = 3D plot area [OK]
Hint: projection='3d' means 3D plot space [OK]
Common Mistakes:
  • Thinking it changes colors automatically
  • Assuming it enables animation
  • Believing it exports 3D files
2. Which of the following is the correct way to create a 3D axes object in matplotlib?
easy
A. ax = plt.axes3d()
B. ax = plt.subplot(111, projection='3d')
C. ax = plt.figure(projection='3d')
D. ax = plt.plot(projection='3d')

Solution

  1. Step 1: Recall the syntax for 3D axes creation

    To create 3D axes, use plt.subplot() or plt.axes() with projection='3d'.
  2. Step 2: Check each option

    ax = plt.subplot(111, projection='3d') is correct. The other options use incorrect functions or parameters.
  3. Final Answer:

    ax = plt.subplot(111, projection='3d') -> Option B
  4. Quick Check:

    Use subplot with projection='3d' = correct syntax [OK]
Hint: Use subplot or axes with projection='3d' [OK]
Common Mistakes:
  • Using plt.plot() with projection
  • Passing projection to plt.figure()
  • Calling non-existent plt.axes3d()
3. What will the following code output?
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
ax.scatter([1,2], [3,4], [5,6])
print(type(ax))
medium
A. <class 'matplotlib.axes._subplots.Axes3DSubplot'>
B. <class 'matplotlib.axes._axes.Axes'>
C. SyntaxError
D. RuntimeError

Solution

  1. Step 1: Understand the code creating 3D axes

    The code creates a figure, then adds a 3D subplot with projection='3d'. This returns an Axes3DSubplot object.
  2. Step 2: Check the printed type

    Printing type(ax) will show the class of the 3D axes object, which is Axes3DSubplot.
  3. Final Answer:

    <class 'matplotlib.axes._subplots.Axes3DSubplot'> -> Option A
  4. Quick Check:

    3D subplot type = Axes3DSubplot [OK]
Hint: 3D subplot returns Axes3DSubplot type [OK]
Common Mistakes:
  • Expecting base Axes type
  • Confusing syntax or runtime errors
  • Not importing Axes3D
4. Identify the error in this code snippet:
import matplotlib.pyplot as plt
fig = plt.figure()
ax = fig.add_subplot(111)
ax = plt.axes(projection='3d')
ax.plot([1,2,3], [4,5,6], [7,8,9])
plt.show()
medium
A. Missing import of Axes3D causes error.
B. plot() does not accept three lists for 3D plotting.
C. Calling plt.axes() after fig.add_subplot() overwrites ax incorrectly.
D. plt.show() is missing parentheses.

Solution

  1. Step 1: Analyze axes creation

    The code first creates ax with fig.add_subplot(111) (2D axes), then immediately overwrites ax with plt.axes(projection='3d'). This is confusing and may cause unexpected behavior.
  2. Step 2: Understand the problem

    Overwriting ax without using the figure's subplot can cause the 3D axes to not be linked to the figure properly.
  3. Final Answer:

    Calling plt.axes() after fig.add_subplot() overwrites ax incorrectly. -> Option C
  4. Quick Check:

    Overwriting ax with plt.axes() causes confusion [OK]
Hint: Avoid overwriting axes objects; create 3D axes once [OK]
Common Mistakes:
  • Forgetting to import Axes3D (not needed in recent matplotlib)
  • Thinking plot() can't take 3 lists
  • Missing plt.show() parentheses
5. You want to plot a 3D scatter plot with points colored by their z-value. Which code snippet correctly creates the 3D axes and colors the points accordingly?
hard
A. fig = plt.figure() ax = fig.add_subplot(111, projection='3d') z = [1, 2, 3] ax.scatter([1,2,3], [4,5,6], z) plt.show()
B. fig = plt.figure() ax = plt.axes(projection='3d') z = [1, 2, 3] ax.scatter([1,2,3], [4,5,6], z, color='z') plt.show()
C. fig = plt.figure() ax = fig.add_subplot(111) z = [1, 2, 3] ax.scatter([1,2,3], [4,5,6], z, c=z) plt.show()
D. fig = plt.figure() ax = fig.add_subplot(111, projection='3d') z = [1, 2, 3] ax.scatter([1,2,3], [4,5,6], z, c=z, cmap='viridis') plt.show()

Solution

  1. Step 1: Create 3D axes correctly

    Use fig.add_subplot(111, projection='3d') to create 3D axes linked to the figure.
  2. Step 2: Color points by z-value

    Pass c=z and a colormap like cmap='viridis' to scatter() to color points based on z.
  3. Final Answer:

    fig = plt.figure() ax = fig.add_subplot(111, projection='3d') z = [1, 2, 3] ax.scatter([1,2,3], [4,5,6], z, c=z, cmap='viridis') plt.show() -> Option D
  4. Quick Check:

    3D axes + c=z + cmap = fig = plt.figure() ax = fig.add_subplot(111, projection='3d') z = [1, 2, 3] ax.scatter([1,2,3], [4,5,6], z, c=z, cmap='viridis') plt.show() [OK]
Hint: Use c=z and cmap for coloring in 3D scatter [OK]
Common Mistakes:
  • Using color='z' instead of c=z
  • Creating 2D axes for 3D data
  • Not specifying projection='3d'