Quiver Plot in Matplotlib in Python

In this tutorial, we will be looking into Quiver Plot in Matplotlib in Python.

We will be using matplotlib which is an open-source data visualization library. Also, it is used to visualize data in many forms for example in static visualizations, interactive visualizations or animated visualization.

What is Quiver plot?

Additionally, within Matplotlib, there are various types of plots, and one of them is the Quiver plot.

Moreover, a quiver plot is a 2D plot. We use them when working with vectors.

It represents velocity vectors as arrows. The direction of the arrow signifies the direction of the vector, while the length of the arrow indicates the magnitude of the vector.

Below are a few examples of Quiver plot.

Quiver Plot in Matplotlib in Python

You may be wondering if it would be very difficult to plot such a beautiful yet challenging graph.

Don’t worry, as always, the Python savior is here to help.

At this time, Python has sent Matplotlib to help us.

Let’s dive in to find out how Matplotlib is going to help us plot these types of graphs.

Implementation: Quiver Plot in Matplotlib

Before we do anything, first we need to install Matplotlib to get its help.

pip install matplotlib

Once installation is complete, we will import important libraries for plotting.

import numpy as np
import matplotlib.pyplot as plt

Sometimes there may be calculations involved, hence we are importing numpy for our help.

Once we finish the important parts, let’s write the main code for plotting.

Furthermore, we will be plotting wind velocity.

Step 1: We will define vector fields. The code will be:

x = np.linspace(-2, 2, 10)
y = np.linspace(-2, 2, 10)
X, Y = np.meshgrid(x, y)

Here we are creating a grid to display our plot. The x-axis will have numbers from -2 to 2 divided into 10 parts. Similarly, the y-axis will also have the same divisions.

If we plot this grid, we will get a plot like the one shown below.

Quiver Plot in Matplotlib

Step 2: Once, we have the grid the next step will be defining the vector field. The code will be:

def vector_field(x, y):
    u = -y
    v = x
    return u, v

In the above code we are just doing simple rotation.

Step 3: Next, we will calculate the vector field at each grid point. The code will be:

U, V = vector_field(X, Y)

Step 4: Now comes the main part that is creating the quiver plot. The code will be:

plt.quiver(X, Y, U, V, scale=20, color='blue')

Here:

X, Y: grid coordinates where the vectors are located. These are typically created using numpy.meshgrid to generate a grid of points.

U, V: components of the vectors at each grid point. These components are specified as 2D arrays of the same shape as X and Y.

scale: It controls the length of the arrows in the plot. If not specified, arrows are autoscaled to fit within the plot.

color: The color of the arrows.

Step 5: Now we will label and display the plot. The code is as follows:

plt.xlabel('X-axis')
plt.ylabel('Y-axis')
plt.title('Quiver Plot of a Wind Velocity')

plt.show()

Output:

Wind velocity Matplotlib

Furthermore, you can also read:

How to use gradient color in matplotlib in Python

Plotting Equations with Python in Matplotlib

Bye! See you next time with a new topic.

Leave a Reply

Your email address will not be published. Required fields are marked *