def PlotNiceTimeSeries(TheFile,TheHvars,TheHuncsC,TheHuncsSp,TheHuncsSt,
                       TheUnitees,TheMCount,TheYCount,TheTimeType,
		       TheStYr,TheEdYr,TheMDI,TheParams,TheReg,TheTrendChoice):
    ''' Plot a panel for each element of TheHvars '''
    ''' Add Coverage, Sampling and Station uncertainty ranges '''
    ''' Add lines for any extra estimates (TheVars) and HadISDH MASKED versions '''
    ''' Save as png and eps '''
    ''' TheHvars is a multi-row array: rows for vars, columns for months '''
    ''' Ditto TheHuncs C=coverage, Sp=sampling, St=station '''
    ''' TheUnitees is the units name list '''
    ''' TheMCount - number of months, TheStYr/EdYr - start and end years '''
    ''' TheMDI - missing data indicator for masking '''
    ''' TheColls - dictionary of colours for each dataset '''
     
    # SOrt out region title
    if TheReg == 'Glob':
        RegString = 'Globe (70$^{o}$S to 70$^{o}$N)'
    elif TheReg == 'Nhem':
        RegString = 'N. Hemi (20$^{o}$N to 70$^{o}$N)'
    elif TheReg == 'Trop':
        RegString = 'Tropics (20$^{o}$S to 20$^{o}$N)'
    elif TheReg == 'Shem':
        RegString = 'S. Hemi (70$^{o}$S to 20$^{o}$S)'
    
    
    # for clearer plotting
    TheMCount=TheMCount+1
    TheYCount=TheYCount+1    
    
    # set up number of panels and number of lines
    nplots=len(TheParams[:])
    print('PLOT NUMBERS: ',nplots)
#    nlines=[]
#    for n in range(nplots):
#        print(n,TheLablees[n][:])
#        nlines.append(len(TheLablees[n][:]))
	
    Letteree=[]
    Letteree=LetterRange(0,nplots)
    
    # set up x axes
    if TheTimeType == 'monthly':
        TheMonths=[]
        yr=TheStYr
        mon=1
        for m in range(TheMCount):
            TheMonths.append(dt.date(yr,mon,1))
            mon=mon+1
            if mon == 13:
                mon=1
                yr=yr+1   
        TheMonths=np.array(TheMonths)	    
    else:
        TheMonths=[]
        yr=TheStYr
        mon=1
        for y in range(TheYCount):
            TheMonths.append(dt.date(yr,mon,1))
            yr=yr+1   
        TheMonths=np.array(TheMonths)	    
    
    xtitlee='Years'
            
    # set up dimensions and plot - this is a 3 column nvar rows plot
    # Panel 1
    xpos=[]
    ypos=[]
    xfat=[]
    ytall=[]
    totalyspace=0.90	# start 0.08 end 0.98
    totalxspace=0.85	# start 0.12 end 0.98
    for n in range(nplots):
        xpos.append(0.12)
        ypos.append(0.98-((n+1)*(totalyspace/nplots)))
        xfat.append(totalxspace)
        ytall.append(totalyspace/nplots)
    
    f,axarr=plt.subplots(nplots,figsize=(7,12),sharex=False)	#6,18
    
    for pp in range(nplots):
        print('Plot: ',pp,TheParams[pp])
#        print(TheHvars[pp,0:10])
        print(TheHuncsC[pp,0:10])
        print(TheHuncsSp[pp,0:10])
        print(TheHuncsSt[pp,0:10])
	#axarr[pp].set_size(14)
        axarr[pp].set_position([xpos[pp],ypos[pp],xfat[pp],ytall[pp]])
        if TheTimeType == 'monthly':
            axarr[pp].set_xlim([TheMonths[0],TheMonths[TheMCount-1]])
        else:
            axarr[pp].set_xlim([TheMonths[0],TheMonths[TheYCount-1]])
        if pp < nplots-1:
            axarr[pp].set_xticklabels([])    
        if timetype == 'monthly':
            miny=min(np.reshape(TheHvars[pp,:],(TheMCount-1)))-max(np.reshape(TheHuncsC[pp,:],(TheMCount-1)))  
            maxy=max(np.reshape(TheHvars[pp,:],(TheMCount-1)))+max(np.reshape(TheHuncsC[pp,:],(TheMCount-1)))  
        else:
            miny=min(np.reshape(TheHvars[pp,:],(TheYCount-1)))-max(np.reshape(TheHuncsC[pp,:],(TheYCount-1)))  
            maxy=max(np.reshape(TheHvars[pp,:],(TheYCount-1)))+max(np.reshape(TheHuncsC[pp,:],(TheYCount-1)))  
        axarr[pp].set_ylim([(math.floor(10.*miny))/10.,(math.ceil(10.*maxy))/10.])
        if len(TheHuncsC[pp,:]) > 0:
            axarr[pp].fill_between(TheMonths[0:len(TheMonths)-1],TheHvars[pp,:]+TheHuncsC[pp,:],TheHvars[pp,:]-TheHuncsC[pp,:],
                           facecolor='Gold',edgecolor='none')
            axarr[pp].fill_between(TheMonths[0:len(TheMonths)-1],TheHvars[pp,:]+TheHuncsSp[pp,:],TheHvars[pp,:]-TheHuncsSp[pp,:],
                           facecolor='OrangeRed',edgecolor='none')
            axarr[pp].fill_between(TheMonths[0:len(TheMonths)-1],TheHvars[pp,:]+TheHuncsSt[pp,:],TheHvars[pp,:]-TheHuncsSt[pp,:],
                           facecolor='DeepSkyBlue',edgecolor='none')
			   
        if timetype == 'monthly':
            axarr[pp].plot(TheMonths[0:len(TheMonths)-1],TheHvars[pp,:],
                c='black',linewidth=0.25)
        else:
            axarr[pp].plot(TheMonths[0:len(TheMonths)-1],TheHvars[pp,:],
                c='black',linewidth=0.5)
	
        axarr[pp].annotate(Letteree[pp]+') '+TheParams[pp],xy=(0.03,0.86),
                  xycoords='axes fraction',size=12)

