Bird
0
0

Which code snippet correctly achieves this and also labels the axes?

hard📝 Application Q15 of 15
Matplotlib - 3D Plotting
You want to create a 3D scatter plot and set the view so the plot looks rotated 45 degrees horizontally and tilted 30 degrees vertically. Which code snippet correctly achieves this and also labels the axes?
Afig = plt.figure() ax = fig.add_subplot(111, projection='3d') ax.scatter([1,2,3], [4,5,6], [7,8,9]) ax.view_init(30, 45) ax.set_xlabel('X axis') ax.set_ylabel('Y axis') ax.set_zlabel('Z axis') plt.show()
Bfig = plt.figure() ax = fig.add_subplot(111) ax.scatter([1,2,3], [4,5,6], [7,8,9]) ax.view_init(45, 30) plt.show()
Cfig = plt.figure() ax = fig.add_subplot(111, projection='3d') ax.scatter([1,2,3], [4,5,6], [7,8,9]) ax.view_init(elev=45, azim=30) plt.show()
Dfig = plt.figure() ax = fig.add_subplot(111, projection='3d') ax.scatter([1,2,3], [4,5,6], [7,8,9]) ax.view_init(45, 30) ax.set_xlabel('X axis') ax.set_ylabel('Y axis') ax.set_zlabel('Z axis') plt.show()
Step-by-Step Solution
Solution:
  1. Step 1: Check subplot creation for 3D

    Only options A, C, and D use projection='3d', which is required for 3D plots.
  2. Step 2: Verify view_init parameters

    The question wants elevation 30 and azimuth 45, so ax.view_init(30, 45) is correct. fig = plt.figure() ax = fig.add_subplot(111, projection='3d') ax.scatter([1,2,3], [4,5,6], [7,8,9]) ax.view_init(30, 45) ax.set_xlabel('X axis') ax.set_ylabel('Y axis') ax.set_zlabel('Z axis') plt.show() matches this.
  3. Step 3: Confirm axis labels are set

    fig = plt.figure() ax = fig.add_subplot(111, projection='3d') ax.scatter([1,2,3], [4,5,6], [7,8,9]) ax.view_init(30, 45) ax.set_xlabel('X axis') ax.set_ylabel('Y axis') ax.set_zlabel('Z axis') plt.show() sets all three axis labels correctly with set_xlabel, set_ylabel, and set_zlabel.
  4. Final Answer:

    Option A correctly sets view angles and labels axes. -> Option A
  5. Quick Check:

    3D plot + view_init(30,45) + axis labels = fig = plt.figure() ax = fig.add_subplot(111, projection='3d') ax.scatter([1,2,3], [4,5,6], [7,8,9]) ax.view_init(30, 45) ax.set_xlabel('X axis') ax.set_ylabel('Y axis') ax.set_zlabel('Z axis') plt.show() [OK]
Quick Trick: Use projection='3d', view_init(30,45), then label axes [OK]
Common Mistakes:
  • Missing projection='3d' for 3D plots
  • Swapping elev and azim values
  • Not labeling all three axes

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Matplotlib Quizzes