首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >在matplotlib图中线出现的位置创建标签

在matplotlib图中线出现的位置创建标签
EN

Stack Overflow用户
提问于 2012-11-16 16:43:51
回答 2查看 77.2K关注 0票数 68

我有一个在matplotlib (时间序列数据)中创建的图形,上面有一系列

matplotlib.pyplot.axvline

台词。我想在图上创建标签,使其看起来靠近这些垂直线(可能在线条的RHS上,靠近图的顶部)。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2012-11-16 22:14:34

您可以使用类似于

plt.axvline(10)
plt.text(10.1,0,'blah',rotation=90)

您可能需要处理text中的x和y值,以使其正确对齐。您可以找到更完整的文档here

票数 94
EN

Stack Overflow用户

发布于 2019-06-15 03:53:46

没有手动放置的解决方案是使用“混合转换”。

Transformations将坐标从一个坐标系转换到另一个坐标系。通过texttransform参数指定转换,您可以提供文本在轴坐标系中的xy坐标(分别从x/y轴的左到右/从上到下从0到1)。使用blended transformations,您可以使用混合坐标系。

这正是您所需要的:您拥有由数据给定的x坐标,并且您希望将文本放置在y轴上相对于轴的某个位置,例如中心。执行此操作的代码如下所示:

import matplotlib.transforms as transforms
import matplotlib.pyplot as plt

fig, ax = plt.subplots()

# the x coords of this transformation are data, and the
# y coord are axes
trans = ax.get_xaxis_transform()

x = 10
ax.axvline(x)
plt.text(x, .5, 'hello', transform=trans)

plt.show()
票数 20
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/13413112

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档