Kei*_*ith 4 python text matplotlib
有没有办法通过轴的分数在图形中定位文本?无论 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()
Run Code Online (Sandbox Code Playgroud)
Jul*_*nck 10
您可以使用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()
Run Code Online (Sandbox Code Playgroud)