Plotting a vector in matplotlib

Is there a straightforward way of plotting a vector in matplotlib? Suppose I want to plot the vector [1 2]’. If I pass this vector in to plot(), I get the line that passes through (0,1), (1,2). Instead I want the line that passes through (0,0),(1,2).

Aditya

wefoundland.com

Hi Aditya,

if you pass a 1-dimensional interatable to the plot-function the elements are
plotted over their indices. In your case [1, 2] gives the y-values and the
indices [0, 1] are used as x-values.

I don't know if there is some kind of vector class yet, but meanwhile you
could set up your one like in the attached small example.

Kind regards,
Matthias

simple_vector_class.py (732 Bytes)

···

On Sunday 02 May 2010 20:19:29 aditya bhargava wrote:

Is there a straightforward way of plotting a vector in matplotlib? Suppose
I want to plot the vector [1 2]'. If I pass this vector in to plot(), I get
the line that passes through (0,1), (1,2). Instead I want the line that
passes through (0,0),(1,2).

Aditya

2010-05-02 20:19, aditya bhargava skrev:

Is there a straightforward way of plotting a vector in matplotlib?
Suppose I want to plot the vector [1 2]'. If I pass this vector in to
plot(), I get the line that passes through (0,1), (1,2). Instead I want
the line that passes through (0,0),(1,2).

I use pyplot.Arrow to visualize displacement fields.

This is a snippet copied from the code I use (it sits in a loop over all vectors I want to plot):

         arr = plt.Arrow(x, y, dx, dy)
         plt.gca().add_patch(arr)

In your case, you would have (x, y) = (0, 0) and (dx, dy) = (1, 2).

Regards

Johan

Thanks Johan and Matthias,
I was just wondering if there was a built-in way to do this in matplotlib. It seems like it would be a useful method to have.

Adit

2010/5/3 Johan Grönqvist <johan.gronqvist@…287…>

···

2010-05-02 20:19, aditya bhargava skrev:

Is there a straightforward way of plotting a vector in matplotlib?

Suppose I want to plot the vector [1 2]'. If I pass this vector in to

plot(), I get the line that passes through (0,1), (1,2). Instead I want

the line that passes through (0,0),(1,2).

I use pyplot.Arrow to visualize displacement fields.

This is a snippet copied from the code I use (it sits in a loop over all

vectors I want to plot):

     arr = plt.Arrow(x, y, dx, dy)

     plt.gca().add_patch(arr)

In your case, you would have (x, y) = (0, 0) and (dx, dy) = (1, 2).

Regards

Johan



Matplotlib-users mailing list

Matplotlib-users@lists.sourceforge.net

https://lists.sourceforge.net/lists/listinfo/matplotlib-users


wefoundland.com