2013-07-26 69 views
8

給出一個DataFrame,如:詮釋點Matplotlib情節

   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

我無法弄清楚如何讓軸附加一個nnotation? 這個例子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 ......

+1

廁所在最後一個環節:http://pandas.pydata.org/pandas -docs/dev/cookbook.html#plotting – Jeff

+0

似乎沒有在Dataframe索引中使用Dataframe? – dartdog

+0

隨着大熊貓0.11.0和matplotlib 1.2.1看來,如果你使用'ax.annotate( '測試',(df5.index正常運行[1],DF5 [ 'SOLD_PRICE'] [1]),.. 。。。至少如果你的'DataFrame'的索引是'DatetimeIndex'。 – hooy

回答

12

您可以用DataFrameindex屬性訪問索引值。所以,你可以簡單地使用

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

這給(根據您的樣本數據)以下的輸出:

enter image description here

+1

非常有幫助,謝謝! – dartdog