备注
前往结尾 下载完整示例代码。
连接具有不同属性的文本对象#
该示例将多个具有不同属性(例如颜色或字体)的文本对象串联在一起,每个文本对象依次排列。第一个文本对象直接使用 text 创建;所有后续的文本对象都使用 annotate 创建,这允许将文本的左下角定位在前一个文本的右下角(xy=(1, 0))(xycoords=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()