【问题标题】:Annotating points from a Pandas Dataframe in Matplotlib plot在 Matplotlib 图中注释来自 Pandas Dataframe 的点
【发布时间】:2013-07-26 21:40:58
【问题描述】:

鉴于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

使用代码:

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

我生成如下图:

我不知道如何让轴附加注释? 这个例子Annotate Time Series plot in Matplotlib 非常接近但是我不知道如何从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()

但是我用什么来代替x[1]y[1] 来获取坐标轴?我尝试了['MORLD'][1]['SOLD_PRICE'][1] 并得到了index out of range...

【问题讨论】:

  • 似乎没有使用带有 Dataframe 索引的 Dataframe?
  • 对于 pandas 0.11.0 和 matplotlib 1.2.1,如果您使用 ax.annotate('Test', (df5.index[1], df5['SOLD_PRICE'][1]), ...,它似乎可以正常运行。至少如果您的DataFrame 的索引是DatetimeIndex
  • @nordev 美丽!!请张贴作为答案,以便我可以正确标记!只是无法解决 DF 索引访问语法!非常感谢

标签: python matplotlib pandas


【解决方案1】:

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

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

这给出(基于您的样本数据)以下输出:

【讨论】:

    猜你喜欢
    • 2013-12-24
    • 2013-04-03
    • 2012-05-09
    • 2020-01-19
    • 2015-07-30
    • 1970-01-01
    • 2019-01-15
    相关资源
    最近更新 更多