# get linear trend and annotate (does this work with masked arrays?)
        lintrend=[0.,0.,0.] # median, 5th adn 95th percentile rate of change per time step
        if (TheTrendChoice == 'MPW'):
            lintrend=MedianPairwise(TheHvars[pp,:],TheMDI,lintrend)
        elif (TheTrendChoice == 'OLS'):
            slopes=OLS_AR1Corr(TheHvars[pp,:],TheMDI,0.9)
            lintrend[0] = slopes[0]
            lintrend[1] = slopes[1]
            lintrend[2] = slopes[2]
	    	
        #pdb.set_trace()
	
        if timetype == 'monthly':
            linstr="%5.2f (%5.2f to %5.2f) %s decade$^{-1}$ " % (lintrend[0]*120,lintrend[1]*120,lintrend[2]*120,TheUnitees[pp])
        else:
            linstr="%5.2f (%5.2f to %5.2f) %s decade$^{-1}$ " % (lintrend[0]*10,lintrend[1]*10,lintrend[2]*10,TheUnitees[pp])

        axarr[pp].annotate(linstr,xy=(0.5,0.08),xycoords='axes fraction',size=12,ha='center')
	 
        axarr[pp].set_ylabel(TheUnitees[pp],fontsize=12)
        if TheTimeType == 'monthly':
            axarr[pp].hlines(0,TheMonths[0],TheMonths[TheMCount-1],
                color='black',linewidth=0.5)
        else:
            axarr[pp].hlines(0,TheMonths[0],TheMonths[TheYCount-1],
                color='black',linewidth=0.5)
	
    axarr[0].annotate(RegString,xy=(0.5,0.84),
             xycoords='axes fraction',size=16,ha='center')
    axarr[nplots-1].set_xlabel(xtitlee,fontsize=12)
         
# Figure Watermark and Labels
#    watermarkstring="/".join(os.getcwd().split('/')[4:])+'/'+os.path.basename( __file__ )+"   "+dt.datetime.strftime(dt.datetime.now(), "%d-%b-%Y %H:%M")
#    plt.figtext(0.01,0.01,watermarkstring,size=6)
    

    #plt.show()
    plt.savefig(TheFile+".eps")
    plt.savefig(TheFile+".png")


     
    return #PlotNiceDotsMap
Exemplo n.º 2
0
def PlotCorrMatrix(Filee, CorrAll, CorrAllMSK, TheRegions, TheNames):
    ''' This plots a correlation matrix for each region '''
    ''' Upper triangle is non-masked '''
    ''' Lower triangle is masked to HadISDH '''
    ''' Shades of YlOrRd from 0.8 to 1 '''

    # make matrix for each region
    RegCount = len(TheRegions)
    SourceCount = len(TheNames)
    GlobMat = np.empty((SourceCount, SourceCount))
    NHemMat = np.empty((SourceCount, SourceCount))
    TropMat = np.empty((SourceCount, SourceCount))
    SHemMat = np.empty((SourceCount, SourceCount))

    np.fill_diagonal(GlobMat, 1.)
    np.fill_diagonal(NHemMat, 1.)
    np.fill_diagonal(TropMat, 1.)
    np.fill_diagonal(SHemMat, 1.)

    print('FILLED DIAGONALS')

    point1 = 0
    point2 = 1
    for cc in range(len(CorrAll)):
        print(cc, point1, point2)
        GlobMat[point1, point2] = CorrAll[cc, 0]
        NHemMat[point1, point2] = CorrAll[cc, 1]
        TropMat[point1, point2] = CorrAll[cc, 2]
        SHemMat[point1, point2] = CorrAll[cc, 3]
        GlobMat[point2, point1] = CorrAllMSK[cc, 0]
        NHemMat[point2, point1] = CorrAllMSK[cc, 1]
        TropMat[point2, point1] = CorrAllMSK[cc, 2]
        SHemMat[point2, point1] = CorrAllMSK[cc, 3]
        point2 = point2 + 1
        if (point2 == 6):
            point1 = point1 + 1
            point2 = point1 + 1

    print('GOT MATRICES')

    # set up colours
    cmap = plt.get_cmap('YlOrRd')

    cmaplist = [cmap(i) for i in range(cmap.N)]
    print('CHOSEN COLOURS')
    # remove the darkest and lightest (white and black) - and reverse
    for loo in range(30):
        cmaplist.remove(cmaplist[0])
    cmap = cmap.from_list('this_cmap', cmaplist, cmap.N)
    print('REFINED COLOURS')

    bounds = np.array([
        0.9, 0.91, 0.92, 0.93, 0.94, 0.95, 0.96, 0.97, 0.98, 0.99, 1, 1.00001
    ])

    strbounds = ["%4.3g" % i for i in bounds]
    norm = mpl_cm.colors.BoundaryNorm(bounds, cmap.N)

    print('SORTED COLOURS')

    # set up plot space and dimensions and plot - this is a 2 column nvar rows plot
    #    xpos=list([0.12,0.62,0.12,0.62])
    #    ypos=list([0.54,0.54,0.08,0.08])
    #    xfat=list([0.36,0.36,0.36,0.36])
    #    ytall=list([0.32,0.32,0.32,0.32])
    xpos = list([0.02, 0.52, 0.02, 0.52])
    ypos = list([0.66, 0.66, 0.2, 0.2])
    xfat = list([0.36, 0.36, 0.36, 0.36])
    ytall = list([0.30, 0.3, 0.3, 0.3])

    miny = 0
    maxy = 6
    xinds = range(7)
    yinds = range(6, -1, -1)
    ArrX, ArrY = np.meshgrid(xinds, yinds)

    Letteree = []
    Letteree = LetterRange(0, len(TheRegions))

    f, axarr = plt.subplots(len(TheRegions), figsize=(12, 13),
                            sharex=False)  #6,18

    for pp in range(len(TheRegions)):
        print('Plot: ', pp)
        #axarr[pp].set_size(14)
        axarr[pp].set_position([xpos[pp], ypos[pp], xfat[pp], ytall[pp]])
        axarr[pp].set_xticklabels([])
        axarr[pp].set_yticklabels([])
        axarr[pp].set_ylim([miny, maxy])
        axarr[pp].set_xlim([miny, maxy])

        if (pp == 0):
            ChosenMat = GlobMat
        if (pp == 1):
            ChosenMat = NHemMat
        if (pp == 2):
            ChosenMat = TropMat
        if (pp == 3):
            ChosenMat = SHemMat

        print('MAKING THE GRIDS')
        print(ChosenMat)
        grids = axarr[pp].pcolor(ArrX,
                                 ArrY,
                                 ChosenMat,
                                 cmap=cmap,
                                 norm=norm,
                                 edgecolor='0.2',
                                 linewidth=0.5)  #,latlon='TRUE'

        for sn in range(len(TheNames)):
            NamePos = sn * (1. / 6.) + (1. / 12.)
            Topsies = -0.02
            Sidesies = 1.02
            print(sn, NamePos, Topsies, Sidesies)
            axarr[pp].annotate(TheNames[sn],
                               xy=(NamePos, Topsies),
                               xycoords='axes fraction',
                               rotation=90,
                               size=12,
                               ha='center',
                               va='top')
            axarr[pp].annotate(TheNames[sn],
                               xy=(Sidesies, 1 - NamePos),
                               xycoords='axes fraction',
                               rotation=0,
                               size=12,
                               ha='left',
                               va='center')

        axarr[pp].annotate(Letteree[pp] + ') ' + TheRegions[pp],
                           xy=(0.0, 1.03),
                           xycoords='axes fraction',
                           size=16)

    # loop through plots (a to d) label sources

    # add colour bar along righthandside

    cbax = f.add_axes([0.1, 0.05, 0.8, 0.03])
    cb = f.colorbar(grids, cax=cbax, orientation='horizontal',
                    ticks=bounds)  #, extend=extend
    cb.ax.tick_params(labelsize=12)
    cb.ax.set_xticklabels(strbounds)
    plt.figtext(0.5, 0.01, 'Correlation', size=16, ha='center')

    # save to file
    #plt.show()
    plt.savefig(Filee + ".eps")
    plt.savefig(Filee + ".png")

    return  #PlotCorrMatrix
