在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的轴,多用鼠标拖动几次,一般就可以啦
结果如图所示: