tri*_*ane 35 python matplotlib labels
我有一个在matplotlib(时间序列数据)中创建的图形,其中有一系列
matplotlib.pyplot.axvline
Run Code Online (Sandbox Code Playgroud)
线.我想在图上创建标签,这些标签看起来接近(可能在线的RHS和图的顶部)这些垂直线.
Dan*_*Dan 61
你可以使用类似的东西
plt.axvline(10)
plt.text(10.1,0,'blah',rotation=90)
Run Code Online (Sandbox Code Playgroud)
您可能必须使用x和y值text来使其正确对齐.您可以在此处找到更完整的文档.
ing*_*net 20
无需手动放置的解决方案是使用“混合转换”。
变换将坐标从一个坐标系转换到另一个坐标系。通过 的transform参数指定变换text,您可以在轴坐标系中给出文本的x和y坐标(分别从 0 到 1 从 x/y 轴的左到右/从上到下)。通过混合变换,您可以使用混合坐标系。
这正是您所需要的:您拥有数据给出的 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()
Run Code Online (Sandbox Code Playgroud)