Matplotlib Quiver Plot

PythonPythonBeginner
Practice Now

This tutorial is from open-source community. Access the source code

Introduction

This lab will guide you through how to create a quiver plot using Matplotlib in Python. A quiver plot displays vector fields as arrows. It is useful in visualizing fluid flows, electric and magnetic fields, and other types of vector fields.

VM Tips

After the VM startup is done, click the top left corner to switch to the Notebook tab to access Jupyter Notebook for practice.

Sometimes, you may need to wait a few seconds for Jupyter Notebook to finish loading. The validation of operations cannot be automated because of limitations in Jupyter Notebook.

If you face issues during learning, feel free to ask Labby. Provide feedback after the session, and we will promptly resolve the problem for you.


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL matplotlib(("`Matplotlib`")) -.-> matplotlib/BasicConceptsGroup(["`Basic Concepts`"]) matplotlib(("`Matplotlib`")) -.-> matplotlib/SpecializedPlotsGroup(["`Specialized Plots`"]) python(("`Python`")) -.-> python/DataStructuresGroup(["`Data Structures`"]) python(("`Python`")) -.-> python/ModulesandPackagesGroup(["`Modules and Packages`"]) python(("`Python`")) -.-> python/DataScienceandMachineLearningGroup(["`Data Science and Machine Learning`"]) matplotlib/BasicConceptsGroup -.-> matplotlib/importing_matplotlib("`Importing Matplotlib`") matplotlib/BasicConceptsGroup -.-> matplotlib/figures_axes("`Understanding Figures and Axes`") matplotlib/SpecializedPlotsGroup -.-> matplotlib/quiver_plots("`Quiver Plots`") python/DataStructuresGroup -.-> python/tuples("`Tuples`") python/ModulesandPackagesGroup -.-> python/importing_modules("`Importing Modules`") python/DataScienceandMachineLearningGroup -.-> python/numerical_computing("`Numerical Computing`") python/DataScienceandMachineLearningGroup -.-> python/data_visualization("`Data Visualization`") subgraph Lab Skills matplotlib/importing_matplotlib -.-> lab-48894{{"`Matplotlib Quiver Plot`"}} matplotlib/figures_axes -.-> lab-48894{{"`Matplotlib Quiver Plot`"}} matplotlib/quiver_plots -.-> lab-48894{{"`Matplotlib Quiver Plot`"}} python/tuples -.-> lab-48894{{"`Matplotlib Quiver Plot`"}} python/importing_modules -.-> lab-48894{{"`Matplotlib Quiver Plot`"}} python/numerical_computing -.-> lab-48894{{"`Matplotlib Quiver Plot`"}} python/data_visualization -.-> lab-48894{{"`Matplotlib Quiver Plot`"}} end

Import Libraries

We need to import the numpy and matplotlib libraries to create a quiver plot.

import numpy as np
import matplotlib.pyplot as plt

Create Data

We need to create the X and Y coordinates using the np.meshgrid() function. Then, we create the U and V arrays that represent the vector fields.

X = np.arange(-10, 10, 1)
Y = np.arange(-10, 10, 1)
U, V = np.meshgrid(X, Y)

Create the Quiver Plot

We can create the quiver plot using the ax.quiver() function. We pass in the X, Y, U, and V arrays as parameters.

fig, ax = plt.subplots()
q = ax.quiver(X, Y, U, V)

Create the Quiver Key

We can add a quiver key to the plot to show the scale of the arrows. We use the ax.quiverkey() function to add the key. We pass in the q object, the X and Y position of the key, the length of the arrow, the label for the key, and the position of the label.

ax.quiverkey(q, X=0.3, Y=1.1, U=10,
             label='Quiver key, length = 10', labelpos='E')

Display the Plot

We can display the plot using the plt.show() function.

plt.show()

Summary

In this lab, we learned how to create a quiver plot using Matplotlib in Python. We started by importing the necessary libraries, creating the data, and then creating the quiver plot. Finally, we added a quiver key to the plot and displayed it.

Other Python Tutorials you may like