python - 在 Matplotlib 图中从 Pandas Dataframe 注释点

标签 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

使用代码:

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

我生成如下图:

Heatherwood example

我不知道如何让轴附加注释? 本例 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...

最佳答案

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

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

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

enter image description here

关于python - 在 Matplotlib 图中从 Pandas Dataframe 注释点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17891493/

相关文章:

python - 查找由具有 x、y、w、h 像素坐标的矩形覆盖的图 block 的坐标

python - 如何在 Python 中计算分解 A=P[I_r,0;0,0]Q?

jquery - Django,ajax 用模型数据填充表单

python - 在绘图上显示数据框

python - Pandas :将系列的数据类型更改为字符串

python - 如何从 matplotlib 中的 AxesSubplot 对象获取 BarContainer 对象?

python - 'c' 参数看起来像单个数字 RGB 或 RGBA 序列

python - 为什么 DataFrame 列对于 json 转换必须是唯一的?

python - Pandas 时间序列假期规则偏移量

python - 使用 Python Pandas 填补面板数据中的空白