Matplotlib - Quiver Plot



A quiver plot is a type of graph used to visualize vector fields, which represent both direction and magnitude. In simple terms, it shows arrows on a grid, where each arrow represents a vector pointing in a specific direction with a certain length.

  • Imagine you are studying the wind patterns over a region. You want to visualize both the direction and strength of the wind at various points across the area.
  • You can use the quiver plot for this visualization, where each arrow in the plot represents the direction the wind is blowing, while the length of the arrow indicates the wind speed.

By looking at the quiver plot, you can quickly see how the wind flows across the region and identify areas with stronger or weaker winds −

Quiver Plots

Quiver Plot in Matplotlib

In Matplotlib, a quiver plot is a visualization that represents vector fields using arrows. We can use the quiver() function from the 'pyplot' module to create arrows for displaying a quiver plot in Matplotlib.

This function takes the X and Y coordinates (and Z for a three-dimensional object) of a vector field, and represents its direction and magnitude as an arrow(s).

Lets start by drawing a basic quiver plot.

Basic Quiver Plot

A basic quiver plot in Matplotlib visualizes a vector in a two-dimensional space. It consists of an arrow, where the length and direction of the arrow indicate the magnitude and direction of a vector.

Example

In the following example, we are creating a basic quiver plot to represent a vector as an arrow. We first specify the starting position of the arrow by 'x_pos' and 'y_pos' which represent the X and Y coordinates, respectively.

The direction of the arrow is determined by 'x_direct' and 'y_direct' for the x-axis and the y-axis, respectively. The resultant plot shows an arrow originating at (1, 1) and pointing in a certain direction −

import numpy as np
import matplotlib.pyplot as plt

# Defining the arrows
x_pos = 1
y_pos = 1
x_direct = 2
y_direct = 1

# Creating the plot
fig, ax = plt.subplots(figsize=(12, 7))

# Plotting the quiver
ax.quiver(x_pos, y_pos, x_direct, y_direct, angles='xy', scale_units='xy', scale=1, color='green', width=0.02, label='Arrow')

# Setting axis limits
ax.set_xlim([0, 4])
ax.set_ylim([0, 3])
ax.set_title('Basic Quiver Plot')

# Displaying the plot
plt.show()

Output

Following is the output of the above code −

Basic Quiver Plot

Meshgrid Quiver Plot

In Matplotlib, a meshgrid quiver plot visually represents evenly spaced vectors as distinct arrows. The magnitude and direction of the entire vector field can be calculated by summing the magnitudes and directions of individual arrows.

Example

In here, we are creating a meshgrid quiver plot of multiple vector fields by arranging the arrows on a 2D grid. The X and Y coordinates range from 0 to 4, with a step count of 0.3. The direction of each vector field is calculated by parameterizing the X and Y coordinates with the sine and cosine angles, respectively. This results in a plot where multiple vector fields are represented as different arrows −

import numpy as np
import matplotlib.pyplot as plt
 
# Creating the arrow
x = np.arange(0, 4, 0.3)
y = np.arange(0, 4, 0.3)
X, Y = np.meshgrid(x, y)
u = np.sin(X)*Y
v = np.cos(Y)*Y
 
# Creating the plot
fig, ax = plt.subplots(figsize =(14, 8))

# Plotting the quiver
ax.quiver(X, Y, u, v)

# Setting axis limits 
ax.axis([-0.3, 2.3, -0.3, 2.3])
ax.set_aspect('equal')
ax.set_title('Meshgrid Quiver Plot')
 
# Displaying the plot
plt.show()

Output

Output of the above code is as follows −

Meshgrid Quiver Plot

Gradient Quiver Plot

A gradient quiver plot in Matplotlib represents a visualization of a vector field with a changing gradient in a 2D space. The gradient indicates the strength of the vector field, represented by the number of arrows. The more arrows there are in a particular region, the stronger the vector field is in that area.

Example

The following example creates a gradient quiver plot to show the change in strength of a vector field. The X and Y coordinates range from -2 to 2, with a step count of 0.1. The gradient is calculated as the change in direction of X and Y values, represented by 'dx' and 'dy' −

import numpy as np
import matplotlib.pyplot as plt
  
# Creating the arrows
x = np.arange(-2, 2, 0.1)
y = np.arange(-2, 2, 0.1)
 
# Creating a gradient function
X, Y = np.meshgrid(x, y)
z = np.sin(X) * np.cos(Y)
dx, dy = np.gradient(z)
 
# Creating the plot 
fig, ax = plt.subplots(figsize=(9, 9))

# Plotting the quiver
ax.quiver(X, Y, dx, dy)

# Setting axis limits
ax.set_aspect('equal')
ax.set_title('Gradient Quiver Plot')
 
# Displaying the plot
plt.show()

Output

After executing the above code, we get the following output −

Gradient Quiver Plot

Colored Quiver Plot

In Matplotlib, a colored quiver plot is an advance version of the basic quiver plot where the vector field is represented by arrows of different colors. The color of the arrows depends on specific parameters such as distance from origin, strength, or direction.

Example

Now, we are creating a colored quiver plot to represent a vector field. We set the starting position of the arrows withing the range 0 to 2 and parameterize the direction based on the X and Y values.

Then, we assign a color to each arrow based on its distance from the center of the field. Arrow closer to the center are darker in color while arrows further away are lighter in color −

import numpy as np
import matplotlib.pyplot as plt

# Creating the arrow 
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.cos(X) * np.sin(Y)
v = np.sin(X) * np.cos(Y)

# Defining the color
color = np.sqrt(u**2 + v**2)

# Creating the plot
fig, ax = plt.subplots(figsize=(14, 9))

# Plotting the quiver
ax.quiver(X, Y, u, v, color, alpha=1, cmap='viridis')

# Setting axis limits
ax.axis([0, 2 * np.pi, 0, 2 * np.pi])
ax.set_aspect('equal')
ax.set_title('Colored Quiver Plot')

# Displaying plot
plt.show()

Output

The output obtained is as shown below −

Colored Quiver Plot
Advertisements