简单粗暴,直接丢代码
里面涉及到的函数可以自己去官方文档或者直接看源码。
import matplotlib.pyplot as plt
import numpy as np
import numpy.random
def creat3d_vec_xyz(start_points, end_points, colors=None, view_angle=20, azimuth=30):
'''
创建一个空间三维坐标系
:param start_points: 绘制的数据起点
:param end_points: 绘制的数据终点
:param colors: 绘制的向量颜色
:param view_angle: 观察角度
:param azimuth: 方位角
:return:
'''
assert start_points.shape == end_points.shape
if colors is None:
colors = numpy.random.randint(0, 255, size=start_points.shape, dtype=np.uint8)
fig = plt.figure()
ax = fig.gca(projection='3d')
# ax = fig.gca(projection='3d')
num_vec = start_points.shape[0]
# q = ax.quiver(start_points[:, 0], start_points[:, 1], start_points[:, 2], end_points[:, 0], end_points[:, 1],
# end_points[:, 2], color="#666666", arrow_length_ratio=0.1)
for i in range(num_vec):
color = '#'
for j in range(3):
color += str(hex(colors[i, j]))[-2:].replace('x', '0').upper()
q = ax.quiver(start_points[i, 0], start_points[i, 1], start_points[i, 2], end_points[i, 0], end_points[i, 1],
end_points[i, 2], color=color, arrow_length_ratio=0.1)
ax.set_xlim(0, 1)
ax.set_ylim(0, 1)
ax.set_zlim(0, 1)
ax.set_xlabel('x')
ax.set_ylabel('y')
ax.set_zlabel('z')
# 调整观察角度和方位角。这里将俯仰角设为60度,把方位角调整为35度
ax.view_init(view_angle, azimuth)
ax.set_title('coordinates-xyz')
plt.show()
使用示例:
import numpy as np
from utils import plot
end_points = [[0, 0, 1], [1, 1, 1], [0.5, 0.5, 1], [1.5, 1.5, 1]]
end_points = np.array(end_points)
end_points = end_points / np.sqrt(np.sum(end_points ** 2, axis=1)).reshape(-1, 1)
start_points = np.zeros((4, 3))
plot.creat3d_vec_xyz(start_points, end_points)
效果: