简体   繁体   English

如何使用matplotlib绘制2d数学向量?

[英]How to plot 2d math vectors with matplotlib?

How can we plot 2D math vectors with matplotlib ? 我们如何使用matplotlib绘制2D数学向量? Does anyone have an example or suggestion about that? 有没有人有这方面的例子或建议?

I have a couple of vectors stored as 2D numpy arrays, and I would like to plot them as directed edges. 我有几个向量存储为2D numpy数组,我想将它们绘制为有向边。

The vectors to be plotted are constructed as below: 要绘制的向量构造如下:

import numpy as np
# a list contains 3 vectors;
# each list is constructed as the tail and the head of the vector
a = np.array([[0, 0, 3, 2], [0, 0, 1, 1], [0, 0, 9, 9]]) 

Edit: 编辑:

I just added the plot of the final answer of tcaswell for anyone interested in the output and want to plot 2d vectors with matplotlib: 我刚刚为任何对输出感兴趣的人添加了tcaswell的最终答案图,并想用matplotlib绘制2d向量: 在此输入图像描述

The suggestion in the comments by halex is correct, you want to use quiver ( doc ), but you need to tweak the properties a bit. halex评论中的建议是正确的,你想使用quiver( doc ),但你需要稍微调整属性。

import numpy as np
import matplotlib.pyplot as plt

soa = np.array([[0, 0, 3, 2], [0, 0, 1, 1], [0, 0, 9, 9]])
X, Y, U, V = zip(*soa)
plt.figure()
ax = plt.gca()
ax.quiver(X, Y, U, V, angles='xy', scale_units='xy', scale=1)
ax.set_xlim([-1, 10])
ax.set_ylim([-1, 10])
plt.draw()
plt.show()

It's pretty straightforward. 这很简单。 Hope this example helps. 希望这个例子有帮助。

import matplotlib.pyplot as plt
import numpy as np
x = np.random.normal(10,5,100)
y = 3 + .5*x + np.random.normal(0,1,100)
myvec = np.array([x,y])
plt.plot(myvec[0,],myvec[1,],'ro')
plt.show()

Will produce: 会产生:

在此输入图像描述

To plot the arrays you can just slice them up into 1D vectors and plot them. 要绘制数组,您可以将它们切片为1D向量并绘制它们。 I'd read the full documentation of matplotlib for all the different options. 我已经阅读了所有不同选项的matplotlib的完整文档。 But you can treat a numpy vector as if it were a normal tuple for most of the examples. 但是你可以将numpy向量视为大多数示例的正常元组。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM