python - Matplotlib - 添加与轴底部对齐的标签

标签 python matplotlib

我想为图表上的垂直线添加标签(在本例中为洋红色线 - 'TEXT TO GO HERE')。

问题是我不知道 Y 轴的最小值,因为该轴是自动的,而 text() 函数需要 x 和 y 值。

我希望能够将文本对齐到绘图的底部,或者找到最小的 Y 轴值(在此示例中,matplotlib 似乎正在获取最小轴值并将其向下舍入到长轴值)

我目前正在尝试用 text() 来做这件事

垂直线是用 axvline() 创建的

屏幕截图显示了我在下面尝试做的事情;

(请注意,图像上的文本是在我的屏幕捕获软件中添加的,以显示我希望文本出现的位置)

enter image description here

最佳答案

您可以使用混合变换来做到这一点:

import matplotlib.transforms as transforms

fig = plt.figure()
ax = fig.add_subplot(111)

trans = transforms.blended_transform_factory(
    ax.transData, ax.transAxes)

ax.plot(range(50))
ax.axvline(20)

ax.text(20, 0.05, 'test', transform=trans, rotation='vertical', ha='right')

transform tutorial

关于python - Matplotlib - 添加与轴底部对齐的标签,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14667502/

相关文章:

python - 使用多索引重新索引并创建空日期

python - 使用 pysftp 连接到 SFTP 服务器时出现 "Failed to load HostKeys"警告

python - 内存错误 : numpy. genfromtxt()

matplotlib - 使用 PyCharm 和 Docker 解释器进行 X11 转发

python - 使用 matplotlib 子图和 seaborn 的 y 轴范围错误

python - 从不同文件导入使用 Turtle 的函数时出错

python在多行上分割正则表达式模式

python - 如何在 Python 中绘制具有可变宽度 bin 的直方图?

python - 使用 Python 的 matplotlib/basemap 的颜色状态

python - basemap 导入错误: No module named 'mpl_toolkits.basemap'