def PlotNiceTimeSeries(TheFile, TheHvars, TheHuncsC, TheHuncsSp, TheHuncsSt,
                       TheLablees, TheUnitees, TheVars, TheMASKVars, TheMCount,
                       TheYCount, TheTimeType, TheStYr, TheEdYr, TheMDI,
                       TheColls, TheParams):
    ''' Plot a panel for each element of TheHvars '''
    ''' Add Coverage, Sampling and Station uncertainty ranges '''
    ''' Add lines for any extra estimates (TheVars) and HadISDH MASKED versions '''
    ''' Save as png and eps '''
    ''' TheHvars is a multi-row array: rows for vars, columns for months '''
    ''' Ditto TheHuncs C=coverage, Sp=sampling, St=station '''
    ''' TheLablees is the name list for all other vars '''
    ''' TheUnitees is the units name list '''
    ''' TheVars is a multirow array if there is 1+ var available - or [] '''
    ''' TheMASKVars - ditto above but masked to HadISDH coverage '''
    ''' TheMCount - number of months, TheStYr/EdYr - start and end years '''
    ''' TheMDI - missing data indicator for masking '''
    ''' TheColls - dictionary of colours for each dataset '''

    # for clearer plotting
    TheMCount = TheMCount + 1
    TheYCount = TheYCount + 1

    # set up number of panels and number of lines
    nplots = len(TheParams[:])
    print('PLOT NUMBERS: ', nplots)
    nlines = []
    for n in range(nplots):
        print(n, TheLablees[n][:])
        nlines.append(len(TheLablees[n][:]))

    Letteree = []
    Letteree = LetterRange(0, nplots * 2)

    # set up x axes
    if TheTimeType == 'monthly':
        TheMonths = []
        yr = TheStYr
        mon = 1
        for m in range(TheMCount):
            TheMonths.append(dt.date(yr, mon, 1))
            mon = mon + 1
            if mon == 13:
                mon = 1
                yr = yr + 1
        TheMonths = np.array(TheMonths)
    else:
        TheMonths = []
        yr = TheStYr
        mon = 1
        for y in range(TheYCount):
            TheMonths.append(dt.date(yr, mon, 1))
            yr = yr + 1
        TheMonths = np.array(TheMonths)

    xtitlee = 'Years'

    # set up dimensions and plot - this is a 2 column nvar rows plot
    # Panel 1
    xpos = []
    ypos = []
    xfat = []
    ytall = []
    totalyspace = 0.90  # start 0.08 end 0.98
    totalxspace = 0.43  # start 0.12 end 0.98
    for n in range(nplots):
        xpos.append(0.10)
        ypos.append(0.98 - ((n + 1) * (totalyspace / nplots)))
        xfat.append(totalxspace)
        ytall.append(totalyspace / nplots)

    f, axarr = plt.subplots(14, figsize=(10, 12), sharex=False)  #6,18

    for pp in range(nplots):
        print('Plot: ', pp, TheParams[pp])
        #        print(TheHvars[pp,0:10])
        print(TheHuncsC[pp, 0:10])
        print(TheHuncsSp[pp, 0:10])
        print(TheHuncsSt[pp, 0:10])
        #axarr[pp].set_size(14)
        axarr[pp].set_position([xpos[pp], ypos[pp], xfat[pp], ytall[pp]])
        if TheTimeType == 'monthly':
            axarr[pp].set_xlim([TheMonths[0], TheMonths[TheMCount - 1]])
        else:
            axarr[pp].set_xlim([TheMonths[0], TheMonths[TheYCount - 1]])
        if pp < nplots - 1:
            axarr[pp].set_xticklabels([])
        axarr[pp].set_ylim([
            (math.floor(10. *
                        (min(TheHvars[pp, :] - TheHuncsC[pp, :])))) / 10.,
            (math.ceil(10. * (max(TheHvars[pp, :] + TheHuncsC[pp, :])))) / 10.
        ])
        if len(TheHuncsC[pp, :]) > 0:
            axarr[pp].fill_between(TheMonths[0:len(TheMonths) - 1],
                                   TheHvars[pp, :] + TheHuncsC[pp, :],
                                   TheHvars[pp, :] - TheHuncsC[pp, :],
                                   facecolor='Gold',
                                   edgecolor='none')
            axarr[pp].fill_between(TheMonths[0:len(TheMonths) - 1],
                                   TheHvars[pp, :] + TheHuncsSp[pp, :],
                                   TheHvars[pp, :] - TheHuncsSp[pp, :],
                                   facecolor='OrangeRed',
                                   edgecolor='none')
            axarr[pp].fill_between(TheMonths[0:len(TheMonths) - 1],
                                   TheHvars[pp, :] + TheHuncsSt[pp, :],
                                   TheHvars[pp, :] - TheHuncsSt[pp, :],
                                   facecolor='DeepSkyBlue',
                                   edgecolor='none')

        if timetype == 'monthly':
            axarr[pp].plot(TheMonths[0:len(TheMonths) - 1],
                           TheHvars[pp, :],
                           c='black',
                           linewidth=0.25)
        else:
            axarr[pp].plot(TheMonths[0:len(TheMonths) - 1],
                           TheHvars[pp, :],
                           c='black',
                           linewidth=0.5)

        axarr[pp].annotate(Letteree[pp] + ') ' + TheParams[pp],
                           xy=(0.03, 0.86),
                           xycoords='axes fraction',
                           size=12)

        # get linear trend and annotate (does this work with masked arrays?)
        lintrend = [
            0., 0., 0.
        ]  # median, 5th adn 95th percentile rate of change per time step
        lintrend = MedianPairwise(TheHvars[pp, :], TheMDI, lintrend)
        if timetype == 'monthly':
            linstr = "%5.2f (%5.2f to %5.2f) %s decade$^{-1}$ " % (
                lintrend[0] * 120, lintrend[1] * 120, lintrend[2] * 120,
                TheUnitees[pp])
        else:
            linstr = "%5.2f (%5.2f to %5.2f) %s decade$^{-1}$ " % (
                lintrend[0] * 10, lintrend[1] * 10, lintrend[2] * 10,
                TheUnitees[pp])

        #plt.figtext(0.1,0.86,rawstr,color='r')
        axarr[pp].annotate(linstr,
                           xy=(0.5, 0.08),
                           xycoords='axes fraction',
                           size=12,
                           ha='center')

        #	axarr[pp].annotate('HadISDH',xy=(0.14,0.9),xycoords='axes fraction',color='black',size=10)
        #	for ll in range(nlines[pp]):	# no problem if 0
        #            print('Other: ',ll,TheLablees[pp][ll])
        ##	    axarr[pp].plot(TheMonths[0:len(TheMonths)-1],TheVars[pp,ll,:],c=TheColls[TheLablees[pp][ll]],linewidth=0.5)
        ##	    axarr[pp].plot(TheMonths[0:len(TheMonths)-1],TheMASKVars[pp,ll,:],c=TheColls[TheLablees[pp][ll]],linestyle='dotted',linewidth=0.5)
        #	    if timetype == 'monthly':
        #	        axarr[pp].plot(TheMonths[0:len(TheMonths)-1],TheMASKVars[pp,ll,:],c=TheColls[TheLablees[pp][ll]],linewidth=0.5)
        #	    else:
        #	        axarr[pp].plot(TheMonths[0:len(TheMonths)-1],TheMASKVars[pp,ll,:],c=TheColls[TheLablees[pp][ll]],linewidth=2)
        #
        #	    axarr[pp].annotate(TheLablees[pp][ll],xy=(0.14,0.82-(ll*0.08)),xycoords='axes fraction',
        #	               color=TheColls[TheLablees[pp][ll]],size=10)
        axarr[pp].set_ylabel(TheUnitees[pp], fontsize=12)
        if TheTimeType == 'monthly':
            axarr[pp].hlines(0,
                             TheMonths[0],
                             TheMonths[TheMCount - 1],
                             color='black',
                             linewidth=0.5)
        else:
            axarr[pp].hlines(0,
                             TheMonths[0],
                             TheMonths[TheYCount - 1],
                             color='black',
                             linewidth=0.5)

    axarr[0].annotate('Globe (70$^{o}$S to 70$^{0}$N)',
                      xy=(0.5, 0.84),
                      xycoords='axes fraction',
                      size=16,
                      ha='center')
    axarr[nplots - 1].set_xlabel(xtitlee, fontsize=12)

    # Panel 2
    xpos = []
    ypos = []
    xfat = []
    ytall = []
    totalyspace = 0.90  # start 0.08 end 0.98
    totalxspace = 0.43  # start 0.12 end 0.98
    for n in range(nplots):
        xpos.append(0.53)
        ypos.append(0.98 - ((n + 1) * (totalyspace / nplots)))
        xfat.append(totalxspace)
        ytall.append(totalyspace / nplots)

