python绘制三维向量

在Python中,ax.quiver()是 Matplotlib 库中用于创建箭头图(quiver plot)的函数。箭头图通常用于表示向量场或者流场。


在 ax.quiver() 函数中,你可以指定起点和终点的坐标作为两个独立的参数,或者将起点的坐标和箭头的长度、方向分量作为四个参数传递。 分别采用两种方法的示例:
1.独立的参数方式:
import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D

fig = plt.figure()
ax = fig.gca(projection='3d')

start_point = np.array([100, -100, 200])
end_point = np.array([500, 600, 700])

ax.quiver(start_point[0], start_point[1], start_point[2],
          end_point[0], end_point[1], end_point[2],
          arrow_length_ratio=0.1)

plt.show()


2.坐标和方向、长度方式:
import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D

fig = plt.figure()
ax = fig.gca(projection='3d')

start_point = np.array([100, -100, 200])
arrow_direction = np.array([500, 600, 700]) - start_point
arrow_length = np.linalg.norm(arrow_direction)

ax.quiver(start_point[0], start_point[1], start_point[2],
          arrow_direction[0], arrow_direction[1], arrow_direction[2],
          arrow_length=arrow_length, arrow_length_ratio=0.1)

plt.show()

其中arrow_length_ratio为箭头和向量长度的比值,自己调整的合适的比例就好啦~

如果存在向量没显示的问题,可以调整一下XYZ的轴,多用鼠标拖动几次,一般就可以啦

结果如图所示:

  • 5
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值