Quiver Plot in Matplotlib
Last Updated :
28 Apr, 2025
Quiver plot is basically a type of 2D plot which shows vector lines as arrows. This type of plots are useful in Electrical engineers to visualize electrical potential and show stress gradients in Mechanical engineering.
Creating a Quiver Plot
Let's start creating a simple quiver plot containing one arrow which will explain how Matplotlib's ax.quiver() function works. The ax.quiver() function takes four arguments:
Syntax:
ax.quiver(x_pos, y_pos, x_dir, y_dir, color)
Here x_pos and y_pos are the starting positions of the arrow while x_dir and y_dir are the directions of the arrow.
The below plot contains one quiver arrow starting at x_pos = 0 and y_pos = 0.The direction of the arrow is pointing towards up and the right at x_dir = 1 and y_dir = 1.
Example:
Python3
# Import libraries
import numpy as np
import matplotlib.pyplot as plt
# Creating arrow
x_pos = 0
y_pos = 0
x_direct = 1
y_direct = 1
# Creating plot
fig, ax = plt.subplots(figsize = (12, 7))
ax.quiver(x_pos, y_pos, x_direct, y_direct)
ax.set_title('Quiver plot with one arrow')
# Show plot
plt.show()
Output :
Quiver Plot with two arrows
Let's add another arrow to the plot passing through two starting points and two directions. By keeping the original arrow starting at origin(0, 0) and pointing towards up and to the right direction(1, 1), and create the second arrow starting at (0, 0) pointing down in direction(0, -1).To see the starting and ending point clearly, we will set axis limits to [-1.5, 1.5] using the method ax.axis() and passing the arguments in the form of [x_min, x_max, y_max, y_min] . By adding an additional argument scale=value to the ax.quiver() method we can manage the lengths of the arrows to look longer and show up better on the plot.
Example:
Python3
# Import libraries
import numpy as np
import matplotlib.pyplot as plt
# Creating arrow
x_pos = [0, 0]
y_pos = [0, 0]
x_direct = [1, 0]
y_direct = [1, -1]
# Creating plot
fig, ax = plt.subplots(figsize = (12, 7))
ax.quiver(x_pos, y_pos, x_direct, y_direct,
scale = 5)
ax.axis([-1.5, 1.5, -1.5, 1.5])
# show plot
plt.show()
Output :
Quiver Plot using meshgrid
A quiver plot containing two arrows is a good start, but it is too slow and too long to add arrows to the quiver plot one by one.So to create a fully 2D surface of arrows we will use meshgrid() method of Numpy. First, create a set of arrays named X and Y which represent the starting positions of x and y respectively of each arrow on the quiver plot. The starting positions of x, y arrows can also be used to define the x and y components of each arrow direction. In the following plot u and v denote the array of directions of the quiver arrows and we will define the arrow direction based on the arrow starting point by using the equations below:
x_{direction} = cos(x_{starting \ point})
y_{direction} = sin(y_{starting \ point})
Example:
Python3
# Import libraries
import numpy as np
import matplotlib.pyplot as plt
# Creating arrow
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
# creating plot
fig, ax = plt.subplots(figsize =(14, 8))
ax.quiver(X, Y, u, v)
ax.xaxis.set_ticks([])
ax.yaxis.set_ticks([])
ax.axis([-0.3, 2.3, -0.3, 2.3])
ax.set_aspect('equal')
# show plot
plt.show()
Output :
Quiver Plot using gradient
Let us create a quiver plot which shows the gradient function.The np, gradient() method of Numpy can be used to apply the gradient function to each arrow's x, y starting position. The equation is used to create the following plot:
z = xe^{-x^2-y^2}
Example:
Python3
# Import libraries
import numpy as np
import matplotlib.pyplot as plt
# Creating arrows
x = np.arange(-2, 2.2, 0.2)
y = np.arange(-2, 2.2, 0.2)
# Creating gradient
X, Y = np.meshgrid(x, y)
z = X * np.exp(-X**2-Y**2)
dx, dy = np.gradient(z)
# Creating plot
fig, ax = plt.subplots(figsize =(9, 9))
ax.quiver(X, Y, dx, dy)
ax.xaxis.set_ticks([])
ax.yaxis.set_ticks([])
ax.set_aspect('equal')
# show plot
plt.show()
Output :
Coloring Quiver Plot
The ax.quiver() method of matplotlib library of python provides an optional attribute color that specifies the color of the arrow. The quiver color attribute requires the dimensions the same as the position and direction arrays.
Below is the code which modifies the quiver plots we made earlier:
Example 1:
Python3
# Import libraries
import numpy as np
import matplotlib.pyplot as plt
# Defining subplots
fig, (ax1, ax2) = plt.subplots(1, 2, figsize =(14, 8))
# first subplot
# Creating arrows
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
# Defining color
color = np.sqrt(((v-n)/2)*2 + ((u-n)/2)*2)
# Creating plot
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')
# second subplot
# Creating arrows
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)
# Creating plot
ax2.quiver(X, Y, dx, dy, color)
ax2.xaxis.set_ticks([])
ax2.yaxis.set_ticks([])
ax2.set_aspect('equal')
ax2.set_title('gradient')
# show figure
plt.tight_layout()
plt.show()
Output :
Example 2: Building a quiver plot of function \vec{F} which defines the 2D field having four vertices as shown in the below plot:
\vec{F} = sin(x)cos(y) \ \hat{i} -cos(x)sin(y) \ \hat{j}
Python3
# Import libraries
import numpy as np
import matplotlib.pyplot as plt
# Creating 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.sin(X)*np.cos(Y)
v = -np.cos(X)*np.sin(Y)
# Defining color
color = np.sqrt(((dx-n)/2)*2 + ((dy-n)/2)*2)
# Creating plot
fig, ax = plt.subplots(figsize =(14, 9))
ax.quiver(X, Y, u, v, color, alpha = 1)
ax.xaxis.set_ticks([])
ax.yaxis.set_ticks([])
ax.axis([0, 2 * np.pi, 0, 2 * np.pi])
ax.set_aspect('equal')
# show plot
plt.show()
Output :
Similar Reads
Matplotlib.pyplot.quiver() in Python
Matplotlib is a library of Python bindings which provides the user with a MATLAB-like plotting framework. Matplotlib can be used in Python scripts, the Python and IPython shell, web application servers, and various graphical user interface toolkits like Tkinter, awxPython, etc. Matplotlib.pyplot.qui
2 min read
Matplotlib.pyplot.quiverkey() in Python
Matplotlib is a library in Python and it is numerical - mathematical extension for NumPy library. Pyplot is a state-based interface to a Matplotlib module which provides a MATLAB-like interface. There are various plots which can be used in Pyplot are Line Plot, Contour, Histogram, Scatter, 3D Plot,
2 min read
Pairplot in Matplotlib
Pair Plot is a type of chart that shows how different numbers in a dataset relate to each other. It creates multiple small scatter plots, comparing two variables at a time. While Seaborn has a ready-made pairplot() function to quickly create this chart, Matplotlib allows more control to customize ho
4 min read
Matplotlib.axes.Axes.quiver() in Python
Matplotlib is a library in Python and it is numerical - mathematical extension for NumPy library. The Axes Class contains most of the figure elements: Axis, Tick, Line2D, Text, Polygon, etc., and sets the coordinate system. And the instances of Axes supports callbacks through a callbacks attribute.
3 min read
Matplotlib Pyplot API
Data visualization plays a key role in data science and analysis. It enables us to grasp datasets by representing them. Matplotlib, a known Python library offers a range of tools, for generating informative and visually appealing plots and charts. One outstanding feature of Matplotlib is its user-ve
4 min read
Half Violin Plot in Matplotlib
A half violin plot is a variation of the traditional violin plot where only one half of the violin (either left or right) is displayed. This type of plot is particularly useful when you want to focus on one side of the distribution while keeping the plot clean and easier to read. Half violin plots a
5 min read
Line plot styles in Matplotlib
Line plots are important data visualization elements that can be used to identify relationships within the data. Using matplotlib.pyplot.plot() function we can plot line plots. Styling tools in this helps us customize line plots according to our requirements which helps in better representations. Li
3 min read
Matplotlib.pyplot.title() in Python
The title() method in the Matplotlib module is used to specify the title of the visualization depicted and display the title using various attributes. In this article, we will learn about this function with the help of examples.Syntax: matplotlib.pyplot.title(label, fontdict=None, loc='center', pad=
3 min read
How to use matplotlib plot inline?
Matplotlib is a Python library that helps in drawing graphs. It is used in data visualization and graph plotting. Matplotlib Plot Inline is a package that supports Matplotlib to display plots directly inline and save them to notebooks. In this article, we'll cover the following:Â What is Matplotlib I
3 min read
Matplotlib.axes.Axes.quiverkey() in Python
Matplotlib is a library in Python and it is numerical - mathematical extension for NumPy library. The Axes Class contains most of the figure elements: Axis, Tick, Line2D, Text, Polygon, etc., and sets the coordinate system. And the instances of Axes supports callbacks through a callbacks attribute.
2 min read