3D 中的文本注释

PythonPythonBeginner
立即练习

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

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

简介

本实验将演示如何使用 Python 中的 Matplotlib 库在 3D 图上放置文本注释。将涵盖以下功能:

  • 使用 ~.Axes3D.text 函数,并设置三种类型的 zdir 值:None、轴名称(例如 'x')或方向元组(例如 (1, 1, 0))。
  • 使用 ~.Axes3D.text 函数,并设置颜色关键字。
  • 使用 .text2D 函数在 ax 对象的固定位置放置文本。

虚拟机使用提示

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

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

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


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL matplotlib(("Matplotlib")) -.-> matplotlib/BasicConceptsGroup(["Basic Concepts"]) python(("Python")) -.-> python/ControlFlowGroup(["Control Flow"]) python(("Python")) -.-> python/DataStructuresGroup(["Data Structures"]) python(("Python")) -.-> python/FunctionsGroup(["Functions"]) 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") python/ControlFlowGroup -.-> python/for_loops("For Loops") python/DataStructuresGroup -.-> python/tuples("Tuples") python/FunctionsGroup -.-> python/build_in_functions("Build-in Functions") python/ModulesandPackagesGroup -.-> python/importing_modules("Importing Modules") python/DataScienceandMachineLearningGroup -.-> python/data_visualization("Data Visualization") subgraph Lab Skills matplotlib/importing_matplotlib -.-> lab-48985{{"3D 中的文本注释"}} matplotlib/figures_axes -.-> lab-48985{{"3D 中的文本注释"}} python/for_loops -.-> lab-48985{{"3D 中的文本注释"}} python/tuples -.-> lab-48985{{"3D 中的文本注释"}} python/build_in_functions -.-> lab-48985{{"3D 中的文本注释"}} python/importing_modules -.-> lab-48985{{"3D 中的文本注释"}} python/data_visualization -.-> lab-48985{{"3D 中的文本注释"}} end

导入库

导入创建 3D 图并添加文本注释所需的库。

import matplotlib.pyplot as plt

创建一个 3D 图

使用 add_subplot 方法创建一个 3D 图。

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

使用带有 zdir 值的 ~.Axes3D.text 函数

使用 ~.Axes3D.text 函数来放置具有不同 zdir 值的文本注释。

zdirs = (None, 'x', 'y', 'z', (1, 1, 0), (1, 1, 1))
xs = (1, 4, 4, 9, 4, 1)
ys = (2, 5, 8, 10, 1, 2)
zs = (10, 3, 8, 9, 1, 8)

for zdir, x, y, z in zip(zdirs, xs, ys, zs):
    label = '(%d, %d, %d), dir=%s' % (x, y, z, zdir)
    ax.text(x, y, z, label, zdir)

使用带有 color 关键字的 ~.Axes3D.text 函数

使用带有 color 关键字的 ~.Axes3D.text 函数来更改文本注释的颜色。

ax.text(9, 0, 0, "red", color='red')

使用 .text2D 函数

使用 .text2D 函数在 ax 对象上的固定位置放置文本注释。

ax.text2D(0.05, 0.95, "2D Text", transform=ax.transAxes)

调整显示区域和标签

调整 3D 图的显示区域和标签。

ax.set_xlim(0, 10)
ax.set_ylim(0, 10)
ax.set_zlim(0, 10)
ax.set_xlabel('X axis')
ax.set_ylabel('Y axis')
ax.set_zlabel('Z axis')

显示图形

显示带有文本注释的 3D 图形。

plt.show()

总结

本实验展示了如何使用 Python 中的 Matplotlib 库在 3D 图上放置文本注释。使用了带有不同 zdir 值和 color 关键字的 ~.Axes3D.text 函数以及 .text2D 函数在 3D 图中放置文本注释。还对 3D 图的显示区域和标签进行了定制。