在Matplotlib图中注释来自Pandas Dataframe的点

dar*_*dog 9 python matplotlib pandas

给定DataFrame这样的:

             LIST_PRICE      SOLD_PRICE
MOYRLD      
1999-03-31   317062.500000   314800
1999-06-30   320900.000000   307100
1999-09-30   400616.666667   366160
1999-12-31   359900.000000   NaN
2000-03-31   359785.714286   330750
Run Code Online (Sandbox Code Playgroud)

使用代码:

import matplotlib.dates as mdates
ax3=df5.plot()
ax3.set_ylim(100000,600000)
ax3.set_title('Heatherwood-Quarterly')
Run Code Online (Sandbox Code Playgroud)

我生成一个类似的情节:

希瑟伍德的例子

我无法弄清楚如何让轴附加注释?这个示例在Matplotlib中的Annotate Time Series图非常接近,但我不知道如何从DataFrame?指定x和y轴?

所以应该接近:

ax3.annotate('Test', (mdates.date2num(x[1]), y[1]), xytext=(15, 15), 
            textcoords='offset points', arrowprops=dict(arrowstyle='-|>'))

fig.autofmt_xdate()
plt.show()
Run Code Online (Sandbox Code Playgroud)

但是我用什么代替x[1]y[1]取得轴?我试过['MORLD'][1]['SOLD_PRICE'][1],得到了 index out of range...

sod*_*odd 13

您可以使用的index属性访问索引值DataFrame.所以你可以简单地使用

ax3.annotate('Test',
             (df5.index[1], df5['SOLD_PRICE'][1]),
             xytext=(15, 15), 
             textcoords='offset points',
             arrowprops=dict(arrowstyle='-|>'))
Run Code Online (Sandbox Code Playgroud)

这给出了(根据您的样本数据)以下输出:

在此输入图像描述