Matplotlib - 添加与轴底部对齐的标签 [英] Matplotlib - Add labels aligned to bottom of axis

查看:37
本文介绍了Matplotlib - 添加与轴底部对齐的标签的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I want to add labels to vertical lines on my chart (The magenta line in this case - 'TEXT TO GO HERE').

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

The problem is I don't know the minimum value of the Y axis as the axis is automatic and the text() function requires an x and y value.

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

I want to either be able to align the text to the bottom of the plot, or, find the minimum Y axis value (in this example it seems matplotlib is getting the minimum axis value and rounding it down by the major axis value)

我目前正在尝试使用text()

I am currently trying to do this with text()

垂直线是使用axvline()创建的

The vertical lines are created with axvline()

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

Screenshot to show what i'm trying to do below;

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

(Note the text on the image was added in my screen capture software to show where I want the text to appear)

推荐答案

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

You can do this using a blended transform:

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')

转换教程

这篇关于Matplotlib - 添加与轴底部对齐的标签的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