1

How to rotate the plotted curve about 90 degrees, please? Is it possible to set that the result of ax.plot(x, y, z) should be rotated about 90 degrees? Thank you

import matplotlib as mpl from mpl_toolkits.mplot3d import Axes3D import numpy as np import matplotlib.pyplot as plt mpl.rcParams['legend.fontsize'] = 10 fig = plt.figure() ax = fig.gca(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, label='parametric curve') plt.show() 

enter image description here

3
  • You can control the elevation and azimuth of the camera with ax.view_init(elev, azim), e.g. ax.view_init(azim=ax.azim+90) Commented Apr 11, 2020 at 8:49
  • It is not possible in the desired direction. Or how to exchange z-axis and y-axis? Commented Apr 11, 2020 at 8:56
  • To exchange the z- and y-axis you could change the order of the arguments in your parametric plot, e.g. ax.plot(x, z, y, label='parametric curve'). Commented Apr 11, 2020 at 9:28

1 Answer 1

1

I advice you to have a look at the rotation matrix. The topic from Wikipedia is a good start! Let's implement it:

We can operate 3 rotations according the 3 axis.

Workflow:

  • First, we must be sure to deal with radian angle.
  • Second, we need to implement the rotation matrix
  • Then we compute the rotation using the numpy.dot
np.array([np.dot(rotation_matrix, vect) for vect in zip(X, Y, Z)]) 
  • Finally plot the results

Full code:

# Modules from mpl_toolkits.mplot3d import Axes3D import numpy as np import matplotlib.pyplot as plt from math import cos, sin, pi # Input angles angle_x = 90 angle_y = 90 angle_z = 90 # Conversion radian theta_x = angle_x*pi/180 theta_y = angle_y*pi/180 theta_z = angle_z*pi/180 # rotation matrix R_x = np.array([[1, 0 , 0 ], [0, cos(theta_x), -sin(theta_x)], [0, sin(theta_x), cos(theta_x)]]) R_y = np.array([[ cos(theta_y), 0, sin(theta_y)], [ 0 , 1, 0 ], [-sin(theta_y), 0, cos(theta_y)]]) R_z = np.array([[cos(theta_z), -sin(theta_z), 0], [sin(theta_z), cos(theta_z), 0], [0 , 0 , 1]]) # Compute initial curve 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) # Compute rotation rotated_x = np.array([np.dot(R_x, vect) for vect in zip(X, Y, Z)]) rotated_y = np.array([np.dot(R_y, vect) for vect in zip(X, Y, Z)]) rotated_z = np.array([np.dot(R_z, vect) for vect in zip(X, Y, Z)]) # Extras for plotting def addExtras(ax): ax.plot(X, Y, Z, label='Initial curve') ax.set_xlabel('X Axis') ax.set_ylabel('Y Axis') ax.set_zlabel('Z Axis') plt.legend() # Create figure fig = plt.figure() # Create subplots ax = fig.add_subplot(2, 2, 1, projection='3d') addExtras(ax) ax = fig.add_subplot(2, 2, 2, projection='3d') ax.plot(rotated_x[:, 0], rotated_x[:, 1], rotated_x[:, 2], label='X+90° rotation curve') addExtras(ax) ax = fig.add_subplot(2, 2, 3, projection='3d') ax.plot(rotated_y[:, 0], rotated_y[:, 1], rotated_y[:, 2], label='Y+90° rotation curve') addExtras(ax) ax = fig.add_subplot(2, 2, 4, projection='3d') ax.plot(rotated_z[:, 0], rotated_z[:, 1], rotated_z[:, 2], label='Z+90° rotation curve') addExtras(ax) # Show results plt.show() 

Output enter image description here

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.