【问题标题】:Plotting 3D vectors using matplotlib使用 matplotlib 绘制 3D 向量
【发布时间】:2021-02-02 13:57:59
【问题描述】:

我遵循了这个thread 来实现绘制 3D 矢量。

当使用给定的向量示例运行代码时,它看起来不错,但是当我尝试使用自己的向量时,我得到的向量没有方向:

我正在尝试了解我的数据出了什么问题。

def plot_vectors(vectors):
fig = plt.figure()
# ax = fig.gca(projection='3d')
# ax = Axes3D(fig)
ax = fig.add_subplot(111, projection='3d')

for vector in vectors:
    # v1 = np.array([vector[0], vector[1], vector[2]])
    v_length = np.linalg.norm(vector)

    x = vector[0]
    y = vector[1]
    z = vector[2]

    # Make the direction data for the arrows
    u = np.sin(np.pi * x) * np.cos(np.pi * y) * np.cos(np.pi * z)
    v = -np.cos(np.pi * x) * np.sin(np.pi * y) * np.cos(np.pi * z)
    w = (np.sqrt(2.0 / 3.0) * np.cos(np.pi * x) * np.cos(np.pi * y) * np.sin(np.pi * z))

    ax.quiver(x, y, z, u, v, w, pivot='tail', length=v_length, arrow_length_ratio=0.3 / v_length)
    # ax.quiver(vector[0], vector[1], vector[2], vector[3], vector[4], vector[5],
    #           pivot='tail', length=vlength, arrow_length_ratio=0.3 / v_length)

# ax.set_xlim([-4, 4])
# ax.set_ylim([-4, 4])
# ax.set_zlim([0, 4])
ax.set_xlabel('x')
ax.set_ylabel('y')
ax.set_zlabel('z')
plt.show()

例如使用这些向量运行它:

plot_vectors([[95.38065011522669, -15.867331426507993, 267.4801091009956],
          [-52.379559088282384, -1.0021180591632532, 163.80875985057938]])

【问题讨论】:

    标签: python matplotlib


    【解决方案1】:

    我运行了您的代码,看起来使用 .3 / v_length 作为 arrow_length_ratio 会为您的 x、y 和 z 值生成一个超小箭头。我会在这里使用不同的计算... 在这种情况下,也许像 .001 * v_length 这样的东西会起作用。 我会一直玩下去,直到你找到你喜欢的并且适用于你所有数据的东西!

    【讨论】:

      猜你喜欢
      • 2015-01-17
      • 1970-01-01
      • 2016-08-17
      • 1970-01-01
      • 1970-01-01
      • 2014-10-23
      • 1970-01-01
      • 2023-02-05
      • 2017-07-06
      相关资源
      最近更新 更多