Chirag Modi
Chirag Modi

Reputation: 11

plt.quiver plots wrong vectors

I'm trying to plot the vectors A and B using matplotlib but somehow, it doesn't plot correctly. Here's my code:

import matplotlib.pyplot as plt

A = [2,4]
B = [1,2]
X = [0,0]
Y = [0,0]

plt.quiver(X,Y,A,B, color=['b','r'],angles='xy', scale_units='xy', scale=1)
plt.ylim(0,5)
plt.xlim(0,5)
plt.show()

Here's the plot: Image

A is somehow plotted as (2, 1) and B as (4,2). Can someone explain this?

Upvotes: 1

Views: 269

Answers (1)

Davide_sd
Davide_sd

Reputation: 13185

As you can read from the documentation, help(plt.quiver):

Call signature:

quiver([X, Y], U, V, [C], **kw)

X, Y define the arrow locations, U, V define the arrow directions, and C optionally sets the color.

In your code:

plt.quiver(X,Y,A,B, color=['b','r'],angles='xy', scale_units='xy', scale=1)

You are telling Matplotlib that A represents the horizontal direction of your vectors, and B represents the vertical direction.

Instead, you want A and B to be the actual vectors (starting from X and Y). Hence, you need to write them as:

import matplotlib.pyplot as plt

U = [2,1]
V = [4,2]
X = [0,0]
Y = [0,0]

plt.quiver(X,Y,U,V, color=['b','r'],angles='xy', scale_units='xy', scale=1)
plt.ylim(0,5)
plt.xlim(0,5)
plt.show()

Upvotes: 1

Related Questions