Matplotlib 3D Quiver Plot

PythonPythonBeginner
Practice Now

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

Introduction

In this lab, you will learn how to create a 3D quiver plot using Python Matplotlib. A quiver plot displays a vector field as arrows. The arrows point in the direction of the vectors and their length represents the magnitude of the vectors.

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/BasicConceptsGroup(["`Basic Concepts`"]) 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/BasicConceptsGroup -.-> python/booleans("`Booleans`") 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-48895{{"`Matplotlib 3D Quiver Plot`"}} matplotlib/figures_axes -.-> lab-48895{{"`Matplotlib 3D Quiver Plot`"}} matplotlib/quiver_plots -.-> lab-48895{{"`Matplotlib 3D Quiver Plot`"}} python/booleans -.-> lab-48895{{"`Matplotlib 3D Quiver Plot`"}} python/tuples -.-> lab-48895{{"`Matplotlib 3D Quiver Plot`"}} python/importing_modules -.-> lab-48895{{"`Matplotlib 3D Quiver Plot`"}} python/numerical_computing -.-> lab-48895{{"`Matplotlib 3D Quiver Plot`"}} python/data_visualization -.-> lab-48895{{"`Matplotlib 3D Quiver Plot`"}} end

Import Libraries and Set Up Plot

The first step is to import the necessary libraries and set up the plot. In this example, we will be using Matplotlib's pyplot module and its 3d toolkit for creating the 3D plot.

import matplotlib.pyplot as plt
import numpy as np

ax = plt.figure().add_subplot(projection='3d')

Create the Grid

Next, we will create a grid of points on which we will display the vector field. In this example, we will create a meshgrid of points using NumPy's meshgrid function. The arange function is used to create an array of evenly spaced points within a specified interval.

x, y, z = np.meshgrid(np.arange(-0.8, 1, 0.2),
                      np.arange(-0.8, 1, 0.2),
                      np.arange(-0.8, 1, 0.8))

Define the Direction of the Arrows

Now we will define the direction of the arrows. In this example, we will define the direction of the arrows using NumPy's trigonometric functions. The sin and cos functions are used to create the u, v, and w arrays that represent the direction of the arrows in the x, y, and z directions.

u = np.sin(np.pi * x) * np.cos(np.pi * y) * np.cos(np.pi * z)
v = -np.cos(np.pi * x) * np.sin(np.pi * y) * np.cos(np.pi * z)
w = (np.sqrt(2.0 / 3.0) * np.cos(np.pi * x) * np.cos(np.pi * y) *
     np.sin(np.pi * z))

Create the Quiver Plot

With the grid and direction of the arrows defined, we can create the quiver plot. In this example, we will use Matplotlib's quiver function to create the plot. The length parameter sets the length of the arrows and the normalize parameter normalizes the arrows to a length of 1.

ax.quiver(x, y, z, u, v, w, length=0.1, normalize=True)

Display the Plot

Finally, we will display the plot using Matplotlib's show function.

plt.show()

Summary

In this lab, you learned how to create a 3D quiver plot using Python Matplotlib. The meshgrid function was used to create a grid of points, and NumPy's trigonometric functions were used to define the direction of the arrows. The quiver function was then used to create the plot, and the show function was used to display it.

Other Python Tutorials you may like