Skip to content Skip to sidebar Skip to footer

Understanding Matplotlib's Quiver Plotting

I'm trying to understand how plt.quiver() works. My issue is as follows: I plot a simple vector (1,1) as such: import numpy as np import matplotlib.pyplot as plt fig = plt.figure(

Solution 1:

If you adjust the aspect ratio of the figure to 1, the vectors are displayed to proper scale:

import numpy as np
import matplotlib.pyplot as plt

fig = plt.figure()
ax = fig.add_subplot(111)
ax.quiver((0,0), (0,0), (1,0), (1,3), units = 'xy', scale = 1)
plt.axis('equal')
plt.xticks(range(-5,6))
plt.yticks(range(-5,6))
plt.grid()
plt.show()

enter image description here

Solution 2:

You can try this code.

import matplotlib.pyplot as plt

fig = plt.figure(2)
ax = fig.add_subplot(111)
ax.quiver(0,0, 1, 1,angles='xy', scale_units='xy', scale = 1)
plt.xticks(range(-5,6))
plt.yticks(range(-5,6))
plt.grid()
plt.draw()
plt.show()

This is the output

Just remember that the first two arguments of quiver are the x and y coordinates of the tail of the vector, the next two are the lengths of the vector along x and y direction respectively. angle='xy' makes the arrow point from tail of the vector to its tip.

You can find out more about matplotlib.quiver at http://matplotlib.org/1.3.1/api/pyplot_api.html#matplotlib.pyplot.quiver

Post a Comment for "Understanding Matplotlib's Quiver Plotting"