Text Annotations in 3D

PythonPythonBeginner
Practice Now

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

Introduction

This lab will demonstrate how to place text annotations on a 3D plot using Matplotlib library in Python. The following functionalities will be covered:

  • Using the ~.Axes3D.text function with three types of zdir values: None, an axis name (ex. 'x'), or a direction tuple (ex. (1, 1, 0)).
  • Using the ~.Axes3D.text function with the color keyword.
  • Using the .text2D function to place text on a fixed position on the ax object.

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`"]) python(("`Python`")) -.-> python/ControlFlowGroup(["`Control Flow`"]) python(("`Python`")) -.-> python/DataStructuresGroup(["`Data Structures`"]) python(("`Python`")) -.-> python/ModulesandPackagesGroup(["`Modules and Packages`"]) python(("`Python`")) -.-> python/DataScienceandMachineLearningGroup(["`Data Science and Machine Learning`"]) python(("`Python`")) -.-> python/FunctionsGroup(["`Functions`"]) 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/ModulesandPackagesGroup -.-> python/importing_modules("`Importing Modules`") python/DataScienceandMachineLearningGroup -.-> python/data_visualization("`Data Visualization`") python/FunctionsGroup -.-> python/build_in_functions("`Build-in Functions`") subgraph Lab Skills matplotlib/importing_matplotlib -.-> lab-48985{{"`Text Annotations in 3D`"}} matplotlib/figures_axes -.-> lab-48985{{"`Text Annotations in 3D`"}} python/for_loops -.-> lab-48985{{"`Text Annotations in 3D`"}} python/tuples -.-> lab-48985{{"`Text Annotations in 3D`"}} python/importing_modules -.-> lab-48985{{"`Text Annotations in 3D`"}} python/data_visualization -.-> lab-48985{{"`Text Annotations in 3D`"}} python/build_in_functions -.-> lab-48985{{"`Text Annotations in 3D`"}} end

Import Libraries

Import the necessary libraries to create a 3D plot and add text annotations.

import matplotlib.pyplot as plt

Create a 3D Plot

Create a 3D plot using the add_subplot method.

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

Using ~.Axes3D.text Function with zdir Values

Use the ~.Axes3D.text function to place text annotations with different zdir values.

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)

Using ~.Axes3D.text Function with color Keyword

Use the ~.Axes3D.text function with the color keyword to change the color of the text annotation.

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

Using .text2D Function

Use the .text2D function to place text annotations on a fixed position on the ax object.

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

Tweaking Display Region and Labels

Tweak the display region and labels of the 3D plot.

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

Display the Plot

Display the 3D plot with text annotations.

plt.show()

Summary

This lab demonstrated how to place text annotations on a 3D plot using Matplotlib library in Python. The ~.Axes3D.text function with different zdir values and color keyword, as well as the .text2D function, were used to place text annotations in the 3D plot. The display region and labels of the 3D plot were also customized.

Other Python Tutorials you may like