A **matplotlib quiver** plot is basically something that helps in displaying the velocity vectors as arrows with the components (u, v) at the points (x, y).

To plot the coordinates specified above, we can use the following command in each corresponding pair of the elements present in x and y.

Contents

## Matplotlib Quiver Syntax

```
quiver (x, y, u, v)
```

## Matploitib Quiver Parameters

**x**: It represents the x-coordinates of the arrow location. It can be either a 1-D or a 2-D array or a sequence.**y**: It represents the y-coordinates of the arrow location. It can be either a 1-D or a 2-D array or a sequence.**u: **It represents the x-components of the arrow vector. It can be either a 1-D or a 2-D array or a sequence.**v:** It represents the y-components of the arrow vector. It can be either a 1-D or a 2-D array or a sequence.

*If x and y are 1-Dimensional but u and v are in 2-D, then x and y should be expanded to 2-D by using

`x,y= np.meshgrid(x,y)`

Here, the length of x and y must match the row and column dimensions of u and v.**c: **It shows the color of the arrow in the plot. This can also be a 1-D or a 2-D array or sequence. (optional)

Units: This is the unit in which the lengths of arrows are measured. It includes the ‘width’ or ‘height’ of the axis, the dpi(dots per inch) is measured in ‘dots,’ or ‘inches’ and ‘x,’ ‘y,’ ‘xy’ are measured in data units.

angles: {‘uv’, ‘xy’}**‘uv’** – the aspect ratio of the arrow id 1, which meach u=v, so the arrow orientation is at 45 degrees inclination in the anti-clockwise direction of the horizontal axis(from the positive side).**‘xy’** – These arrows point from (x,y) to (x+u,y+v)**scale:** It is always of float type. (optional)**scale_units:** It includes parameters like ‘width’, ‘height’, ‘dots’, ‘inches’, ‘x’, ‘y’, ‘xy’. (optional)

### More Advanced Parameters

**width:** The Width is measured in arrow units. The defaults depend on the choice of units, above, and the number of vectors. A typical starting value is about 0.005 times the width of the plot.**headwidth:** The Headwidth is the multiple of shaft width. The default value is 3 units.

headlength: The Headlength is shaft width. The default value is 5.**headaxislength:** The Headaxislength shaft intersection. Its default value is 4.5.

mineshaft: The mineshaft is defined as the length below which the arrow scales in units of head length. *NOTE: Never set this to less than 1, or small arrows will look terrible! Its default value is 1.*

**minlength:**It is the minimum length as a multiple of shaft width. If an arrow length is less than this, plot a dot (hexagon) of the same diameter instead. Its default value is 1.

**pivot:**This is the part of the arrow that is at the grid point. The arrow rotates about this point, hence it is called the name pivot.

**color:**This is a synonym for the PolyCollection face color kwarg. If C has been set, color does not affect.

## Basic Example of a Matplotlib Quiver Plot:

```
import matplotlib.pyplot as plt
import numpy as np
x,y = np.meshgrid(np.arange(-2,2,.2), np.arange(-2,2,.25))
z = x*np.exp(-x ** 2 - y ** 2)
v,u = np.gradient(z,.2,.2)
fig, ax = plt.subplots()
q = ax.quiver(x,y,u,v)
plt.show()
```

## Creating Quiver Plot

Quiver plot is a type of 2-D plot make up of vector lines in shape of arrows. These plots are mainly used to visualize gradients.

The basic syntax of a quiver plot is:

`ax.quiver(x_position, y_position, x_direction, y_direction, color)`

## Matplotlib Quiver Plot With 1 Arrow

The plot below shows one quiver arrow, which is starting from (0,0) and is pointing towards the up and the right side at (1,1).

```
#importing libraries
import numpy as np
import matplotlib.pyplot as plt
#creating arrow
x_position = 0
y_position = 0
x_direction = 1
y_direction = 1
#creating the quiver plot
fig, ax = plt.subplots(figsize = (12,7))
ax.quiver(x_position, y_position, x_direction, y_direction)
ax.set_title('A Quiver Plot with an arrow')
#Displaying the plot
ply.show()
```

## Quiver Plot With 2 Arrows

We saw one arrow plot, now lets add another arrow. Both the arrows are starting from the same point (0,0) and plotting one of them towards (1,1) and another towards (0,-1). Set the axis limits to (-1.5,1.5).

```
import numpy as np
import matplotlib.pyplot as plt
x_position = [0,0]
y_position = [0,0]
x_direction = [1,0]
y_direction = [1,-1]
fig, ax = plt.subplots(figsize = (12,7))
ax.quiver(x_position, y_position, x_direction, y_direction, scale = 5)
ax.axis([-1.5, 1.5, -1.5, 1.5])
plt.show()
```

## Matplotlib Quiver Plot using Meshgrid

The 2-D surface filled with arrows is created by using meshgrid() method of Numpy. To create this surface, we first need to create a set of arrays named X and Y, which will represent the starting positions of x and y of each arrow on the quiver plot, respectively. Here, we also have to denote u and v in the plot as an array of directions of the quiver arrows.

**Syntax**:

```
x_{direction} = cos(x_{starting \ point})
y_{direction} = sin(y_{starting \ point})
```

Example:

```
import numpy as np
import matplotlib.pyplot as plt
x_position = [0,2.2, 0.2]
y_position = [0,2.2, 0.2]
X,Y = np.meshgrid(x,y)
u=np.cos(X)*Y
v=np.sin(Y)*X
fig, ax = plt.subplots(figsize = (14,8))
ax.quiver(X, Y, u, v)
ax.xaxis.set_tricks([])
ax.yaxis.set_tricks([])
ax.axis([-0.3, 2.3, -0.3, 2.3])
ax.set_aspect('equal')
plt.show()
```

## Coloring Quiver Plot

This is an optional step that specifies the color of the arrow. This requires the dimensions which are same as that of position and direction arrays.

```
import numpy as np
import matplotlib.pyplot as plt
fig, (ax1, ax2) = plt.subplots(1, 2, figsize =(14, 8))
x = np.arange(0, 2.2, 0.2)
y = np.arange(0, 2.2, 0.2)
X, Y = np.meshgrid(x, y)
u = np.cos(X)*Y
v = np.sin(y)*Y
n = -2
color = np.sqrt(((v-n)/2)*2 + ((u-n)/2)*2)
ax1.quiver(X, Y, u, v, color, alpha = 0.8)
ax1.xaxis.set_ticks([])
ax1.yaxis.set_ticks([])
ax1.axis([-0.2, 2.3, -0.2, 2.3])
ax1.set_aspect('equal')
ax1.set_title('meshgrid function')
x = np.arange(-2, 2.2, 0.2)
y = np.arange(-2, 2.2, 0.2)
X, Y = np.meshgrid(x, y)
z = X * np.exp(-X**2 -Y**2)
dx, dy = np.gradient(z)
n = -2
//Defining color
color = np.sqrt(((dx-n)/2)*2 + ((dy-n)/2)*2)
ax2.quiver(X, Y, dx, dy, color)
ax2.xaxis.set_ticks([])
ax2.yaxis.set_ticks([])
ax2.set_aspect('equal')
ax2.set_title('gradient')
plt.tight_layout()
plt.show()
```

## Conclusion

This is how you use the quiver plot in matplotlib in Python. This serves as a very helpful tool in many phases.

Hope this helped.

Still have any doubts or questions, do let me know in the comment section below. I will try to help you as soon as possible.

*Happy Pythoning!*