15

I am using pandas and matplotlib to visualize this dataframe

    HDD  EnergyPerSquareMeter   Year
0    3333.6  23.11   1997
1    3349.6  24.30   1998
2    3319.5  24.78   1999
3    3059.1  22.01   2000
4    3287.5  24.17   2001
5    3054.9  20.01   2002
6    3330.0  21.25   2003
7    3307.3  19.22   2004
8    3401.4  18.31   2005
9    3261.6  20.40   2006
10   3212.8  15.34   2008
11   3231.2  15.95   2009
12   3570.1  15.79   2010
13   2995.3  13.88   2011

And I would like to plot EnergyPerSquareMeter as scatterplot (with the x-axis=HDD) and annotate the points with the year.

I did this:

ax =EnergyvsHDD.plot(x='HDD', y='EnergyPerSquareMeter', marker="o" , linestyle='None', figsize=(12,8))
for i, txt in enumerate(EnergyvsHDD['Year']):
    ax.annotate(txt, (x[i],y[i]), size=10, xytext=(0,0), ha='right', textcoords='offset points')

The outcome is:

enter image description here

The annotated text of the years doesn´t appear near the points. What am I doing wrong?

UPDATED

Using this code:

def label_point_orig(x, y, val, ax):
    a = pd.concat({'x': x, 'y': y, 'val': val}, axis=1)
    print a
    for i, point in a.iterrows():
        ax.text(point['x'], point['y'], str(point['val']))

And then:

ax = EnergyvsHDD.set_index('HDD')['EnergyPerSquareMeter'].plot(style='o')
label_point_orig(EnergyvsHDD.HDD, EnergyvsHDD.EnergyPerSquareMeter, EnergyvsHDD.Year, ax)
draw()

The points don't appear in the proper place:

enter image description here

Although using this code it works:

plt.scatter(list(EnergyvsHDD.HDD), list(EnergyvsHDD.EnergyPerSquareMeter))
label_point_orig(EnergyvsHDD.HDD, EnergyvsHDD.EnergyPerSquareMeter, EnergyvsHDD.Year, plt)
draw()

enter image description here

Does anybody know why?

1
  • I've used this code to generate labels, however, it seems that it shows floating point values even my values are type of integer. Do you have the same problem? Commented Aug 17, 2017 at 21:40

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.