0
0
Matplotlibdata~10 mins

Viewing angle control in Matplotlib - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Viewing angle control
Create 3D plot
Set viewing angle: azim, elev
Render plot with new angle
Visualize rotated 3D data
We create a 3D plot, set the viewing angles (azimuth and elevation), then render the plot to see the rotated view.
Execution Sample
Matplotlib
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
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=30, azim=45)
plt.show()
This code creates a 3D scatter plot and sets the viewing angle to elevation 30° and azimuth 45°.
Execution Table
StepActionParameterEffect on ViewOutput
1Create figurefig = plt.figure()Prepare canvasEmpty 3D plot ready
2Add 3D subplotax = fig.add_subplot(111, projection='3d')3D axes createdAxes ready for 3D data
3Plot pointsax.scatter([1,2,3], [4,5,6], [7,8,9])Points placed in 3D spacePoints visible in default view
4Set view angleax.view_init(elev=30, azim=45)Rotate view to elev=30°, azim=45°View changes to new angle
5Show plotplt.show()Render plot window3D scatter plot displayed with new angle
6ExitPlot window closedEnd of visualizationExecution stops
💡 Plot window closed by user, ending visualization
Variable Tracker
VariableStartAfter Step 2After Step 3After Step 4Final
figNoneFigure object createdFigure object createdFigure object createdFigure object created
axNone3D Axes object created3D Axes with points plotted3D Axes with view angle set3D Axes with view angle set
Key Moments - 2 Insights
Why does changing azim and elev in view_init rotate the plot instead of moving the points?
Because view_init changes the camera angle, not the data coordinates. The points stay fixed; only the viewpoint changes (see execution_table step 4).
What happens if you call view_init before plotting points?
The view angle is set before points are drawn, so the final plot still shows points from that angle (the order does not affect the final view, see variable_tracker).
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table at step 4. What does ax.view_init(elev=30, azim=45) do?
AIt changes the viewing angle of the plot
BIt moves the points to new coordinates
CIt changes the color of the points
DIt creates a new figure
💡 Hint
Refer to the 'Effect on View' column in step 4 of the execution_table
According to variable_tracker, what is the state of 'ax' after step 3?
A3D Axes object created
B3D Axes with points plotted
CNone
D3D Axes with view angle set
💡 Hint
Check the 'ax' row under 'After Step 3' in variable_tracker
If you change elev to 90 in view_init, what will happen to the plot view?
AThe plot will be viewed from the side
BThe plot will be viewed from below
CThe plot will be viewed from directly above
DThe plot will not change
💡 Hint
Elevation controls vertical angle; 90 means looking straight down (see concept_flow)
Concept Snapshot
Viewing angle control in matplotlib 3D plots:
- Use ax.view_init(elev, azim) to set elevation and azimuth angles
- Elevation (elev) is vertical angle in degrees
- Azimuth (azim) is horizontal rotation in degrees
- Changes the camera view, not the data
- Call before plt.show() to see effect
Full Transcript
This lesson shows how to control the viewing angle of a 3D plot in matplotlib. We start by creating a figure and adding 3D axes. Then we plot points in 3D space. Using ax.view_init with elevation and azimuth angles, we rotate the camera view around the data. Finally, we render the plot with plt.show(). The points stay fixed; only the viewpoint changes. This helps us see the data from different angles easily.