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.textfunction with three types of zdir values: None, an axis name (ex. 'x'), or a direction tuple (ex. (1, 1, 0)). - Using the
~.Axes3D.textfunction with the color keyword. - Using the
.text2Dfunction 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.
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.