Matplotlib 三维箭袋图

PythonPythonBeginner
立即练习

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

💡 本教程由 AI 辅助翻译自英文原版。如需查看原文,您可以 切换至英文原版

简介

在本实验中,你将学习如何使用 Python Matplotlib 创建三维箭袋图。箭袋图以箭头形式显示向量场。箭头指向向量的方向,其长度表示向量的大小。

虚拟机使用提示

虚拟机启动完成后,点击左上角切换到“笔记本”标签页,以访问 Jupyter Notebook 进行练习。

有时,你可能需要等待几秒钟让 Jupyter Notebook 完成加载。由于 Jupyter Notebook 的限制,操作验证无法自动化。

如果你在学习过程中遇到问题,随时向 Labby 提问。课程结束后提供反馈,我们将立即为你解决问题。


Skills Graph

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

导入库并设置绘图

第一步是导入必要的库并设置绘图。在本示例中,我们将使用 Matplotlib 的 pyplot 模块及其 3d 工具包来创建三维绘图。

import matplotlib.pyplot as plt
import numpy as np

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

创建网格

接下来,我们将创建一个点的网格,在其上显示向量场。在本示例中,我们将使用 NumPy 的 meshgrid 函数创建点的网格。arange 函数用于在指定区间内创建等间距点的数组。

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))

定义箭头方向

现在我们将定义箭头的方向。在本示例中,我们将使用 NumPy 的三角函数来定义箭头的方向。sincos 函数用于创建 uvw 数组,这些数组表示箭头在 xyz 方向上的方向。

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))

创建箭袋图

在定义了箭头的网格和方向后,我们就可以创建箭袋图了。在本示例中,我们将使用 Matplotlib 的 quiver 函数来创建该图。length 参数设置箭头的长度,normalize 参数将箭头归一化为长度 1。

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

显示绘图

最后,我们将使用 Matplotlib 的 show 函数来显示绘图。

plt.show()

总结

在本实验中,你学习了如何使用 Python 的 Matplotlib 创建三维箭袋图。使用 meshgrid 函数创建了点的网格,并使用 NumPy 的三角函数定义了箭头的方向。然后使用 quiver 函数创建绘图,并使用 show 函数显示它。