Bird
0
0

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📝 Application Q15 of 15
Matplotlib - 3D Plotting
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?
Afig = plt.figure() ax = fig.add_subplot(111, projection='3d') z = [1, 2, 3] ax.scatter([1,2,3], [4,5,6], z) plt.show()
Bfig = plt.figure() ax = plt.axes(projection='3d') z = [1, 2, 3] ax.scatter([1,2,3], [4,5,6], z, color='z') plt.show()
Cfig = plt.figure() ax = fig.add_subplot(111) z = [1, 2, 3] ax.scatter([1,2,3], [4,5,6], z, c=z) plt.show()
Dfig = 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()
Step-by-Step Solution
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]
Quick Trick: 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'

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Matplotlib Quizzes