Challenge - 5 Problems
3D Plot Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2: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()
Attempts:
2 left
💡 Hint
Check how the projection='3d' argument affects the plot type.
✗ Incorrect
The code creates a 3D scatter plot with the given x, y, z coordinates. The projection='3d' enables 3D plotting in matplotlib.
❓ data_output
intermediate1: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()
Attempts:
2 left
💡 Hint
Count the number of points generated by np.linspace.
✗ Incorrect
np.linspace(0,1,5) generates 5 points evenly spaced between 0 and 1. The plot connects these 5 points in 3D space.
🔧 Debug
advanced2: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()
Attempts:
2 left
💡 Hint
Check if all coordinate arrays have the same length.
✗ Incorrect
The y list has length 2, while x and z have length 3. This mismatch causes a ValueError when plotting.
❓ visualization
advanced2: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()
Attempts:
2 left
💡 Hint
Consider how matplotlib handles 2D vs 3D axes.
✗ Incorrect
projection='2d' is not a valid argument; matplotlib defaults to 2D plotting ignoring z values, so the plot is 2D line connecting (x,y).
🧠 Conceptual
expert2:30remaining
Understanding 3D axes creation in matplotlib
Which statement correctly describes how matplotlib creates 3D axes with projection='3d'?
Attempts:
2 left
💡 Hint
Think about what kind of object is created with projection='3d'.
✗ Incorrect
Setting projection='3d' creates an Axes3D object that has methods for 3D plotting, different from regular 2D axes.