#    f,axarr=plt.subplots(7,figsize=(6,12),sharex=True)	#6,18

    for pp in range(nplots):
        print('Plot: ', pp + nplots, TheParams[pp])
        axarr[pp + nplots].set_position(
            [xpos[pp], ypos[pp], xfat[pp], ytall[pp]])
        if TheTimeType == 'monthly':
            axarr[pp + nplots].set_xlim(
                [TheMonths[0], TheMonths[TheMCount - 1]])
        else:
            axarr[pp + nplots].set_xlim(
                [TheMonths[0], TheMonths[TheYCount - 1]])
        if pp < nplots - 1:
            axarr[pp + nplots].set_xticklabels([])
        axarr[pp + nplots].set_ylim([
            (math.floor(10. *
                        (min(TheHvars[pp, :] - TheHuncsC[pp, :])))) / 10.,
            (math.ceil(10. * (max(TheHvars[pp, :] + TheHuncsC[pp, :])))) / 10.
        ])
        axarr[pp + nplots].set_yticklabels([])
        axarr[pp + nplots].annotate(Letteree[pp + nplots] + ') ' +
                                    TheParams[pp],
                                    xy=(0.03, 0.86),
                                    xycoords='axes fraction',
                                    size=12)

        for ll in range(nlines[pp]):  # no problem if 0
            print('Other: ', ll, TheLablees[pp][ll])
            #	    axarr[pp+nplots].plot(TheMonths[0:len(TheMonths)-1],TheVars[pp,ll,:],c=TheColls[TheLablees[pp][ll]],linewidth=0.5)
            #	    axarr[pp+nplots].plot(TheMonths[0:len(TheMonths)-1],TheMASKVars[pp,ll,:],c=TheColls[TheLablees[pp][ll]],linestyle='dotted',linewidth=0.5)
            if timetype == 'monthly':
                axarr[pp + nplots].plot(TheMonths[0:len(TheMonths) - 1],
                                        TheHvars[pp, :] -
                                        TheMASKVars[pp, ll, :],
                                        c=TheColls[TheLablees[pp][ll]],
                                        linewidth=0.5)
            else:
                axarr[pp + nplots].plot(TheMonths[0:len(TheMonths) - 1],
                                        TheHvars[pp, :] -
                                        TheMASKVars[pp, ll, :],
                                        c=TheColls[TheLablees[pp][ll]],
                                        linewidth=1)

