在 Matplotlib 图中注释来自 Pandas 数据框的点

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17891493/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 21:01:58  来源:igfitidea点击:

Annotating points from a Pandas Dataframe in Matplotlib plot

pythonmatplotlibpandas

提问by dartdog

Given a DataFramelike:

给出一个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

Using the code:

使用代码:

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

I generate a plot like:

我生成一个情节,如:

Heatherwood example

石南花示例

I cannot figure out how to get the axis to attach an annotation? This example Annotate Time Series plot in Matplotlibis very close but I don't know how to specify the x and y axis from the DataFrame?

我不知道如何让轴附加注释?这个示例在 Matplotlib 中注释时间序列图非常接近,但我不知道如何从DataFrame?

So it should be close to:

所以它应该接近:

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

fig.autofmt_xdate()
plt.show()

But what do I use instead of x[1]and y[1]to get the axis? I tried ['MORLD'][1]and ['SOLD_PRICE'][1]and got index out of range...

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

回答by sodd

You can access the index values with the indexattribute of the DataFrame. So you can simply use

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

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

This gives (based on your sample data) the below output:

这给出(基于您的示例数据)以下输出:

enter image description here

在此处输入图片说明