Python 使用pyplot注释x轴下方的垂直线

Python 使用pyplot注释x轴下方的垂直线,python,matplotlib,plot,annotations,Python,Matplotlib,Plot,Annotations,使用matplotlib,我试图注释在顺序数据绘图上绘制的垂直线。排队 plt.annotate('%.2f' % 2.34, xy=(0,0), xytext=(0,-20), xycoords='axes fraction', textcoords='offset points',color='r') 我可以让文本显示在我的绘图下面。然而,我绘制了垂直线——比如说,以240的间隔——并且我想在x轴下面特别注释这些线中的每一条 我尝试将xycoords格式切换为“data”,然后使用xy=(

使用matplotlib,我试图注释在顺序数据绘图上绘制的垂直线。排队

plt.annotate('%.2f' % 2.34, xy=(0,0), xytext=(0,-20), xycoords='axes fraction', textcoords='offset points',color='r')
我可以让文本显示在我的绘图下面。然而,我绘制了垂直线——比如说,以240的间隔——并且我想在x轴下面特别注释这些线中的每一条

我尝试将xycoords格式切换为“data”,然后使用xy=(240,0),但当我将xycoords格式切换为“data”时,注释文本不再显示在绘图上

我还尝试计算我想要放置注释的分数(例如240/绘图长度),但仍然没有正确放置注释


当我将坐标系更改为“数据”而不是“轴分数”时,为什么注释命令停止向绘图添加文本?谢谢

如果没有一个可行的例子,很难说确切的问题是什么。在任何情况下,作为您问题的解决方案,我建议您复制轴(在同一个图中),并使用第二个轴来控制附件

以下是一个可行的解决方案:

import numpy as np
import matplotlib.pyplot as plt

fig = plt.figure()
ax1 = fig.add_subplot(111)
ax2 = ax1.twiny()

# Add some extra space for the second axis at the bottom
fig.subplots_adjust(bottom=0.2)
ax1.set_xticks(range(-100, 100, 10))
ax1.set_xlim(-100, 100)
ax1.set_xticklabels([str(i) for i in range(-100, 100, 10)], rotation=45)

ax2.spines["bottom"].set_position(("axes", -0.1))
ax2.xaxis.set_ticks_position("bottom")
ax2.spines["bottom"].set_visible(True)
ax2.set_xticks([-50,50])
ax2.set_xticklabels(['Line 1', 'Line 2'], rotation=45, color='blue')
ax2.set_xlim(-100, 100)

b1 = np.random.randint(0,100,6)
b2 = np.random.randint(0,100,6)
b3 = np.random.randint(0,100,6)
ax1.scatter(np.random.normal(0, 1, 100)*100, np.random.normal(0, 1, 100)*100, c='green', s=90)
ax1.axvline(-50)
ax1.axvline(50)

plt.show()
,这导致: