简体   繁体   English

绘制简单的2D向量

[英]Plotting a simple 2D vector

New to Python and just trying to accomplish what I think must be the simplest of tasks: plotting a basic 2D vector. Python的新手,只是尝试完成我认为必须是最简单的任务:绘制基本2D向量。 However my online search has gotten me nowhere so I turn to stackoverflow with my very first question. 但是我的在线搜索使我无处可寻,所以我首先问了第一个问题,即stackoverflow。

I Just want to plot a single 2D vector, let's call it my_vector. 我只想绘制一个2D向量,我们称之为my_vector。 my_vector goes from (0,0) to (3,11). my_vector从(0,0)变为(3,11)。

What I have done is this: 我所做的是这样的:

from __future__ import print_function
import numpy as np
import pylab as pl
%pylab inline

x_cords = np.arange(4)
y_cords = np.linspace(0, 11, 4)
my_vector = vstack([x_cords, y_cords])
fig = plt.figure()
ax = fig.add_subplot(111)
ax.plot(my_vector)
plt.show()

Which gives the following image (and totally not what I am after): 这给出了以下图像(并且完全不是我所追求的):

a very wrong plot 一个非常错误的情节

However I have found that 但是我发现

ax.plot(x_cords, y_cords)

instead of 代替

ax.plot(my_vector)

gives me the plot I am looking for but then I don't have that single vector I am after. 给了我我要寻找的情节,但是我没有那个我想要的矢量。

So how does one correctly plot a basic 2D vector? 那么如何正确绘制基本的2D向量呢? Thank you and sorry if this has indeed been posted somewhere else... 谢谢,如果确实在其他地方发布了此内容,则表示抱歉。

You can also unpack your 2D vector 您还可以解压缩2D向量

pl.plot(*my_vector)

Which is effectively just doing 实际上这只是在做

pl.plot(x_cords, y_cords)

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

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