# get correlation and annotate (does this work with masked arrays?)
            if timetype == 'monthly':
                subH = TheHvars[pp, 6 * 12:TheMCount - 2]
                subO = TheMASKVars[pp, ll, 6 * 12:TheYCount - 2]
            else:
                subH = TheHvars[pp, 6:TheYCount - 2]
                subO = TheMASKVars[pp, ll, 6:TheYCount - 2]

#standardise
            print('STDEVS: ', np.std(subH), np.std(subO))
            subH = subH / np.std(subH)
            subO = subO / np.std(subO)
            #stop
            rval = pearsonr(subH, subO)
            print(rval)
            linstr = "(R = %6.3f)" % (rval[0])
            axarr[pp + nplots].annotate('HadISDH - ' + TheLablees[pp][ll] +
                                        ' ' + linstr,
                                        xy=(0.03, 0.08 + (ll * 0.1)),
                                        xycoords='axes fraction',
                                        color=TheColls[TheLablees[pp][ll]],
                                        size=12)

#        axarr[pp+nplots].set_ylabel(TheUnitees[pp],fontsize=10)
        if TheTimeType == 'monthly':
            axarr[pp + nplots].hlines(0,
                                      TheMonths[0],
                                      TheMonths[TheMCount - 1],
                                      color='black',
                                      linewidth=0.5)
        else:
            axarr[pp + nplots].hlines(0,
                                      TheMonths[0],
                                      TheMonths[TheYCount - 1],
                                      color='black',
                                      linewidth=0.5)

    axarr[7].annotate('Difference Series',
                      xy=(0.5, 0.84),
                      xycoords='axes fraction',
                      size=16,
                      ha='center')
    axarr[(nplots * 2) - 1].set_xlabel(xtitlee, fontsize=12)

    # Figure Watermark and Labels
    #    watermarkstring="/".join(os.getcwd().split('/')[4:])+'/'+os.path.basename( __file__ )+"   "+dt.datetime.strftime(dt.datetime.now(), "%d-%b-%Y %H:%M")
    #    plt.figtext(0.01,0.01,watermarkstring,size=6)

    #plt.show()
    plt.savefig(TheFile + ".eps")
    plt.savefig(TheFile + ".png")

    return  #PlotNiceDotsMap
Exemplo n.º 4
0
def PlotTimeSeries(TheFile, ALLarr, trendALL, corrALL, ALLarrMSK, trendALLMSK,
                   corrALLMSK, ExtraInfo, TheUnits, MonCount, YrCount,
                   TypeTime, YrStart, YrEnd, TheMDI, SourceCount, Titlee,
                   ShortNameSource, TheRCount):
    ''' Plot a multi-panel and overlay the time series '''
    ''' Add a legend with colours and sources '''
    ''' Annotate with trends and otherinfo '''
    ''' This will be an 8 panel plot - Non-masked on the left, masked on the right '''
    ''' Save as png and eps '''

    # for clearer plotting
    MonCount = MonCount + 1
    YrCount = YrCount + 1

    # Mask the time series
    MSKALLarr = ma.masked_where(
        ALLarr <= -999.,
        ALLarr)  # a fudge here as floating point precision issues in MDI
    MSKALLarrMSK = ma.masked_where(
        ALLarrMSK <= -999.,
        ALLarrMSK)  # a fudge here as floating point precision issues in MDI

    # set up x axes
    if TypeTime == 'monthly':
        TheMonths = []
        yr = YrStart[0]
        mon = 1
        for m in range(MonCount):
            TheMonths.append(dt.date(yr, mon, 1))
            mon = mon + 1
            if mon == 13:
                mon = 1
                yr = yr + 1
        TheMonths = np.array(TheMonths)
    else:
        TheMonths = []
        yr = YrStart[0]
        mon = 1
        for y in range(YrCount):
            TheMonths.append(dt.date(yr, mon, 1))
            yr = yr + 1
        TheMonths = np.array(TheMonths)

    xtitlee = 'Years'
    ytitlee = 'Anomalies (' + TheUnits + ')'

    # set up number of panels and number of lines
    nplots = TheRCount
    print('PLOT NUMBERS: ', nplots)
    nlines = []
    for n in range(nplots):
        nlines.append(SourceCount)

    Letteree = []
    Letteree = LetterRange(0, nplots * 2)

    # set up dimensions and plot - this is a 2 column nvar rows plot
    xpos = []
    ypos = []
    xfat = []
    ytall = []
    totalyspace = 0.90  # start 0.08 end 0.98
    totalxspace = 0.40  # start 0.12 end 0.98
    for n in range(nplots):
        xpos.append(0.07)
        ypos.append(0.98 - ((n + 1) * (totalyspace / nplots)))
        xfat.append(totalxspace)
        ytall.append(totalyspace / nplots)

    # set up dimensions and plot - this is a 3 column nvar rows plot

    if (SourceCount == 6):
        cols = [
            'DimGrey', 'Firebrick', 'DodgerBlue', 'DarkOrange', 'MediumBlue',
            'HotPink'
        ]
    else:
        cols = [
            'Firebrick', 'DodgerBlue', 'DarkOrange', 'MediumBlue', 'HotPink'
        ]

    f, axarr = plt.subplots(TheRCount * 2, figsize=(16, 12),
                            sharex=False)  #6,18

    for pp in range(nplots):
        print('Plot: ', pp)
        #axarr[pp].set_size(14)
        axarr[pp].set_position([xpos[pp], ypos[pp], xfat[pp], ytall[pp]])
        if TypeTime == 'monthly':
            axarr[pp].set_xlim([TheMonths[0], TheMonths[MonCount - 1]])
        else:
            axarr[pp].set_xlim([TheMonths[0], TheMonths[YrCount - 1]])
        if pp < nplots - 1:
            axarr[pp].set_xticklabels([])
        miny = (math.floor(
            10. * np.min(np.append(MSKALLarr, MSKALLarrMSK)))) / 10.
        maxy = (math.ceil(
            10. * np.max(np.append(MSKALLarr, MSKALLarrMSK)))) / 10.
        axarr[pp].set_ylim([miny, maxy])

        if TypeTime == 'monthly':
            for sn in range(SourceCount):
                axarr[pp].plot(TheMonths[0:len(TheMonths) - 1],
                               MSKALLarr[sn, pp, :],
                               c=cols[sn],
                               linewidth=1)
        else:
            for sn in range(SourceCount):
                axarr[pp].plot(TheMonths[0:len(TheMonths) - 1],
                               MSKALLarr[sn, pp, :],
                               c=cols[sn],
                               linewidth=2)

        axarr[pp].annotate(Letteree[pp] + ') ' + Titlee[pp],
                           xy=(0.02, 0.90),
                           xycoords='axes fraction',
                           size=16)

        # get linear trend and annotate (does this work with masked arrays?)
        if trendALL[0, pp, 0] != TheMDI:
            scaletrend = ([0.32, 0.26, 0.2, 0.14, 0.08,
                           0.02])  #([0.88,0.82,0.76,0.70])
            if (SourceCount == 5):
                scaletrend = scaletrend = ([0.26, 0.2, 0.14, 0.08, 0.02])
            for sn in range(SourceCount):
                linstr = '{0:5.2f}'.format(
                    trendALL[sn, pp, 0]) + ' (' + '{0:5.2f}'.format(
                        trendALL[sn, pp, 1]) + ',' + '{0:5.2f}'.format(
                            trendALL[sn, pp, 2]) + ') ' + '{:s}'.format(
                                TheUnits) + ' dec$^{-1}$'
                namstr = ShortNameSource[sn] + ' (' + '{0:5.2f}'.format(
                    np.std(MSKALLarr[sn, pp, :])) + ')'
                #linstr="%19s %5.2f (%5.2f to %5.2f) %s decade$^{-1}$ " % (NameSource[sn],trendGB[sn,0],trendGB[sn,1],trendGB[sn,2],TheUnits)
                axarr[pp].annotate(namstr,
                                   xy=(0.5, scaletrend[sn]),
                                   xycoords='axes fraction',
                                   size=9,
                                   ha='left',
                                   color=cols[sn])
                axarr[pp].annotate(linstr,
                                   xy=(0.72, scaletrend[sn]),
                                   xycoords='axes fraction',
                                   size=9,
                                   ha='left',
                                   color=cols[sn])

