Annotating points from a Pandas Dataframe in Matplotlib graphics

Given a DataFrame like:

  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 

Using the code:

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

I create a schedule like:

Heatherwood example

I can't figure out how to get the axis to attach the annotation? This example The Annotate Time Series graph in Matplotlib is very close, but I don’t know how to specify the x and y DataFrame from the DataFrame ?

Therefore, it should be close to:

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

But what do I use instead of x[1] and y[1] to get the axis? I tried ['MORLD'][1] and ['SOLD_PRICE'][1] and got index out of range ...

+8
python matplotlib pandas
source share
1 answer

You can access index values ​​using the index DataFrame attribute. Therefore you can just use

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

This gives (based on your sample data) the output below:

enter image description here

+12
source share

All Articles