python 绘制三维向量

简单粗暴,直接丢代码

里面涉及到的函数可以自己去官方文档或者直接看源码。

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)

效果:
在这里插入图片描述

  • 1
    点赞
  • 18
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值