Bird
0
0

You want to plot a 3D scatter plot where point colors represent a fourth variable. Which code correctly creates the 3D axes and applies color mapping?

hard📝 Application Q9 of 15
Matplotlib - 3D Plotting
You want to plot a 3D scatter plot where point colors represent a fourth variable. Which code correctly creates the 3D axes and applies color mapping?
Afig = plt.figure() ax = fig.add_subplot(111, projection='3d') sc = ax.scatter(x, y, z, c=colors, cmap='viridis') plt.colorbar(sc) plt.show()
Bfig = plt.figure() ax = plt.axes(projection=3d) ax.scatter(x, y, z, color=colors) plt.show()
Cfig = plt.figure() ax = fig.add_subplot(111) ax.scatter(x, y, z, c=colors) plt.show()
Dfig = plt.figure() ax = fig.add_subplot(111, projection='3d') ax.scatter(x, y, z) plt.colorbar() plt.show()
Step-by-Step Solution
Solution:
  1. Step 1: Create 3D axes

    Use fig.add_subplot(111, projection='3d') to enable 3D plotting.
  2. Step 2: Scatter with color mapping

    Pass the fourth variable as c=colors and specify a colormap with cmap='viridis'.
  3. Step 3: Add colorbar

    Use plt.colorbar(sc) to display the color scale.
  4. Final Answer:

    fig = plt.figure() ax = fig.add_subplot(111, projection='3d') sc = ax.scatter(x, y, z, c=colors, cmap='viridis') plt.colorbar(sc) plt.show() correctly implements all steps.
  5. Quick Check:

    Check for projection='3d', c=colors, and colorbar usage [OK]
Quick Trick: Use c=colors and plt.colorbar() with 3D axes [OK]
Common Mistakes:
  • Using projection without quotes
  • Omitting color mapping parameter
  • Calling colorbar without scatter reference

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Matplotlib Quizzes