给定一个 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
的 index
属性访问索引值。所以你可以简单地使用
ax3.annotate('Test',
(df5.index[1], df5['SOLD_PRICE'][1]),
xytext=(15, 15),
textcoords='offset points',
arrowprops=dict(arrowstyle='-|>'))
这会(根据您的样本数据)给出以下输出:
关于python - 在 Matplotlib 图中从 Pandas Dataframe 注释点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17891493/