Bird
Raised Fist0
Matplotlibdata~10 mins

Viewing angle control in Matplotlib - Interactive Code Practice

Choose your learning style10 modes available

Start learning this pattern below

Jump into concepts and practice - no test required

or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to set the elevation angle of the 3D plot to 30 degrees.

Matplotlib
ax.view_init(elev=[1], azim=45)
Drag options to blanks, or click blank then click option'
A45
B30
C60
D90
Attempts:
3 left
💡 Hint
Common Mistakes
Confusing elevation with azimuth angle.
Using values outside the typical 0-90 degree range.
2fill in blank
medium

Complete the code to set the azimuth angle of the 3D plot to 120 degrees.

Matplotlib
ax.view_init(elev=20, azim=[1])
Drag options to blanks, or click blank then click option'
A45
B90
C120
D60
Attempts:
3 left
💡 Hint
Common Mistakes
Mixing up elevation and azimuth values.
Using negative values without understanding their effect.
3fill in blank
hard

Fix the error in the code to correctly set the viewing angle with elevation 45 and azimuth 135.

Matplotlib
ax.view_init(elev=45, azim=[1])
Drag options to blanks, or click blank then click option'
Aelev
B'135'
Cazim
D135
Attempts:
3 left
💡 Hint
Common Mistakes
Passing azimuth as a string instead of a number.
Using variable names instead of numeric values.
4fill in blank
hard

Fill both blanks to create a 3D plot with elevation 60 and azimuth 90.

Matplotlib
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
ax.view_init(elev=[1], azim=[2])
plt.show()
Drag options to blanks, or click blank then click option'
A60
B45
C90
D30
Attempts:
3 left
💡 Hint
Common Mistakes
Swapping elevation and azimuth values.
Using incorrect numeric values.
5fill in blank
hard

Fill all three blanks to create a 3D scatter plot with elevation 25, azimuth 135, and set the marker style to 'o'.

Matplotlib
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
ax.view_init(elev=[1], azim=[2])
ax.scatter(x, y, z, marker=[3])
plt.show()
Drag options to blanks, or click blank then click option'
A25
B135
C'o'
D'x'
Attempts:
3 left
💡 Hint
Common Mistakes
Using quotes incorrectly for numeric values.
Choosing wrong marker styles.

Practice

(1/5)
1. What does the ax.view_init(elev, azim) function do in matplotlib 3D plots?
easy
A. It sets the vertical and horizontal viewing angles of the 3D plot.
B. It changes the color of the 3D plot.
C. It adds labels to the axes of the 3D plot.
D. It saves the 3D plot as an image file.

Solution

  1. Step 1: Understand the function purpose

    The ax.view_init function is used to control the viewing angle of 3D plots in matplotlib.
  2. Step 2: Identify parameters meaning

    The parameters elev and azim set the vertical and horizontal angles respectively.
  3. Final Answer:

    It sets the vertical and horizontal viewing angles of the 3D plot. -> Option A
  4. Quick Check:

    Viewing angle control = ax.view_init(elev, azim) [OK]
Hint: Remember elev = vertical, azim = horizontal angles [OK]
Common Mistakes:
  • Confusing view_init with color or label functions
  • Mixing up elev and azim parameters
  • Thinking it saves the plot instead of changing view
2. Which of the following is the correct syntax to set the elevation to 30 and azimuth to 45 in a matplotlib 3D plot?
easy
A. ax.view_init(azim=30, elev=45)
B. ax.view_init(elev=45, azim=30)
C. ax.view_init(45, 30)
D. ax.view_init(30, 45)

Solution

  1. Step 1: Recall parameter order in view_init

    The view_init method takes elev first, then azim.
  2. Step 2: Match values to parameters

    Elevation should be 30 and azimuth 45, so ax.view_init(30, 45) is correct.
  3. Final Answer:

    ax.view_init(30, 45) -> Option D
  4. Quick Check:

    elev=30, azim=45 means ax.view_init(30, 45) [OK]
Hint: Remember order: elev first, then azim [OK]
Common Mistakes:
  • Swapping elev and azim values
  • Using keyword arguments incorrectly
  • Passing azim before elev
3. What will be the effect of this code snippet on the 3D plot's view?
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
ax.view_init(elev=90, azim=0)
plt.show()
medium
A. The plot is viewed from the side at 90 degrees azimuth.
B. The plot is viewed from directly above (top-down view).
C. The plot is viewed from the front with default angles.
D. The plot will raise an error due to invalid angles.

Solution

  1. Step 1: Analyze elev=90 effect

    Elevation of 90 degrees means the camera is directly above the plot looking down.
  2. Step 2: Analyze azim=0 effect

    Azimuth 0 means no horizontal rotation, so the view is straight down from above.
  3. Final Answer:

    The plot is viewed from directly above (top-down view). -> Option B
  4. Quick Check:

    elev=90 means top-down view [OK]
Hint: elev=90 means looking straight down [OK]
Common Mistakes:
  • Thinking azim=0 changes vertical angle
  • Assuming default view instead of top-down
  • Believing this causes an error
4. Identify the error in this code that tries to set the viewing angle:
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
ax.view_init(azim=45, elev=30)
plt.show()
medium
A. The projection='3d' is missing in add_subplot.
B. The plt.show() is missing parentheses.
C. The parameters elev and azim are swapped; elev must come first without keywords.
D. There is no error; the code runs correctly.

Solution

  1. Step 1: Check parameter usage in view_init

    The view_init method does not accept keyword arguments for elev and azim in this order; it expects positional arguments.
  2. Step 2: Identify correct parameter order

    Correct usage is ax.view_init(30, 45) where elev=30 and azim=45 as positional arguments.
  3. Final Answer:

    The parameters elev and azim are swapped; elev must come first without keywords. -> Option C
  4. Quick Check:

    view_init requires positional elev, azim [OK]
Hint: Use positional args: elev first, azim second [OK]
Common Mistakes:
  • Using keyword arguments in wrong order
  • Omitting projection='3d' (not the error here)
  • Forgetting plt.show() parentheses
5. 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?
hard
A. 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()
B. fig = 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()
C. fig = 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()
D. fig = 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()

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]
Hint: 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