import matplotlib.pyplot as plt
plt.hlines(7, 0, 2, linestyles='dashed')
plt.hlines(11, 0, 2, linestyles='dashed')
plt.hlines(10, 0, 2, linestyles='dashed')
plt.hlines(8, 0, 2, linestyles='dashed')
plt.annotate(
'', xy=(1, 10), xycoords='data',
xytext=(1, 8), textcoords='data',
arrowprops={'arrowstyle': ''})
plt.annotate(
'D = 1', xy=(1, 9), xycoords='data',
xytext=(5, 0), textcoords='offset points')
# alternatively,
# plt.text(1.01, 9, 'D = 1')
plt.show()
产量
有关plt.annotate可用的许多选项的更多信息,请参见this page.
如上所示,文本可以用plt.annotate或plt.text放置.使用plt.annotate可以指定偏移量(例如(5,0)),而使用plt.text可以指定数据坐标中的文本位置(例如(1.01,9)).