通过轴分数定位文本

10
有没有一种方法可以通过轴的分数来定位图中的文本?我希望在所有绘图中文本位置相同,而不受它们在x和y方向上不同范围的影响。这个功能在ax.annotate()中,但我需要加入额外的'xy'参数,这使得我的代码更难读懂。
import matplotlib.pyplot as plt
import numpy as np

fig = plt.figure() 
ax = fig.add_subplot(1, 1, 1)
ax.plot(np.arange(10),12*np.arange(10)) 
ax.annotate('Correct Position', xy=(0, 0), xytext=(0.4, 0.7), textcoords='axes fraction')
ax.text(0.4, 0.7, 'Incorrect Position')

plt.show()
1个回答

20
您可以使用transform关键字:
import matplotlib.pyplot as plt
import numpy as np

fig = plt.figure() 
ax = fig.add_subplot(1, 1, 1)
ax.plot(np.arange(10),12*np.arange(10)) 
ax.text(0.4, 0.7, 'Correct Position', transform=ax.transAxes)

plt.show()

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接