gpt4 book ai didi

python - 按轴分数定位文本

转载 作者:行者123 更新时间:2023-12-02 07:08:55 25 4
gpt4 key购买 nike

有没有办法通过轴的分数在图中定位文本?我希望所有绘图的文本都位于相同位置,无论 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()

最佳答案

您可以使用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()

关于python - 按轴分数定位文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30464351/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com