In [ ]:
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
import os

"""iterating over raintemp.csv and creating lots of clima diagrams"""

def save(path, ext='png', close=True, verbose=True):
    """Save a figure from pyplot. 
    from: http://www.jesshamrick.com/2012/09/03/saving-figures-from-pyplot/
    Parameters
    ----------
    path : string
        The path (and filename, without the extension) to save the
        figure to.
    ext : string (default='png')
        The file extension. This must be supported by the active
        matplotlib backend (see matplotlib.backends module).  Most
        backends support 'png', 'pdf', 'ps', 'eps', and 'svg'.
    close : boolean (default=True)
        Whether to close the figure after saving.  If you want to save
        the figure multiple times (e.g., to multiple formats), you
        should NOT close it in between saves or you will have to
        re-plot it.
    verbose : boolean (default=True)
        Whether to print information about when and where the image
        has been saved.
    """
    
    # Extract the directory and filename from the given path
    directory = os.path.split(path)[0]
    filename = "%s.%s" % (os.path.split(path)[1], ext)
    if directory == '':
        directory = '.'

    # If the directory does not exist, create it
    if not os.path.exists(directory):
        os.makedirs(directory)

    # The final path to save to
    savepath = os.path.join(directory, filename)

    if verbose:
        print("Saving figure to '%s'..." % savepath),

    # Actually save the figure
    plt.savefig(savepath)
    
    # Close it
    if close:
        plt.close()

    if verbose:
        print("Done")
        
# main program
data=pd.read_csv("raintemp.csv", index_col=None)
table=pd.pivot_table(data, values=["rain(mm)","temp(dC)"], index=["loc","month"])
#fig = table.query('loc=="Agna"')
locspivot=pd.pivot_table(data,  index=["loc"])
locs = list(locspivot.index)
locs
for location in locs:
    print("processing: ", location)
    fig = table.query('loc=="'+location+'"')
    rainvalues = []
    tempvalues = []
    for line in fig["rain(mm)"]:
        rainvalues.append(line)
    for line in fig["temp(dC)"]:
        tempvalues.append(line)
    #fig.plot(title=location +" rain/temp 1994-2014", xticks=[1,2,3,4,5,6,7,8,9,10,11,12])
    ## -------- making the diagram -------------
    i = np.arange(len(rainvalues))
    fig, rr = plt.subplots()

    #bar_width = 1

    opacity = 0.4
    #error_config = {'ecolor': '0.3'}


    rr.bar(i, rainvalues, alpha=opacity, color='b',
                  align='center' )

    tt=plt.twinx()
    tt.plot(tempvalues, linestyle='-', color="r", linewidth=1.0, label="temp")

    plt.xlabel('months')
    rr.set_ylabel('(mm)')
    #plt.ylabel('(mm)')
    tt.set_ylabel('(C)')
    text="rain and temp of " + location
    plt.title(text)
    plt.xticks(i,('jan', 'feb', 'mar', 'apr', 'mai', 'jun', 'jul', 'aug', 'sept', 'oct', 'nov', 'dez'))
    plt.legend()
    plt.grid(True)


    #rr.set_ylim([0,40])
    plt.tight_layout()
    plt.show()
    
    ## --------- saving to file -------------
    save(location, close=False, verbose=True)
    save(location, ext="svg", close=True, verbose=True)
print("i'm all done!")


processing:  Adria_-_Bellombra