将具有不同属性的文本对象连接在一起#

该示例将具有不同属性(例如,颜色或字体)的多个 Text 对象串联在一起,并将每个对象彼此定位。第一个 Text 直接使用 text 创建;所有后续对象都使用 annotate 创建,这允许将 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 生成的图库