## get correlation and annotate (does this work with masked arrays?)
#        if corrALL[0,pp] != TheMDI:
#	    scaletrend=([0.02,0.08,0.14,0.2,0.26,0.32])
#	    point1=0
#	    point2=1
#	    for sn in range(len(corrALL[:,0])):
#	        linstr='r = '+'{0:6.3f}'.format(corrALL[sn,pp])
#	        #linstr="%19s %5.2f (%5.2f to %5.2f) %s decade$^{-1}$ " % (NameSource[sn],trendGB[sn,0],trendGB[sn,1],trendGB[sn,2],TheUnits)
#	        axarr[pp].annotate(ShortNameSource[point1]+','+ShortNameSource[point2],xy=(0.6,scaletrend[sn]),xycoords='axes fraction',size=10,ha='left',color='Black')
#	        axarr[pp].annotate(linstr,xy=(0.85,scaletrend[sn]),xycoords='axes fraction',size=10,ha='left',color='Black')
#		point2=point2+1
#		if (point2 == SourceCount):	# loop through
#	            point1=point1+1
#	            point2=1+point1

# Plot a line at zero
        if TypeTime == 'monthly':
            axarr[pp].hlines(0,
                             TheMonths[0],
                             TheMonths[MonCount - 1],
                             color='black',
                             linewidth=0.5)
        else:
            axarr[pp].hlines(0,
                             TheMonths[0],
                             TheMonths[YrCount - 1],
                             color='black',
                             linewidth=0.5)

        axarr[pp].set_ylabel(ytitlee, fontsize=12)

    axarr[pp].set_xlabel(xtitlee, fontsize=12)

    # NOW DO THE MASKED PLOTS
    # set up dimensions and plot - this is a 2 column nvar rows plot
    xpos = []
    ypos = []
    xfat = []
    ytall = []
    totalyspace = 0.90  # start 0.08 end 0.98
    totalxspace = 0.40  # start 0.12 end 0.98
    for n in range(nplots):
        xpos.append(0.57)
        ypos.append(0.98 - ((n + 1) * (totalyspace / nplots)))
        xfat.append(totalxspace)
        ytall.append(totalyspace / nplots)

    # set up dimensions and plot - this is a 3 column nvar rows plot

#    f,axarr=plt.subplots(TheRCount*2,figsize=(16,12),sharex=False)	#6,18

    for pp in range(nplots):
        print('Plot: ', pp)
        #axarr[pp].set_size(14)
        axarr[pp + 4].set_position([xpos[pp], ypos[pp], xfat[pp], ytall[pp]])
        if TypeTime == 'monthly':
            axarr[pp + 4].set_xlim([TheMonths[0], TheMonths[MonCount - 1]])
        else:
            axarr[pp + 4].set_xlim([TheMonths[0], TheMonths[YrCount - 1]])
        if pp < nplots - 1:
            axarr[pp + 4].set_xticklabels([])
