Pieter Moens
Pieter Moens

Reputation: 341

Python - pyplot.quiver(X, Y, U, V) not plotting expected result

When attempting to plot vectors using the quiver method in the pyplot library, I receive an unexpected and incorrect result. While debugging, I printed out the required arrays to the console and received :

X = [0, 0, 0, 0, 40, 40, 40, 40, 80, 80, 80, 80, 120, 120, 120, 120] # X-coordinate
Y = [0, 40, 80, 120, 0, 40, 80, 120, 0, 40, 80, 120, 0, 40, 80, 120] # Y-coordinate
UN = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, -1, -1, -1, -1] # X-component
VN = [0, 0, 0, -1, 0, 0, 0, -1, 0, -1, -1, -1, 0, 0, -1, -1] # Y-component

The arrays above are plotted using the following snippet of code :

plot = plt.figure()
plt.quiver(X, Y, UN, VN, color='Teal', headlength=7)

plt.title('Quiver Plot, Single Colour')
plt.show(plot)

Output:

Quiver Plot

As you can see we expected the vectors to be max 1 unit long (0 or 1) and still we receive incorrect (longer) vectors on our plot.

Upvotes: 1

Views: 1506

Answers (1)

Kennet Celeste
Kennet Celeste

Reputation: 4771

You should use scale_units='xy' and scale=1 :

code:

import matplotlib.pyplot as plt

X = [0, 0, 0, 0, 40, 40, 40, 40, 80, 80, 80, 80, 120, 120, 120, 120] # X-coordinate
Y = [0, 40, 80, 120, 0, 40, 80, 120, 0, 40, 80, 120, 0, 40, 80, 120] # Y-coordinate
UN = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, -1, -1, -1, -1] # X-component
VN = [0, 0, 0, -1, 0, 0, 0, -1, 0, -1, -1, -1, 0, 0, -1, -1] # Y-component

plot = plt.figure()
plt.quiver(X, Y, UN, VN, color='Teal', scale_units ='x',scale=1)

plt.title('Quiver Plot, Single Colour')
plt.xlim(-10,130)
plt.ylim(-10,130)
plt.show(plot)

result:

enter image description here

They are there but they are actually pretty small. Here's the zoomed version on the top left one :

enter image description here

Upvotes: 2

Related Questions