0
0
Matplotlibdata~20 mins

Viewing angle control in Matplotlib - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Viewing Angle Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2: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)
ANone
B45
C0
D30
Attempts:
2 left
💡 Hint
The elev parameter sets the elevation angle in degrees.
data_output
intermediate
2: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)
A120
BNone
C0
D10
Attempts:
2 left
💡 Hint
The azim parameter controls the azimuth angle in degrees.
visualization
advanced
3: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()
AElevation 30°, Azimuth 60°
BElevation 0°, Azimuth 0°
CElevation 60°, Azimuth 30°
DElevation 45°, Azimuth 45°
Attempts:
2 left
💡 Hint
Check the parameters passed to view_init(elev=..., azim=...).
🔧 Debug
advanced
2: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)
ATypeError: view_init() got an unexpected keyword argument 'elevation'
BAttributeError: 'Axes3D' object has no attribute 'view_init'
CValueError: Elevation angle must be between 0 and 90
DNo error, runs successfully
Attempts:
2 left
💡 Hint
Check the parameter names for view_init method.
🚀 Application
expert
3: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)
Aax.view_init(elev=i, azim=30)
Bax.view_init(elev=30, azim=i)
Cax.view_init(elev=0, azim=i*10)
Dax.view_init(elev=30, azim=i*10)
Attempts:
2 left
💡 Hint
The azimuth angle changes by i degrees each loop, elevation stays fixed.