#        miny=(math.floor(10.*np.min(MSKALLarr)))/10.
#        maxy=(math.ceil(10.*np.max(MSKALLarr)))/10.
        axarr[pp + 4].set_ylim([miny, maxy])

        if TypeTime == 'monthly':
            for sn in range(SourceCount):
                axarr[pp + 4].plot(TheMonths[0:len(TheMonths) - 1],
                                   MSKALLarrMSK[sn, pp, :],
                                   c=cols[sn],
                                   linewidth=1)
        else:
            for sn in range(SourceCount):
                axarr[pp + 4].plot(TheMonths[0:len(TheMonths) - 1],
                                   MSKALLarrMSK[sn, pp, :],
                                   c=cols[sn],
                                   linewidth=2)

        axarr[pp + 4].annotate(Letteree[pp + 4] + ') ' + Titlee[pp],
                               xy=(0.02, 0.9),
                               xycoords='axes fraction',
                               size=16)

        # get linear trend and annotate (does this work with masked arrays?)
        if trendALLMSK[0, pp, 0] != TheMDI:
            scaletrend = ([0.32, 0.26, 0.2, 0.14, 0.08,
                           0.02])  #([0.88,0.82,0.76,0.70])
            if (SourceCount == 5):
                scaletrend = scaletrend = ([0.26, 0.2, 0.14, 0.08, 0.02])
            for sn in range(SourceCount):
                if (sn > 1):
                    mush = ' (masked)'
                else:
                    mush = ''
                linstr = '{0:5.2f}'.format(
                    trendALLMSK[sn, pp, 0]) + ' (' + '{0:5.2f}'.format(
                        trendALLMSK[sn, pp, 1]) + ',' + '{0:5.2f}'.format(
                            trendALLMSK[sn, pp, 2]) + ') ' + '{:s}'.format(
                                TheUnits) + ' dec$^{-1}$'
                namstr = ShortNameSource[sn] + ' (' + '{0:5.2f}'.format(
                    np.std(MSKALLarrMSK[sn, pp, :])) + ')'
                #linstr="%19s %5.2f (%5.2f to %5.2f) %s decade$^{-1}$ " % (NameSource[sn],trendGB[sn,0],trendGB[sn,1],trendGB[sn,2],TheUnits)
                axarr[pp + 4].annotate(namstr,
                                       xy=(0.5, scaletrend[sn]),
                                       xycoords='axes fraction',
                                       size=9,
                                       ha='left',
                                       color=cols[sn])
                axarr[pp + 4].annotate(linstr,
                                       xy=(0.72, scaletrend[sn]),
                                       xycoords='axes fraction',
                                       size=9,
                                       ha='left',
                                       color=cols[sn])

## get correlation and annotate (does this work with masked arrays?)
#        if corrALL[0,pp] != TheMDI:
#	    scaletrend=([0.02,0.08,0.14,0.2,0.26,0.32])
#	    point1=0
#	    point2=1
#	    for sn in range(len(corrALL[:,0])):
#	        linstr='r = '+'{0:6.3f}'.format(corrALL[sn,pp])
#	        #linstr="%19s %5.2f (%5.2f to %5.2f) %s decade$^{-1}$ " % (NameSource[sn],trendGB[sn,0],trendGB[sn,1],trendGB[sn,2],TheUnits)
#	        axarr[pp+4].annotate(ShortNameSource[point1]+','+ShortNameSource[point2],xy=(0.6,scaletrend[sn]),xycoords='axes fraction',size=10,ha='left',color='Black')
#	        axarr[pp+4].annotate(linstr,xy=(0.85,scaletrend[sn]),xycoords='axes fraction',size=10,ha='left',color='Black')
#		point2=point2+1
#		if (point2 == SourceCount):	# loop through
#	            point1=point1+1
#	            point2=1+point1

# Plot a line at zero
        if TypeTime == 'monthly':
            axarr[pp + 4].hlines(0,
                                 TheMonths[0],
                                 TheMonths[MonCount - 1],
                                 color='black',
                                 linewidth=0.5)
        else:
            axarr[pp + 4].hlines(0,
                                 TheMonths[0],
                                 TheMonths[YrCount - 1],
                                 color='black',
                                 linewidth=0.5)

        axarr[pp + 4].set_ylabel(ytitlee, fontsize=12)

    axarr[pp + 4].set_xlabel(xtitlee, fontsize=12)

    # Figure Watermark and Labels
    #    watermarkstring="/".join(os.getcwd().split('/')[4:])+'/'+os.path.basename( __file__ )+"   "+dt.datetime.strftime(dt.datetime.now(), "%d-%b-%Y %H:%M")
    #    plt.figtext(0.01,0.01,watermarkstring,size=6)

    #plt.show()
    plt.savefig(TheFile + ".eps")
    plt.savefig(TheFile + ".png")

    return  #PlotTimeSeries
Exemplo n.º 5
0
def PlotNiceTimeSeries(TheFile, TheHvars, TheUnitees, TheMCount, TheYCount,
                       TheTimeType, TheStYr, TheEdYr, TheMDI, TheColls,
                       TheParams):
    ''' Plot a panel for each element of TheHvars '''
    ''' Add Coverage, Sampling and Station uncertainty ranges '''
    ''' Add lines for any extra estimates (TheVars) and HadISDH MASKED versions '''
    ''' Save as png and eps '''
    ''' TheHvars is a multi-row array: rows for vars, columns for months '''
    ''' Ditto TheHuncs C=coverage, Sp=sampling, St=station '''
    ''' TheLablees is the name list for all other vars '''
    ''' TheUnitees is the units name list '''
    ''' TheVars is a multirow array if there is 1+ var available - or [] '''
    ''' TheMASKVars - ditto above but masked to HadISDH coverage '''
    ''' TheMCount - number of months, TheStYr/EdYr - start and end years '''
    ''' TheMDI - missing data indicator for masking '''
    ''' TheColls - dictionary of colours for each dataset '''

    # set up number of panels and number of lines
    nplots = len(TheParams[:])
    print('PLOT NUMBERS: ', nplots)
    #    nlines=[]
    #    for n in range(nplots):
    #        print(n,TheLablees[n][:])
    #        nlines.append(len(TheLablees[n][:]))

    Letteree = []
    Letteree = LetterRange(0, nplots)

    # set up x axes
    if TheTimeType == 'monthly':
        TheMonths = []
        yr = TheStYr
        mon = 1
        for m in range(TheMCount):
            TheMonths.append(dt.date(yr, mon, 1))
            mon = mon + 1
            if mon == 13:
                mon = 1
                yr = yr + 1
        TheMonths = np.array(TheMonths)
    else:
        TheMonths = []
        yr = TheStYr
        mon = 1
        for y in range(TheYCount):
            TheMonths.append(dt.date(yr, mon, 1))
            yr = yr + 1
        TheMonths = np.array(TheMonths)

    xtitlee = 'Years'

    # set up dimensions and plot
    xpos = []
    ypos = []
    xfat = []
    ytall = []
    totalyspace = 0.90  # start 0.08 end 0.98
    totalxspace = 0.84  # start 0.12 end 0.98
    for n in range(nplots):
        xpos.append(0.14)
        ypos.append(0.98 - ((n + 1) * (totalyspace / nplots)))
        xfat.append(totalxspace)
        ytall.append(totalyspace / nplots)

