matplotlib quiver plot Tutorail with example

The quiver plot is the two dimensional plot. And will show the vector lines as arrows. Quiver plots are very useful in electrical and mechanical engineering to visualize potential and show stress. The example demonstrates using quiver to plot wind speed contours and wind direction arrows from wind vector component input data.  The vector components are located in space in this case. For second plot, the data is normalized to produce arrows with a uniform size on the plot.

Quiver plot:-

Plot the quiver arrow starting from x_pos=0, y_pos=0.
The arrows direction and pointing up and right x_direct=1, y_direct=1.
Using quiver to plot wind speed that counters and wind the direction arrows from component collocated in space.
For second plot data used for arrows is normalized to produce arrows with uniform size on the plot.
The quiver plot will display the velocity vectors as arrows with component (u, v) at points as (x, y).
The vector is defined as component u(1),v(1) and then display at point x(1),y(1).
The (x, y, u, v) as arrows coordinates in specified pair of elements in x, y.
The x, y, v and v must be of the same size and has corresponding position and the velocity of components.
The quiver (..Line spec, ‘filled’) will fill the marker symbol at origin of vectors.
Quiver will specify the property name and property value for quiver objects created by function.
The arrows are scaled not overlap but can scale them to be longer or shorter as you need.
The (u, v) will draw vector specified by u,v at equally spaced points in x-y plane.
The scale will scales the arrow to fit within grid and then stretches by factor scale.
If scale is 2 doubles the length and if scale is 0.5 halves length.
Use scale of 0 to plot the velocity of vectors without scaling automatic.
Now you can tune the length of arrows after drawn by using plot edit tool.
The quiver (...Line spec) will specify the line style, marker symbol and color using valid line spec.
Quiver will draw the markers at the origin of vectors.

The quiver (ax,) plots into the axes ax of into current axes (gca).
h=quiver (...) will return the quiver object.

Expanding x- and y-Coordinates:-

The mat lab will expand the x and y coordinates if they are not the matrices.
The expansion is equivalent to mesh grid to generate the matrices from vector.
[X, y]=mesh grid(x, y);
Quiver(x, y, u, v)
If the condition is true,
Length(x) =n and length(y) =m, where [m, n] =size (u) =size (v).
The vector x will correspond to column of u, v and y will correspond to row of u and v.

Parameters of Quiver plot:-

Following are the list of different parameters of Quiver plot.


x

1D or 2D array, sequence. The x coordinates of the arrow locations

y

1D or 2D array, sequence. The y coordinates of the arrow locations

u

1D or 2D array, sequence. The x components of the arrow vectors

v

1D or 2D array, sequence. The y components of the arrow vectors

c

1D or 2D array, sequence. The arrow colors

The quiver is used to plot the vector field in metplotlib.
Quier(x, y, u, v, **kw)
Matplotlib ax. quiver () method will work and method will take positional arguments.
Where x_pos and y_pos are arrows starting position and x_direct_direct are arrow directions.
Our plot contains quiver arrow at starting point x_pos=0, y_pos=0.
The arrow direction is pointing up and right x_direct=1, y_direct=1.
X: The x coordinates of arrow locations.
Y: The y coordinates of arrow locations.
U: The x components of arrow vectors.
V: The y components of arrow vectors.
The following code draws a simple quiver plot –

quiver plot

Example:-

Import matplotlib.pyplot as plt
Import numpy as np
x=np.array ((0))
y=np.array ((0))
u=np.array ((2))
v=np.array ((-2))
fig ax=plt.subplots()
q=ax. quiver (X,Y,U,V,units=’xy’,scale=1)
plt.grid ()
ax.set_aspect (‘equal’)
plt.xlimit (-5, 5)
plt.ylimit (-5, 5)
plt.title(‘how to plot a vector in matplitlib’,fontsize=10)
plt.savefig (‘how_to_plot_a_vector_in_matplotlib_fig3.png’, bbox_inches=’tight’)
#plt.show ()
Plt.close ()

 

 

Plot a simple vector field with quiver:-

matplotlib quiver

Example:-
Import matplotlib.pyplot as plt
Import numpy as np
x,y=np.meshgrid(np.arrange(-10,10,1).np.arrange(-10,10,1))
x_shape=x.shape
u=np.zeros (x_shape)
v=np.zeros (x_shape)
for i in range(x_shape[0]):
for j in range(x_shape[1]):
u[i,j]=1.0
v[i,j]=1.0
Fig, ax=plt.subplots ()
q=ax. quiver (x,y,u,v,units=’xy’,scale=2,color=’red’)
ax.set_aspect(‘equal’)
plt.xlim (-5, 5)
plt.ylim (-5, 5)
plt.title (‘how to plot vector field matpltlib?’, fontsize=10)
plt.savefig (‘how_to_plot_a_vector_field_in_matplotlib_fig1.png’, bbox_inches=’tight)
#plt.show()
Plt.close()

 

