0
0
Matplotlibdata~20 mins

3D axes with projection='3d' in Matplotlib - Practice Problems & Coding Challenges

Choose your learning style9 modes available
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'.