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
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.
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.
Final Answer:
Calling plt.axes() after fig.add_subplot() overwrites ax incorrectly. -> Option C
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
Step 1: Create 3D axes correctly
Use fig.add_subplot(111, projection='3d') to create 3D axes linked to the figure.
Step 2: Color points by z-value
Pass c=z and a colormap like cmap='viridis' to scatter() to color points based on z.