我有一个在matplotlib (时间序列数据)中创建的图形,上面有一系列
matplotlib.pyplot.axvline
台词。我想在图上创建标签,使其看起来靠近这些垂直线(可能在线条的RHS上,靠近图的顶部)。
发布于 2012-11-16 22:14:34
您可以使用类似于
plt.axvline(10)
plt.text(10.1,0,'blah',rotation=90)
您可能需要处理text
中的x和y值,以使其正确对齐。您可以找到更完整的文档here。
发布于 2019-06-15 03:53:46
没有手动放置的解决方案是使用“混合转换”。
Transformations将坐标从一个坐标系转换到另一个坐标系。通过text
的transform
参数指定转换,您可以提供文本在轴坐标系中的x
和y
坐标(分别从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()
https://stackoverflow.com/questions/13413112
复制相似问题