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(2)
ax = fig.add_subplot(111)
ax.quiver(0,0, 1, 1, units = 'xy', scale = 1)
plt.xticks(range(-5,6))
plt.yticks(range(-5,6))
plt.grid()
I would expect the arrow to go from (0,0)
to (1,1)
, but the result is slightly off from that:
Similarly, I try and plot an arrow for vector (0,3)
and the resulting arrow seems to be for vector (0,3.5)
...
My assumption is that this has something to do with the kwargs 'units'
, 'scale'
, 'angles'
, & 'scale_units'
. I've read the docs on them but don't fully understand how they work. A sunday school explanation would be greatly appreciated!
Quiver plot is basically a type of 2D plot which shows vector lines as arrows. This type of plots are useful in Electrical engineers to visualize electrical potential and show stress gradients in Mechanical engineering.
quiver( U , V ) plots arrows with directional components specified by U and V at equally spaced points. If U and V are vectors, then the x-coordinates of the arrows range from 1 to the number of elements in U and V , and the y-coordinates are all 1.
The Quiver object does not have a savefig method and so fig. savefig will not work. However, you should just be able to swap the the fig. savefig command with plt.
Create a matrix of 2×3 dimension. Create an origin point, from where vecors could be originated. Plot a 3D fields of arrows using quiver() method with origin, data, colors and scale=15.
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()
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()
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With