Grace Cheng
Grace Cheng

Reputation: 59

How to plot 2D vectors

I would like to plot vector[3,3] and [2,-2]

But it turns out that the result is incorrect. Can anyone fix my code?

import numpy as np
import matplotlib.pyplot as plt

soa = np.array([[3,3], [2,-2]])
plt.figure()
ax = plt.gca()
ax.quiver(soa[0], soa[1], angles='xy', scale_units='xy', scale=1)
ax.set_xlim([-2, 5])
ax.set_ylim([-2, 5])
plt.draw()
plt.show()

Upvotes: 0

Views: 922

Answers (2)

Michael Szczesny
Michael Szczesny

Reputation: 5036

quiver expects the origins of your vectors and the coordinates as columns

import numpy as np
import matplotlib.pyplot as plt

soa = np.array([[3,3], [2,-2]])
plt.figure()
ax = plt.gca()
ax.quiver(*np.zeros_like(soa.T), *soa.T, angles='xy', scale_units='xy', scale=1, color=['r','g'])
ax.set_xlim([-2, 5])
ax.set_ylim([-2, 5]);

Out:

two vectors

Upvotes: 1

Stan11
Stan11

Reputation: 284

Use the following in your piece of code

origin = np.array([[0, 0],[0, 0]])
ax.quiver(*origin, soa[0], soa[1], angles='xy', scale_units='xy', scale=1)

Upvotes: 0

Related Questions