Keyword arguments :-

Units: [‘width’ | 'height' | 'dots' | 'inches' | 'x' | 'y' | ‘xy’]
 The arrow dimensions except for length are in multiples of this unit.

The arrows scale depends on the units for 'x' or ‘y’; the arrows get larger as one zooms in.
 For other units the arrow size is totally independent of the zoom state.
 For the ‘width or 'height' the arrow size will increase with the width and height of the axes.
 If the window is resized for 'dots' or 'inches' the resizing does not change the arrows.
Angles: [‘uv’ | 'xy' | array]
 The 'uv' is the arrow axis aspect ratio is 1, so that if U*==*V the orientation of the arrow on the plot is 45 degrees CCW from the horizontal axis (positive to the right).
With 'xy', the arrow points from (x, y) to (x+u, y+v) and use this for plotting a gradient field.
Scale: [None | float]
Data units per arrow length unit are a smaller scale parameter that makes the arrow longer.
A simple auto scaling algorithm is used and based on the average vector length and number of vectors.
The arrow length unit is by the scale unit’s parameter
Scale units: It is any of the unit’s options.
Example, if scale units is 'inches', scale is 2.0, and (u, v) = (1, 0), then vector will be 0.5 inches long.
If scale units will be 'width', then the vector is half the width of the axes.
If scale units is 'x' then the vector will be 0.5 of the x-axis unit.
 To plot the vectors in the x-y plane with u and v having the same units as x and y, we use "angles='xy', scale_units='xy', scale=1".

Width
The shaft width in arrow unit is default and depends on choice of units.
The number of vectors is a typical starting value is about 0.005 times the width of the plot.
headwidth: The headwidth is a scalar.
Head width as multiple of shaft width and the default is 3.
headlength: Also the headlength is scalar.
Head length as multiple of shaft width, default is 5
headaxislength: It is also the scalar one.
Head length at shaft intersection, default is 4.5.
minshaft: It is scalar.
The length below which arrow scales in the units of head length.
Don’t set this to less than 1, or small arrows will look terrible! Default is 1
minlength: It is scalar.
The minimum length as a multiple of shaft width is if an arrow length is less than this.
Pivot: [‘tail’ | 'mid' | 'middle' | ‘tip’]
The part of the arrow that is at the grid point the arrow will rotate about this point and hence the name is pivot.
Color: [color | color sequence]
This is a synonym for poly Collection face color kwarg

Example:-

Import matplotlib.pyplot as plt
Import numpy as np
From numpy import ma
X,y=np.meshgrid(np.arange(0,2*np.pi,.2),np.arange(0,2*np.pi,.2))
U=np.cos(x)
V=np.sin(y)
Plt.figure()
Plt.title(‘Arrows scale with plot width, not view
Q=plt.quiver (x, y, u, v, units=’width’)
Qk=plt.quiverkey(q,0.9,0.9,2,r’$\frac{m}{s}$’,labelpos=’E’,coordinates=’figure’)
Plt.scatter(x)
Plt.figure()
Plt.title(“pivot=mid;”every third arrow;units=’inches’”)
Q=plt.quiver(x[::3,::3],y[::3,::3],u[::3,::3],v[::3,::3],),pivot=’mid’,units=’inches’)
Qk=plt.quiverkey(q,0.9,0.9,1,r’$1\frac{m}{s}$’,labelpos=’E’,coordinates=figure’)
Plt.scatter(x[::3,::3],y[::3,::3],color=’r’,s=5)
Plt.figure()
Plt.title(“pivot=’tip’;scales with x view”)
M=np.hypot(u,v)
Q=plt.quiver (x, y, u, v, m, units=’x’, pivot=’tip’, width=0.022, scale=1/0.15)
Qk=plt.quiverkey(q,0.9,0.9,1,r’$1 \frac{m}{s}$’,labelpos=’E’,coordinates=’figure’)
Plt.scatter(X,Y,color=’k’,s=5)
Plt.show()

 

Output:-

quiver plot python

quiver plot python

python quiver plot

Additional Services : Refurbished Laptops Sales, Python Classes, Share Market Classes And SEO Freelancer in Pune, India