 # How to plot a Quiver plot in Python?

In this tutorial, we’ll try to understand how to plot a quiver plot in Python with the help of some examples.

### What is a Quiver plot?

A quiver plot is basically a type of 2D plot which shows vector lines as arrows. These types of plots are used by Electrical engineers to visualize electrical potential and show stress gradients in Mechanical engineering.

We can plot a quiver plot in Python using `axes.quiver` method in matplotlib.

## Quiver Plot using `matpltolib.Axes.quiver()` method

We can plot a Quiver plot in Python using the `Axes.quiver` method within the matplotlib module. The following is the syntax –

Basic Syntax:

`axes.quiver([x, y], u, v, [c], **kwargs)`

Parameters:

• 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

For more details, about the parameters, refer this.

Now let us try to understand the above method using some examples.

📚 Data Science Programs By Skill Level

Introductory

Intermediate ⭐⭐⭐

🔎 Find Data Science Programs 👨‍💻 111,889 already enrolled

Disclaimer: Data Science Parichay is reader supported. When you purchase a course through a link on this site, we may earn a small commission at no additional cost to you. Earned commissions help support this website and its team of writers.

### Example 1 – Quiver plot with a single arrow

```import matplotlib.pyplot as plt
import numpy as np

#generating the figure and axes
fig, ax = plt.subplots()

#defining the parameters
x = 0
y = 0
u = 1
v = 1

#plotting the Quiver plot
ax.quiver(x,y,u,v)
plt.show()```

Output:

The steps followed in the above examples are:

• Import the modules
• create a figure and axes using `pyplot.subplots` (refer this).
• Define the parameters
• Plot the Quiver plot using `axes.quiver` method (refer this).

### Example 2 – Quiver plot with two arrows

```import matplotlib.pyplot as plt
import numpy as np

#generating the figure and axes
fig, ax = plt.subplots()

#defining the parameters
x = [0, -0.5]
y = [0, 0.5]
u = [1, 0]
v = [1, -1]

#plotting the Quiver plot
ax.quiver(x,y,u,v)
plt.show()```

Output:

The steps followed in the above examples are:

• Import the modules
• create a figure and axes using `pyplot.subplots` (refer this).
• Define the parameters
• Plot the Quiver plot using `axes.quiver` method (refer this).

### Example 3 – Quiver plot using a meshgrid

```import matplotlib.pyplot as plt
import numpy as np

#generating the figure and axes
fig, ax = plt.subplots()

#defining the parameters
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

#plotting the Quiver plot
ax.quiver(x,y,u,v)
plt.show()```

Output:

The steps followed in the above examples are:

• Import the modules
• create a figure and axes using `pyplot.subplots`
• Define the parameters
• generate X,Y values using `numpy.arange` (refer this)generate a meshgrid using `numpy.meshgrid` (refer this).Then create the respective u and v values
• Plot the Quiver plot using `axes.quiver` method.

### Example 4 – Quiver plot along with gradient

```import matplotlib.pyplot as plt
import numpy as np

#generating the figure and axes
fig, ax = plt.subplots()

#defining the parameters

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)

#plotting the Quiver plot
ax.quiver(x,y,dx,dy)
plt.show()```

Output:

The steps followed in the above examples are:

• Import the modules
• create a figure and axes using `pyplot.subplots`
• Define the parameters
• generate X,Y values using `numpy.arange`generate a meshgrid using `numpy.meshgrid`Then create the respective dx and dy values using `numpy.gradient` (refer this)
• Plot the Quiver plot using `axes.quiver` method

### Example 5 – Quiver plot along with four vortices

```import matplotlib.pyplot as plt
import numpy as np

#generating the figure and axes
fig, ax = plt.subplots()

#defining the parameters

x = np.arange(0,2*np.pi+2*np.pi/20,2*np.pi/20)
y = np.arange(0,2*np.pi+2*np.pi/20,2*np.pi/20)

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

u = np.sin(X)*np.cos(Y)
v = -np.cos(X)*np.sin(Y)

#plotting the Quiver plot
ax.quiver(x,y,u,v)
plt.show()```

Output:

The steps followed in the above examples are:

• Import the modules
• create a figure and axes using `pyplot.subplots`
• Define the parameters
• generate X,Y values using `numpy.arange`generate a meshgrid using `numpy.meshgrid`Then create the respective u and v values.
• Plot the Quiver plot using `axes.quiver` method

### Example 5 – Quiver plot with colour

```import matplotlib.pyplot as plt
import numpy as np

#generating the figure and axes
fig, ax = plt.subplots()

#defining the parameters
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_array = np.sqrt(((v-n)/2)**2 + ((u-n)/2)**2)

#plotting the Quiver plot
ax.quiver(x,y,u,v, color_array, alpha=0.8)
plt.show()```

Output:

The steps followed in the above examples are:

• Import the modules
• create a figure and axes using `pyplot.subplots`
• Define the parameters
• generate X,Y values using `numpy.arange` generate a meshgrid using `numpy.meshgrid`Then create the respective u and v values
• Plot the Quiver plot using `axes.quiver` method by adding a color array for colours.

### Example 7 – Quiver plot in 3D

```import matplotlib.pyplot as plt
import numpy as np

#generating the figure and axes

#defining the parameters
X = np.arange(-0.8, 1, 0.2)
Y = np.arange(-0.8, 1, 0.2)
Z = np.arange(-0.8, 1, 0.8)
x, y, z = np.meshgrid(X,Y,Z)

u = np.sin(np.pi * x) * np.cos(np.pi * y) * np.cos(np.pi * z)
v = -np.cos(np.pi * x) * np.sin(np.pi * y) * np.cos(np.pi * z)
w = (np.sqrt(2.0 / 3.0) * np.cos(np.pi * x) * np.cos(np.pi * y) *
np.sin(np.pi * z))

#plotting the Quiver plot
ax.quiver(x, y, z, u, v, w, length=0.1, normalize=True)
plt.show()```

Output:

The steps followed in the above examples are:

• Import the modules
• Create an axes using `pyplot.figure.add_subplot(projection='3d')` for the 3D plot.
• Define the parameters
• generate X,Y,Z values using `numpy.arange` generate a meshgrid using `numpy.meshgrid`Then create the respective u,v and w values.
• Plot the Quiver plot using `axes.quiver` method

You might also be interested in –

• 