#    plt.clf()
#    fig = plt.figure(1,figsize=(8,12))
#    plt.axes([0.15,0.1,0.8,0.80])
    f, axarr = plt.subplots(7, figsize=(6, 12), sharex=True)  #6,18

    for pp in range(nplots):
        print('Plot: ', pp, TheParams[pp])
        #        print(TheHvars[pp,0:10])
        #        print(TheHuncsC[pp,0:10])
        #        print(TheHuncsSp[pp,0:10])
        #        print(TheHuncsSt[pp,0:10])
        #axarr[pp].set_size(14)
        axarr[pp].set_position([xpos[pp], ypos[pp], xfat[pp], ytall[pp]])
        if TheTimeType == 'monthly':
            axarr[pp].set_xlim([TheMonths[0], TheMonths[TheMCount - 1]])
        else:
            axarr[pp].set_xlim([TheMonths[0], TheMonths[TheYCount - 1]])
#	axarr[pp].set_ylim([math.floor(min(TheHvars[pp,:]-TheHuncsC[pp,:])),
#	                    math.ceil(max(TheHvars[pp,:]+TheHuncsC[pp,:]))])
#	if len(TheHuncsC[pp,:]) > 0:
#	    axarr[pp].fill_between(TheMonths,TheHvars[pp,:]+TheHuncsC[pp,:],TheHvars[pp,:]-TheHuncsC[pp,:],
#	                   facecolor='LightGray',edgecolor='none')
#	    axarr[pp].fill_between(TheMonths,TheHvars[pp,:]+TheHuncsSp[pp,:],TheHvars[pp,:]-TheHuncsSp[pp,:],
#	                   facecolor='LightSlateGray',edgecolor='none')
#	    axarr[pp].fill_between(TheMonths,TheHvars[pp,:]+TheHuncsSt[pp,:],TheHvars[pp,:]-TheHuncsSt[pp,:],
#	                   facecolor='LightSlateGray',edgecolor='none')

        if timetype == 'monthly':
            axarr[pp].plot(TheMonths,
                           TheHvars[pp, :],
                           c='black',
                           linewidth=0.5)
        else:
            axarr[pp].plot(TheMonths, TheHvars[pp, :], c='black', linewidth=2)

        axarr[pp].annotate(Letteree[pp] + ') ' + TheParams[pp],
                           xy=(0.03, 0.9),
                           xycoords='axes fraction',
                           size=10)

        # get the decadal linear trend and annotate
        TrendStats = np.empty(3)
        TrendStats = MedianPairwise(TheHvars[pp, :], TheMDI, TrendStats)
        if (timetype == 'monthly'):
            TrendStats = np.array(TrendStats) * 120.
        else:
            TrendStats = np.array(TrendStats) * 10.
        trendlinstr = '{0:5.2f}'.format(
            TrendStats[0]) + ' (' + '{0:5.2f}'.format(
                TrendStats[1]) + '  to ' + '{0:5.2f}'.format(
                    TrendStats[2]) + ') ' + '{:s}'.format(
                        TheUnitees[pp]) + ' decade$^{-1}$'

        axarr[pp].annotate(trendlinstr,
                           xy=(0.14, 0.9),
                           xycoords='axes fraction',
                           color='black',
                           size=10)
        #	for ll in range(nlines[pp]):	# no problem if 0
        #            print('Other: ',ll,TheLablees[pp][ll])
        ##	    axarr[pp].plot(TheMonths,TheVars[pp,ll,:],c=TheColls[TheLablees[pp][ll]],linewidth=0.5)
        ##	    axarr[pp].plot(TheMonths,TheMASKVars[pp,ll,:],c=TheColls[TheLablees[pp][ll]],linestyle='dotted',linewidth=0.5)
        #	    if timetype == 'monthly':
        #	        axarr[pp].plot(TheMonths,TheMASKVars[pp,ll,:],c=TheColls[TheLablees[pp][ll]],linewidth=0.5)
        #	    else:
        #	        axarr[pp].plot(TheMonths,TheMASKVars[pp,ll,:],c=TheColls[TheLablees[pp][ll]],linewidth=2)
        #
        #	    axarr[pp].annotate(TheLablees[pp][ll],xy=(0.14,0.82-(ll*0.08)),xycoords='axes fraction',
        #	               color=TheColls[TheLablees[pp][ll]],size=10)
        axarr[pp].set_ylabel(TheUnitees[pp], fontsize=10)
        if TheTimeType == 'monthly':
            axarr[pp].hlines(0,
                             TheMonths[0],
                             TheMonths[TheMCount - 1],
                             color='black')
        else:
            axarr[pp].hlines(0,
                             TheMonths[0],
                             TheMonths[TheYCount - 1],
                             color='black')

    axarr[nplots - 1].set_xlabel(xtitlee, fontsize=10)

    # Figure Watermark and Labels
    #    watermarkstring="/".join(os.getcwd().split('/')[4:])+'/'+os.path.basename( __file__ )+"   "+dt.datetime.strftime(dt.datetime.now(), "%d-%b-%Y %H:%M")
    #    plt.figtext(0.01,0.01,watermarkstring,size=6)

    #plt.show()
    plt.savefig(TheFile + ".eps")
    plt.savefig(TheFile + ".png")

    return  #PlotNiceDotsMap