Signup/Sign In

Matplotlib Quiver Plot - quiver() Function

In this tutorial, we will cover the Quiver Plot using Matplotlib Library.

To plot the 2D field of arrows, we use the Quiver plot in the matplotlib library.

  • This plot mainly helps in displaying the velocity vectors as arrows having components (u,v) at the points (x,y).
  • The Quiver plots are useful for Electrical engineers to visualize electrical potential and for Mechanical engineers to show stress gradients.

Creating Matplotlib Quiver Plot

In order to create a Quiver Plot the ax.quiver() function is used.

The required syntax to use this function is as follows:

ax.quiver(x_pos, y_pos, x_dir, y_dir, color) 

Following are the parameters of this function, which you can see above in the syntax:

  • x_pos and y_pos

    These two parameters of the function are used to indicate the starting position of the arrows.

  • x_dir and y_dir

    These two parameters of the function are used to indicate the directions of the arrows.

  • color

    This parameter is used to specify the color of the Quiver Plot.

Let us now dive into some examples related to this.

Simple Quiver Plot Example:

In the example given below, we will cover how to plot a Quiver plot with a single arrow:

import numpy as np 
import matplotlib.pyplot as plt 

x_pos = 0
y_pos = 0
x_direct = 1
y_direct = 1

fig, ax = plt.subplots(figsize = (10, 7)) 
ax.quiver(x_pos, y_pos, x_direct, y_direct) 
ax.set_title('Quiver plot with a single arrow') 

plt.show() 

Here is the output:

single arrow quiver plot matplotlib example

Two Arrow Quiver Plot Example:

In the example given below, we will cover how to plot a Quiver plot with two arrows:

import numpy as np 
import matplotlib.pyplot as plt 

x_pos = [0, 0] 
y_pos = [0, 0] 
x_direct = [1, 0] 
y_direct = [1, -1] 

fig, ax = plt.subplots(figsize = (12, 7)) 
ax.quiver(x_pos, y_pos, x_direct, y_direct,scale = 8) 

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

plt.show() 

Here is the output:

two arrow quiver plot matplotlib example

Time for Live Example!

Now we will cover a live example where we will draw the Quiver plot using meshgrid:

In the above live example, you can even change the code and try running other code too, to see different quiver plot outputs.



About the author:
Aspiring Software developer working as a content writer. I like computer related subjects like Computer Networks, Operating system, CAO, Database, and I am also learning Python.