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

此示例将多个具有不同属性(例如,颜色或字体)的文本对象连接在一起,并将它们依次定位。第一个文本对象是直接使用 text 创建的;所有后续对象都是使用 annotate 创建的,这允许将文本对象的左下角定位在前一个文本对象的右下角(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 生成的画廊