Reputation: 13
New to Python and just trying to accomplish what I think must be the simplest of tasks: plotting a basic 2D vector. However my online search has gotten me nowhere so I turn to stackoverflow with my very first question.
I Just want to plot a single 2D vector, let's call it my_vector. my_vector goes from (0,0) to (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):
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? Thank you and sorry if this has indeed been posted somewhere else...
Upvotes: 1
Views: 1932
Reputation: 117856
You can also unpack your 2D vector
pl.plot(*my_vector)
Which is effectively just doing
pl.plot(x_cords, y_cords)
Upvotes: 1