Pandas Plotting from Pivot Table

Viewed 29065

I am basically trying to reproduce climate diagrams showing mean temperature and precipitation over the year for various locations.

I've generated a pivot table from my csv the following way:

data = pd.read_csv("05_temp_rain_v2.csv")
pivot = data.pivot_table(["rain(mm)","temp(dC)"], ["loc","month"])  

sample data in text form:

loc,lat,long,year,month,rain(mm),temp(dC)
Adria_-_Bellombra,45.011129,12.034126,1994,1,45.6,4.6  
Adria_-_Bellombra,45.011129,12.034126,1994,2,31.4,4  
Adria_-_Bellombra,45.011129,12.034126,1994,3,1.6,10.7  
Adria_-_Bellombra,45.011129,12.034126,1994,4,74.4,11.5  
Adria_-_Bellombra,45.011129,12.034126,1994,5,26,17.2  
Adria_-_Bellombra,45.011129,12.034126,1994,6,108.6,20.6

Pivot Table:

enter image description here

Since I am handling various locations, I am iterating over them:

locations=pivot.index.get_level_values(0).unique()

for location in locations:
    split=pivot.xs(location)

    rain=split["rain(mm)"]
    temp=split["temp(dC)"]

    plt.subplots()
    temp.plot(kind="line",color="r",).legend()
    rain.plot(kind="bar").legend()

An example plot output is shown below:

enter image description here

Why are my temperature values being plotted starting from February (2)?
I assume it is because the temperature values are listed in the second column.

What would be the proper way to handle and plot different data (two columns) from a pivot table?

3 Answers
Related