I'm trying to understand how the quiver function in the NumPy module works. Supposedly it allows to visualize graphically the values of two arrays, for example horizontal and vertical velocities. I have the following very simple example, but I show it just to see if you can help me to find out what I'm not doing well:
x = np.linspace(0,1,11)
y = np.linspace(1,0,11)
u = v = np.zeros((11,11))
u[5,5] = 0.2
plt.quiver(x, y, u, v)
The code produces the following figure:
As you can see, the arrow is not an arrow, but a line and it is longer than 0.2. My intention is to get an arrow of length 0.2 and I thought I could do it using quiver
. Is it possible? Or should I better use another command?
A quiver plot displays the velocity vectors as arrows with components (u,v) at the points (x,y). The above command plots vectors as arrows at the coordinates specified in each corresponding pair of elements in x and y.
Matplotlib is designed to be as usable as MATLAB, with the ability to use Python and the advantage of being free and open-source. Each pyplot function makes some change to a figure: e.g., creates a figure, creates a plotting area in a figure, plots some lines in a plotting area, decorates the plot with labels, etc.
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.
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.
matplotlib quiver does auto scaling. Set the scale to 1
to get your 0.2 units in x an y:
x = np.linspace(0,1,11)
y = np.linspace(1,0,11)
u = v = np.zeros((11,11))
u[5,5] = 0.2
plt.quiver(x, y, u, v, scale=1)
If you don't set scale
, matplotlib uses an auto scaling algorithm based on the average vector length and the number of vectors. Since you only have one vector with a length greater zero, it becomes really big. Adding more vectors makes the arrows successively smaller.
To have equal x and y extensions of your arrow a few more adjustments are needed:
x = np.linspace(0,1,11)
y = np.linspace(1,0,11)
u = v = np.zeros((11,11))
u[5,5] = 0.2
plt.axis('equal')
plt.quiver(x, y, u, v, scale=1, units='xy')
Both axes need to be equal and the units need to be set to xy
.
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