In this article, we are going to discuss the methods of how to create quiver graphs in Python.

## Draw Quiver Plot having one arrow:

In this step, we are going to create a quiver plot that has one arrow and we will observe the working of the matplotlib.ax.quiver() function.

import numpy as np

x = 2

y = 2

u = 3

v = 3

fig, ax = plt.subplots(figsize = (5, 5))

ax.quiver(x, y, u, v)

ax.set_title('Quiver plot')

plt.show()

To create the quiver plots, we must acquire the required libraries: matplotlib and NumPy. For creating the arrow, we declare some variables and give them random values. These variables show the position and direction of the arrow. Further, we make an object that shows the size of the figure.

We call the function quiver(). This function contains four parameters. The parameters ‘x’ and ‘y’ show the starting points of the drawn arrow. The arrow’s directives are shown by the parameters ‘u’ and ‘v.’ In addition to this; we call the set.title() function, which shows the label of the quiver plot. In the end, we can display the quiver plot using the plt.show() function.

This graph has one quiver arrow in the output that starts at the ‘x’ = 2 and ‘y’= 2. At ‘u’ = 3 and ‘v’ = 3, the direction of the drawn arrow is upward and to the right.

## Draw Quiver Plot having two arrows:

Now we have to insert another arrow into the graph. We can accomplish this by providing two arrow initial points and two directives of the arrows.

import numpy as np

fig, ax = plt.subplots()

x = [1, 1]

y = [1, 1]

u = [2, 2]

v = [2, -2]

ax.quiver(x, y, u, v,

scale=8)

ax.axis([-5, 5, -5, 5])

plt.show()

After integrating the libraries matplotlib.pyplot as plt and NumPy as np, we call the function plt.subplots() functions to create the graph. Then we declare two arrays that contain the initial points of the arrows. Meanwhile, we store these arrays in ‘x’ and ‘y’ variables.

Similarly, we declare the other two arrays that hold the arrows’ direction and assign them to the ‘u’ and ‘v’ variables. We call the ax.quiver() function now. In the above code, this function has 4 parameters, including starting position of the arrows and the direction of the arrows. But in this scenario, an additional parameter ‘scale’ is provided to the ax.quiver() function. This parameter is used to adjust the dimension of the arrows.

By decreasing the value of the ‘scale’ argument, we can draw a large arrow. We can adjust the initial and termination points of the arrows by defining the limits to the ax.axis() function. We provide the parameters so that the first parameter shows the minimum value of ‘x’; the second parameter shows the maximum value of ‘x’; the third parameter shows the minimum value of ‘y’, and the last parameter shows the maximum value of ‘y’.

Now the graph is displayed by using the plt.show () function.

We get two arrows in this graph. One arrow has a starting position (1, 1), pointing in the right direction upward. Similarly, the other arrow has a starting position (1, 1), and it is also pointing in the right direction downward.

## Draw Quiver Plot by use of Mesh Grid:

The subsequent program demonstrates how to show a quiver plot with the help of a mesh grid:

import numpy as np

x = np.arange(0, 2.4, 0.3)

y = np.arange(0, 2.4, 0.3)

X, Y = np.meshgrid(x, y)

u = np.cos(X)*Y

v = np.sin(Y)*Y

fig, ax = plt.subplots(figsize =(12, 10))

ax.quiver(X, Y, u, v)

ax.xaxis.set_ticks([])

ax.yaxis.set_ticks([])

ax.axis([-0.2, 2.2 -0.2, 2.2])

ax.set_aspect('equal')

plt.show()

At the program’s start, we import two packages, matplotlib.pyplot as plt and NumPy as np. Here we create the initial position by using the NumPy library. We declare two arrays and assign these arrays to the variable, respectively.

Now we call the meshgrid() function of the NumPy library. This function contains two parameters. Now, we make two new variables. These variables store the points which show the direction of the arrows. We have to create a plot using the plt.subplot() function. We can adjust the size of the plot with this function.

Meanwhile, we apply the ax.quiver() method. And this method holds four arguments, including the starting position points of arrows and the direction points of arrows. We declare two functions ax.axis.set_ticks([ ]) and ax.yaxis.set_ticks([ ]) which eliminates the tick signs from x-axis and y-axis.

We can do this by using an ax.axis(). Here we modify the axis limits. In the end, before displaying the graph, we define the ax.set_aspect() function. This function determines the plot’s perspective ratio.

In this case, we obtain this type of graph by using the Mesh grid method. This graph shows a quiver plot having multiple arrows.

## Specify the color to Quiver Plot:

The matplotlib library’s ax.quiver() function contains an additional parameter color that defines the hue of the arrow. The contours of the quiver color parameter must be the same as the measurements of the location and directional elements.

import numpy as np

fig, (ax1, ax2) = plt.subplots(1, 2, figsize =(10, 5))

x = np.arange(0, 2.0, 0.4)

y = np.arange(0, 2.0, 0.4)

X, Y = np.meshgrid(x, y)

u = np.cos(X)*Y

v = np.sin(y)*Y

n = -4

color = np.sqrt(((v-n)/2)*2 + ((u-n)/2)*2)

ax1.quiver(X, Y, u, v, color, alpha = 1.0)

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('First plot')

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 = -4

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('Second plot')

plt.tight_layout()

plt.show()

We introduce the matplotlib.pyplot as plt and NumPy as np libraries. In the next step, we are going to define subplots and adjust the size of the graph. For drawing the first subplot, we create arrows using the NumPy package. The initial positions and directions of the arrows are defined for the arrows of the first subplot.

Further, we set the color of the arrows by calling the function. We create the graph by applying the quiver() function. Here we assigned the color of the plot. We specified the title of the first subplot by calling the set_title() function.

Now let’s go for the second subplot. To create arrows for the second subplot, we specify the initial positions and point for directions of the arrows. We create a gradient between both subplots. We define the color for the second subplot. We create the subplot by using the quiver() function. We decide the color of the arrows of the second subplot.

In the end, we show the graph by calling the plt.show() method.

## Conclusion:

We utilized matplotlib and Python to create a variety of new quiver graphs in this article. Matplotlib’s pyplot package includes quiver graphs. The positional points of x and y and the directions of arrows are the parameters for the ax.quiver() function. The meshgrid() method in Numpy can generate such positional modules. Further, we introduced an additional hue in quiver graphs by calling the ax.quiver() function with a new parameter ‘color.’