Challenge - 5 Problems
Viewing Angle Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
What is the elevation angle after this code runs?
Consider this code that sets the elevation angle of a 3D plot. What will be the elevation angle printed?
Matplotlib
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=30, azim=45) elev = ax.elev print(elev)
Attempts:
2 left
💡 Hint
The elev parameter sets the elevation angle in degrees.
✗ Incorrect
The method view_init sets the elevation (elev) and azimuth (azim) angles. Here elev=30 means the elevation angle is 30 degrees.
❓ data_output
intermediate2:00remaining
What is the azimuth angle after setting view_init?
After running this code, what is the azimuth angle stored in ax.azim?
Matplotlib
import matplotlib.pyplot as plt fig = plt.figure() ax = fig.add_subplot(111, projection='3d') ax.view_init(elev=10, azim=120) azim = ax.azim print(azim)
Attempts:
2 left
💡 Hint
The azim parameter controls the azimuth angle in degrees.
✗ Incorrect
The azim parameter in view_init sets the azimuth angle. Here it is set to 120 degrees.
❓ visualization
advanced3:00remaining
Which option shows the correct azimuth and elevation for this 3D plot?
This code creates a 3D plot and sets the viewing angle. Which option correctly describes the viewing angles used?
Matplotlib
import matplotlib.pyplot as plt from mpl_toolkits.mplot3d import Axes3D import numpy as np fig = plt.figure() ax = fig.add_subplot(111, projection='3d') X = np.linspace(-5, 5, 100) Y = np.linspace(-5, 5, 100) X, Y = np.meshgrid(X, Y) Z = np.sin(np.sqrt(X**2 + Y**2)) ax.plot_surface(X, Y, Z, cmap='viridis') ax.view_init(elev=60, azim=30) plt.show()
Attempts:
2 left
💡 Hint
Check the parameters passed to view_init(elev=..., azim=...).
✗ Incorrect
The code sets elev=60 and azim=30, so the viewing angle is elevation 60°, azimuth 30°.
🔧 Debug
advanced2:00remaining
What error does this code raise?
This code tries to set the viewing angle but has a mistake. What error will it raise?
Matplotlib
import matplotlib.pyplot as plt fig = plt.figure() ax = fig.add_subplot(111, projection='3d') ax.view_init(elevation=45, azimuth=90)
Attempts:
2 left
💡 Hint
Check the parameter names for view_init method.
✗ Incorrect
The method view_init expects parameters named elev and azim, not elevation or azimuth.
🚀 Application
expert3:00remaining
How to programmatically rotate a 3D plot by 10 degrees azimuth each frame?
You want to create an animation that rotates a 3D plot by increasing the azimuth angle by 10 degrees every frame, starting from 0°. Which code snippet correctly updates the azimuth angle inside a loop?
Matplotlib
import matplotlib.pyplot as plt from mpl_toolkits.mplot3d import Axes3D import numpy as np fig = plt.figure() ax = fig.add_subplot(111, projection='3d') X = np.linspace(-5, 5, 100) Y = np.linspace(-5, 5, 100) X, Y = np.meshgrid(X, Y) Z = np.sin(np.sqrt(X**2 + Y**2)) ax.plot_surface(X, Y, Z, cmap='coolwarm') for i in range(0, 360, 10): # Update viewing angle here pass plt.pause(0.1)
Attempts:
2 left
💡 Hint
The azimuth angle changes by i degrees each loop, elevation stays fixed.
✗ Incorrect
To rotate by azimuth angle increasing by 10 degrees each frame, set azim=i and keep elev fixed (e.g., 30).