python - 如何使用 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)
解决方案
从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)。
推荐阅读
- fft - 我想通过 gnuradio-companion (i)fft 吗?
- java - 为什么 Toast 会跳到此循环中的最后一个元素?
- macos - 如何在 Visual Studio for Mac 中安装 Dotfuscator?
- node.js - res.redirect() 搜索但不加载
- bash - 下载脚本的密码
- javascript - 什么是 JavaScript 实例?
- sql-server-2008 - 打印带有参数值的动态 SQL
- javascript - 为什么我在使用 push() 时总是在数组中获取数组?
- php - 用于匹配单行标准 USPS 地址的 RegEx
- javascript - 如何在不立即运行的情况下更改元素 onclick 的 onclick?