连接具有不同属性的文本对象#

该示例将几个具有不同属性(例如,颜色或字体)的 Text 对象连接在一起,并将它们一个接一个地定位。第一个 Text 使用 text 直接创建;所有后续的 Text 都使用 annotate 创建,这允许将 Text 的左下角定位在之前一个 Text 的右下角 (xy=(1, 0)) (xycoords=text)。

rainbow text
import matplotlib.pyplot as plt

plt.rcParams["font.size"] = 20
ax = plt.figure().add_subplot(xticks=[], yticks=[])

# The first word, created with text().
text = ax.text(.1, .5, "Matplotlib", color="red")
# Subsequent words, positioned with annotate(), relative to the preceding one.
text = ax.annotate(
    " says,", xycoords=text, xy=(1, 0), verticalalignment="bottom",
    color="gold", weight="bold")  # custom properties
text = ax.annotate(
    " hello", xycoords=text, xy=(1, 0), verticalalignment="bottom",
    color="green", style="italic")  # custom properties
text = ax.annotate(
    " world!", xycoords=text, xy=(1, 0), verticalalignment="bottom",
    color="blue", family="serif")  # custom properties

plt.show()

由 Sphinx-Gallery 生成的图库