首页 > 解决方案 > 如何使用 matplotlib 绘制合成向量?

问题描述

我什至找到了合成向量并打印了它的大小,但我无法绘制它。

当前结果

这是代码:在这里我找到了结果,这是正确的,但我的图表不正确。我得到的输出在最后。

import numpy as np
a1=np.array([2,3])
a2=np.array([1,6])
import matplotlib.pyplot as plt
plt.xlim(-10,10)
plt.ylim(-10,10)
at=a1+a2
origin=np.array([0,0])
plt.quiver(*origin,at[0],at[1],scale=50)
plt.grid()
plt.ylabel('Y-axis')
plt.xlabel('X-axis')
plt.legend(['Vector Addition of two vectors'])
plt.show()
print(at)
m=np.linalg.norm(at)
print(m)

标签: pythonmatplotlib

解决方案


scale_units部分的文档中:

要在 xy 平面上绘制向量,其中 u 和 v 具有与 x 和 y 相同的单位,请使用angles='xy', scale_units='xy', scale=1。

所以你可以把你的行改成这样:

plt.quiver(*origin, at[0], at[1], angles='xy', scale_units='xy', scale=1)

你会得到这个结果:

图形

它指向正确的位置,即 (3, 9)。


推荐阅读