串連具有不同屬性的文字物件#

此範例將數個具有不同屬性(例如顏色或字型)的 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 產生