如何使用matplotlib绘制二维数学向量? [英] How to plot 2d math vectors with matplotlib?

查看:574
本文介绍了如何使用matplotlib绘制二维数学向量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何使用matplotlib绘制2D数学向量?有人对此有示例或建议吗?

How can we plot 2D math vectors with matplotlib? Does anyone have an example or suggestion about that?

我有几个向量存储为2D numpy数组,我想将它们绘制为有向边.

I have a couple of vectors stored as 2D numpy arrays, and I would like to plot them as directed edges.

要绘制的向量的构造如下:

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]]) 


我刚刚为对输出感兴趣并想要使用matplotlib绘制2d向量的任何人添加了tcaswell最终答案的图:

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:

推荐答案

halex注释中的建议是正确的,您想使用颤动(

The suggestion in the comments by halex is correct, you want to use quiver (doc), but you need to tweak the properties a bit.

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()

这篇关于如何使用matplotlib绘制二维数学向量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