How to rotate the camera in 3D plots

We have a parametric spiral curve that ascends along the different axes which is described in the following points:

  1. We have spiral curve along the z axis, as shown in the following code:
# Line plot
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
theta = np.linspace(-4 * np.pi, 4 * np.pi, 100)
z = np.linspace(-2, 2, 100)
r = z**2 + 1
x = r * np.sin(theta)
y = r * np.cos(theta)
ax.plot(x,y,z)

Hence, here we see our spiral in the 3D plot:

  1. If we take a look at the view_init method, we have two keyword arguments: elevation, which is given by elev, and azimuth, which is given by azim. These are measured in angles in degrees; the former describes the elevation of the plane, and the latter, the azimuth, describes the rotation:
# Rotating camera angles: elevation & azimuth
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
theta = np.linspace(-4 * np.pi, 4 * np.pi, 100)
z = np.linspace(-2, 2, 100)
r = z**2 + 1
x = r * np.sin(theta)
y = r * np.cos(theta)
ax.plot(x,y,z)
ax.view_init(elev=0)

Hence, if we set the elevation to 0, our perspective shifts to look at this side, as shown here:

  1. When we set the elevation to 90, we look from the top, as shown here:

We can also rotate all the way through a full 360 degrees, as well.

  1. For the azimuth, we are looking at various rotations:
# Rotating camera angles: elevation & azimuth
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
theta = np.linspace(-4 * np.pi, 4 * np.pi, 100)
z = np.linspace(-2, 2, 100)
r = z**2 + 1
x = r * np.sin(theta)
y = r * np.cos(theta)
ax.plot(x,y,z)
ax.view_init(azim=0)

So, an azimuth of 0 looks straight on at one of our axes, as shown here:

  1. In order to look at the y axis, we can set azim to 90, which gives the following output:
  1. Likewise, when we set this to 180, we get the following perspective:

Hence, this is how we can rotate through the full 360 degree angle range for both the azimuth and the elevation to get the perspective we want.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset