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.
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()
| Step | Action | Parameter | Effect on View | Output |
|---|---|---|---|---|
| 1 | Create figure | fig = plt.figure() | Prepare canvas | Empty 3D plot ready |
| 2 | Add 3D subplot | ax = fig.add_subplot(111, projection='3d') | 3D axes created | Axes ready for 3D data |
| 3 | Plot points | ax.scatter([1,2,3], [4,5,6], [7,8,9]) | Points placed in 3D space | Points visible in default view |
| 4 | Set view angle | ax.view_init(elev=30, azim=45) | Rotate view to elev=30°, azim=45° | View changes to new angle |
| 5 | Show plot | plt.show() | Render plot window | 3D scatter plot displayed with new angle |
| 6 | Exit | Plot window closed | End of visualization | Execution stops |
| Variable | Start | After Step 2 | After Step 3 | After Step 4 | Final |
|---|---|---|---|---|---|
| fig | None | Figure object created | Figure object created | Figure object created | Figure object created |
| ax | None | 3D Axes object created | 3D Axes with points plotted | 3D Axes with view angle set | 3D Axes with view angle set |
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