在Matplotlib中,可以使用mplot3d模块创建和操作3D图形。要在3D图中绕任意轴旋转,可以使用以下步骤:
import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
x = np.random.rand(100)
y = np.random.rand(100)
z = np.random.rand(100)
colors = np.random.rand(100)
ax.scatter(x, y, z, c=colors)
start = np.array([0, 0, 0])
direction = np.array([1, 1, 1])
theta = np.deg2rad(45) # 旋转角度
rotation_matrix = np.array([[np.cos(theta) + direction[0]**2 * (1 - np.cos(theta)),
direction[0] * direction[1] * (1 - np.cos(theta)) - direction[2] * np.sin(theta),
direction[0] * direction[2] * (1 - np.cos(theta)) + direction[1] * np.sin(theta)],
[direction[1] * direction[0] * (1 - np.cos(theta)) + direction[2] * np.sin(theta),
np.cos(theta) + direction[1]**2 * (1 - np.cos(theta)),
direction[1] * direction[2] * (1 - np.cos(theta)) - direction[0] * np.sin(theta)],
[direction[2] * direction[0] * (1 - np.cos(theta)) - direction[1] * np.sin(theta),
direction[2] * direction[1] * (1 - np.cos(theta)) + direction[0] * np.sin(theta),
np.cos(theta) + direction[2]**2 * (1 - np.cos(theta))]])
rotated_points = np.dot(rotation_matrix, np.array([x, y, z]))
ax.scatter(rotated_points[0], rotated_points[1], rotated_points[2], c=colors)
ax.set_title('3D Scatter Plot')
ax.set_xlabel('X')
ax.set_ylabel('Y')
ax.set_zlabel('Z')
plt.show()
这样就可以在Matplotlib中绕任意轴旋转3D图形了。
关于Matplotlib的更多信息和使用方法,可以参考腾讯云的Matplotlib产品介绍链接地址:Matplotlib产品介绍
领取专属 10元无门槛券
手把手带您无忧上云