Matplotlib - Why is my quiver plot always off?

Viewed 207

I am trying to plot vectors using the quiver API in matplotlib.

I'm scratching my head because I am noticing the arrow tips don't end where they are supposed to.

For example, if I'm trying to plot a simple vector [2, 1], this is what I get.

enter image description here

Notice that the arrow is pointing a little past 2.0 in the x-direction and a little short of 1.0 in the y-direction.

Is this just an imperfection with the rendering? Or am I scaling something incorrectly?

Here is my code:

# Import libraries
import numpy as np
import matplotlib.pyplot as plt

# Vector origin location
X = [0]
Y = [0]

# Directional vectors
U = [2]
V = [1]

# Creating plot
plt.quiver(X, Y, U, V, color='b', units='xy', scale=1)
plt.title('Single Vector')

# x-lim and y-lim
plt.xlim(-2, 5)
plt.ylim(-2, 2.5)

# Show plot with gird
plt.grid()
plt.show()

UPDATE

I am using PyCharm as my environment. I noticed that when I resize the window the arrow tip moves. Could this be my problem? How do I anchor this down?

enter image description here

enter image description here

enter image description here

1 Answers

I found this in the docs, in case it can help: To plot vectors in the x-y plane, with u and v having the same units as x and y, use angles='xy', scale_units='xy', scale=1.

Adding it to quiver seems to solve it, no matter the size of the window.

Related