# How to plot vectors in python using matplotlib

Hello Guys, How are you all? Hope You all Are Fine. Today We Are Going To learn about How to plot vectors in python using matplotlib in Python. So Here I am Explain to you all the possible Methods here.

## How to plot vectors in python using matplotlib?

1. How to plot vectors in python using matplotlib?

Thanks to everyone, each of your posts helped me a lot. rbierman code was pretty straight for my question, I have modified a bit and created a function to plot vectors from given arrays. I'd love to see any suggestions to improve it further.

2. plot vectors in python using matplotlib

Thanks to everyone, each of your posts helped me a lot. rbierman code was pretty straight for my question, I have modified a bit and created a function to plot vectors from given arrays. I'd love to see any suggestions to improve it further.

## Method 1

```import numpy as np
import matplotlib.pyplot as plt

V = np.array([[1,1], [-2,2], [4,-7]])
origin = np.array([[0, 0, 0],[0, 0, 0]]) # origin point

plt.quiver(*origin, V[:,0], V[:,1], color=['r','b','g'], scale=21)
plt.show()
```

Then to add up any two vectors and plot them to the same figure, do so before you call `plt.show()`. Something like:

```plt.quiver(*origin, V[:,0], V[:,1], color=['r','b','g'], scale=21)
v12 = V[0] + V[1] # adding up the 1st (red) and 2nd (blue) vectors
plt.quiver(*origin, v12[0], v12[1])
plt.show()
```

NOTE: in Python2 use `origin[0], origin[1]` instead of `*origin`

## Method 2

Thanks to everyone, each of your posts helped me a lot. rbierman code was pretty straight for my question, I have modified a bit and created a function to plot vectors from given arrays. I’d love to see any suggestions to improve it further.

```import numpy as np
import matplotlib.pyplot as plt
def plotv(M):
rows,cols = M.T.shape
print(rows,cols)

#Get absolute maxes for axis ranges to center origin
#This is optional
maxes = 1.1*np.amax(abs(M), axis = 0)
colors = ['b','r','k']
fig = plt.figure()
fig.suptitle('Vectors', fontsize=10, fontweight='bold')

ax.set_title('Vector operations')

ax.set_xlabel('x')
ax.set_ylabel('y')

for i,l in enumerate(range(0,cols)):
# print(i)

ax.text(M[i,0],M[i,1], str(M[i]), style='italic',

plt.plot(0,0,'ok') #<-- plot a black point at the origin
# plt.axis('equal')  #<-- set the axes to the same scale
plt.xlim([-maxes[0],maxes[0]]) #<-- set the x axis limits
plt.ylim([-maxes[1],maxes[1]]) #<-- set the y axis limits

plt.grid(b=True, which='major') #<-- plot grid lines
plt.show()

r = np.random.randint(4,size=[2,2])
print(r[0,:])
print(r[1,:])