2013-07-26 13 views
8

dato un DataFrame come:punti Annotazione da un Pandas dataframe nella trama 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 

Utilizzando il codice:

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

ho generare una trama simile:

Heatherwood example

Non riesco a capire come ottenere l'asse per collegare un a nnotation? Questo esempio Annotate Time Series plot in Matplotlib è molto vicino ma non so come specificare l'asse x e y da DataFrame?

quindi dovrebbe essere vicino a:

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

fig.autofmt_xdate() 
plt.show() 

Ma quello che posso usare al posto di x[1] e y[1] per ottenere l'asse? Ho provato ['MORLD'][1] e ['SOLD_PRICE'][1] e ottenuto index out of range ...

+1

loo all'ultimo link: http://pandas.pydata.org/pandas -docs/dev/cookbook.html # plotting – Jeff

+0

Non sembra che stia usando un Dataframe con l'indicizzazione di Dataframe? – dartdog

+0

Con pandas 0.11.0 e matplotlib 1.2.1 sembra funzionare correttamente se si utilizza 'ax.annotate ('Test', (df5.index [1], df5 ['SOLD_PRICE'] [1]), .. .'. Almeno se l'indice del tuo 'DataFrame' è un' DatetimeIndex'. – hooy

risposta

12

È possibile accedere ai valori di indice con l'attributo index del DataFrame. Così si può semplicemente utilizzare

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

questo dà (sulla base di dati di esempio) l'output di seguito:

enter image description here

+1

Molto utile, grazie! – dartdog