def plot_pattcorrs(pcdf, pcdf2=None, rmin=None, axis=None, tftype='seasonal'): """ plot_pattcorrs(pcdf, axis): this figure plots range of pattern correlations as bars, with mean patt corr marker, plus % common variation annotated. pcdf: DataFrame of pattern correlations pcdf2: second set of data to plot rmin: minimum r value (pattern correlation) that is significant for the data axis: plot axis tftype: time frequency type: 'seasonal' or 'monthly' """ import math as math if tftype=='seasonal': numt=4 seasons=('SON','DJF','MAM','JJA') else: print 'ONLY SEASONAL IMPLEMENTED @@ 11/25/14' numt=12 # monthly seasons=con.get_mon() # within ensemble pattern correlation combinations # number of combinations: n choose k (here k=2) # n! / (k! *(n-k)!) ncomb = math.factorial(len(pcdf)) / (2 * math.factorial(len(pcdf)-2) ) # first, create matrix of data to take mean, max, min, etc pcstack = np.zeros((ncomb,numt)) # ncomb combos x 4 seasons (or 12 mon) stackii=0 keys=pcdf.keys() for eii,skey1 in enumerate(keys): for ineii,skey2 in enumerate(keys[eii+1:len(keys)]): # stack the pc's so I can take max/min, etc pcstack[stackii] = pcdf[skey1][skey2] stackii+=1 mxsea = np.max(pcstack,axis=0) mnsea = np.min(pcstack,axis=0) avgsea = np.mean(pcstack,axis=0) tmpstack=copy.copy(pcstack) tmpstack[pcstack<0] = 0 # get rid of negative correlations. they are zero for all intents & purposes in this case pcstacksq = np.power(tmpstack,2) # square the patt corr (the ones that are positive) avgseasq = np.mean(pcstacksq,axis=0) # average squared patt corr (coef of determination) if not pcdf2.empty: ncomb2 = math.factorial(len(pcdf2)) / (2 * math.factorial(len(pcdf2)-2) ) pcstack2 = np.zeros((ncomb2,numt)) # ncomb combos x 4 seasons stackii=0 keys=pcdf2.keys() for eii,skey1 in enumerate(keys): for ineii,skey2 in enumerate(keys[eii+1:len(keys)]): # stack the pc's so I can take max/min, etc pcstack2[stackii] = pcdf2[skey1][skey2] stackii+=1 mxsea2 = np.max(pcstack2,axis=0) mnsea2 = np.min(pcstack2,axis=0) avgsea2 = np.mean(pcstack2,axis=0) tmpstack2=copy.copy(pcstack2) tmpstack2[pcstack2<0] = 0 # get rid of negative correlations. they are zero for all intents & purposes in this case pcstacksq2 = np.power(tmpstack2,2) # square the patt corr (the ones that are positive) avgseasq2 = np.mean(pcstacksq2,axis=0) # average squared patt corr (coef of determination) # add annual mean annwgts = np.array((91,90,92,92))/365. print annwgts #fig,axs = plt.subplots(1,1) #fig.set_size_inches(6,4) # more squat ax = axis #[0] legtpl=() wi=0.1 # width of bar incr=0.3 # how much to shift in b/w the 2 sets of data xxsea2=np.arange(1,6) xboxmin=xxsea2-.2 if rmin!=None: ax.axhspan(-1*rmin,rmin,color='orange',alpha=.3) # shade where corr is NOT significant fillcol='0.7' fillcol2=ccm.get_linecolor('dodgerblue') for boxii in range(0,4): # loop through seasons # shaded bars/boxes ax.fill_between((xboxmin[boxii],xboxmin[boxii]+wi),mnsea[boxii],mxsea[boxii],color=fillcol, alpha=.5) if not pcdf2.empty: ax.fill_between((xboxmin[boxii]+incr,xboxmin[boxii]+incr+wi),mnsea2[boxii], mxsea2[boxii],color=fillcol2, alpha=.5) # markers ax.plot(xboxmin[boxii]+wi/2.,avgsea[boxii],color='k',marker='_',linestyle='none',markersize=15)#,alpha=.7) if not pcdf2.empty: ax.plot(xboxmin[boxii]+wi/2.+incr,avgsea2[boxii],color='b',marker='_',linestyle='none',markersize=15)#,alpha=.7) # mean values (text) val = '$%.0f$'%(avgseasq[boxii]*100) # @@ square the corrs before taking mean ax.annotate(val+'%', xy=(xboxmin[boxii]+wi/2. -.09, .95), xycoords='data') if not pcdf2.empty: val = '$%.0f$'%(avgseasq2[boxii]*100) # @@ square the corrs before taking mean ax.annotate(val+'%', xy=(xboxmin[boxii]+wi/2.+incr -.06, .95), xycoords='data') boxii=boxii+1 # add annual mean ----------- annwgtst = np.tile(annwgts,(len(pcdf),1)) # tile # each ens w/ each other # pcstacksq is ncomb x numt annwgtst = np.tile(annwgts,(ncomb,1)) # tile ann = np.average(pcstack,weights=annwgts,axis=1) # ann mean per patt corr annmax = np.max(ann) annmin = np.min(ann) avgann = np.mean(ann) annsq = np.average(pcstacksq,weights=annwgts,axis=1) # ann mean per patt corr avgannsq = np.mean(annsq) if not pcdf2.empty: # add annual mean annwgtst = np.tile(annwgts,(len(pcdf2),1)) # tile # each ens w/ each other # pcstacksq is ncomb x numt annwgtst = np.tile(annwgts,(ncomb2,1)) # tile ann = np.average(pcstack2,weights=annwgts,axis=1) # ann mean per patt corr annmax2 = np.max(ann) annmin2 = np.min(ann) avgann2 = np.mean(ann) annsq2 = np.average(pcstacksq2,weights=annwgts,axis=1) # ann mean per patt corr avgannsq2 = np.mean(annsq2) # plot annual mean markers ax.fill_between((xboxmin[boxii],xboxmin[boxii]+wi),annmin,annmax,color=fillcol,alpha=.5) if not pcdf2.empty: ax.fill_between((xboxmin[boxii]+incr,xboxmin[boxii]+incr+wi),annmin2,annmax2,color=fillcol2,alpha=.5) ax.plot(xboxmin[boxii]+wi/2.,avgann,color='k',marker='_',linestyle='none',markersize=15)#,alpha=.9) if not pcdf2.empty: ax.plot(xboxmin[boxii]+wi/2.+incr,avgann2,color='b',marker='_',linestyle='none',markersize=15)#,alpha=.9) val = '$%.0f$'%(avgannsq*100) # @@ square the corrs before taking mean ax.annotate(val+'%', xy=(xboxmin[boxii]+wi/2. -.09, .95), xycoords='data') if not pcdf2.empty: val = '$%.0f$'%(avgannsq2*100) # @@ square the corrs before taking mean ax.annotate(val+'%', xy=(xboxmin[boxii]+wi/2.+incr -.06, .95), xycoords='data') ax.set_ylabel('Pattern Correlation') ax.set_xlim((.5,5.5)) ax.set_xticks(xxsea2) ax.set_xticklabels((seasons)+('ANN',)) ax.set_ylim((0,1)) ax.grid(True)
## darkolivegreen3 = np.array([162, 205, 90])/255. ## darkseagreen = np.array([143, 188, 143])/255. ## darkseagreen4 = np.array([105, 139, 105])/255. ## dodgerblue = np.array([30, 144, 255])/255. ## orangered4 = np.array([139, 37, 0])/255. ## lightsteelblue3 = np.array([162, 181, 205])/255. # more grey looking ## lightsteelblue4 = np.array([110, 123, 139])/255. # more grey looking ## steelblue3 = np.array([79, 148, 205])/255. # more blue looking ## steelblue4 = np.array([54, 100, 139])/255. # more blue looking ## colordict = {'kemctl1r1': darkseagreen, 'kemctl1r2': darkseagreen4, 'kemctl1r3': lightsteelblue3, ## 'kemctl1r4': lightsteelblue4, 'kemctl1r5': steelblue4, 'kemctl1ens': dodgerblue, ## 'kemctl1': orangered4, 'kemhadctl': darkolivegreen3 } colordict = {'r1': ccm.get_linecolor('warm1'), #'firebrick'), 'r4': ccm.get_linecolor('warm2'), #'firebrick1'), 'r3': ccm.get_linecolor('warm3'), #'yelloworange'),#'chocolate1'), 'r5': ccm.get_linecolor('warm4'), #'darkyellow') #'skyblue'), #yelloworange'), 'r2': ccm.get_linecolor('warm5'), #'steelblue3'), #'darkgoldenrod1'), 'ens': ccm.get_linecolor('magenta'), '': ccm.get_linecolor('mediumblue'), #'mediumpurple1'), #darkyellow'), # @@ empty key? 'kemhad': ccm.get_linecolor('deepskyblue')} # Load data into dicts # order ens simulations in order of most ice loss in melt season to least. Then ens mean, PERT2, observations if requested ## sims = bcasename+'r1', bcasename+'r4', bcasename+'r3', bcasename+'r5', bcasename+'r2', bcasename+'ens',bcasename ## if addobs: ## sims = sims + ('kemhadctl',)
fig,axs = plt.subplots(1,1) fig.set_size_inches(6,4) # more squat ax = axs #[0] legtpl=() wi=0.1 # width of bar incr=0.3 # how much to shift in b/w the 2 sets of data xxsea2=np.arange(1,6) xboxmin=xxsea2-.2 ax.axhspan(-1*rmin,rmin,color='orange',alpha=.3) # shade where corr is NOT significant fillcol='0.7' fillcol2=ccm.get_linecolor('dodgerblue') for boxii in range(0,4): # loop through seasons # shaded bars/boxes ax.fill_between((xboxmin[boxii],xboxmin[boxii]+wi),ensminsea[boxii],ensmaxsea[boxii],color=fillcol) ax.fill_between((xboxmin[boxii]+incr,xboxmin[boxii]+incr+wi),mnsea[boxii], mxsea[boxii],color=fillcol2, alpha=.5) # markers ax.plot(xboxmin[boxii]+wi/2.,ensmeansea[boxii],color='k',marker='_',linestyle='none',markersize=15)#,alpha=.9) ax.plot(xboxmin[boxii]+wi/2.+incr,avgsea[boxii],color='b',marker='_',linestyle='none',markersize=15)#,alpha=.7) # mean values (text) #val = '$%.0f$'%(ensmeansea2[boxii]*ensmeansea2[boxii]*100) val = '$%.0f$'%(ensmeanseasq[boxii]*100)# @@ square the corrs before taking mean #print val #print ensmeansea2[boxii]*ensmeansea2[boxii]*100 ax.annotate(val+'%', xy=(xboxmin[boxii]-.07, .95), xycoords='data')
# dblob should have regional means *with* time dimension # want to test sig different mean and variance b/w ctl and pert allstats=sh.calc_runstats(dblob,sims, seas=seasons,siglevel=siglevel) fpvaldf=pd.DataFrame(allstats['fpval']) # pval of f statistic for variance significance fpvaldft=fpvaldf.transpose() tpvaldf=pd.DataFrame(allstats['tpval']) # pval of t statistic for mean significance tpvaldft=tpvaldf.transpose() import cccmacmaps as ccm if plottype=='calcregunccascade': effdof=False col=('0.3',ccm.get_linecolor('firebrick')) if field=='st' and region in ('eurasia','eurasiamori'): #xlab = '$\Delta$ Eurasia SAT ($^\circ$C)' xlab = '($^\circ$C)' elif field=='st' and region=='polcap60': #xlab = '$\Delta$ >60$^\circ$N SAT ($^\circ$C)' xlab = '($^\circ$C)' else: xlab=None for sea in seasons: seas=(sea,) xlims=None fig,axs = plt.subplots(2,1) fig.set_size_inches(4,8) fig.set_frameon(False)
casename + '_r' + str(eii) + 'i1p1_185001-201212.nc' orignatdt[eii] = cnc.getNCvar(fname,field,timesel=origsel,seas=season) orignatdf=pd.DataFrame(orignatdt,index=origyrs) pastnatorig = orignatdf.loc[subyrs] presnatorig = orignatdf.loc[subyrs2] (natorigtstat,natorigpv) = cutl.ttest_ind(presnatorig,pastnatorig,axis=0) if dopct: diffnatorig = (presnatorig.mean(axis=0) - pastnatorig.mean(axis=0)) / pastnatorig.mean(axis=0)*100 else: diffnatorig = presnatorig.mean(axis=0) - pastnatorig.mean(axis=0) # TIMESERIES firebrick=ccm.get_linecolor('firebrick') hcol=ccm.get_linecolor('darkolivegreen3') hcolline=ccm.get_linecolor('darkolivegreen3')#'darkseagreen4') natcol=ccm.get_linecolor('steelblue3') natcolline=ccm.get_linecolor('steelblue3')#4') fig,axs=plt.subplots(1,1) axs.plot(years,allnatdf,color=natcol,alpha=.5) axs.plot(years,allflddf,color='r',alpha=.3) axs.plot(origyrs,orignatdf,color=natcol,linewidth=2) axs.plot(origyrs,origdf,color='brown',linewidth=2) axs.plot(hadyrs,haddf,color='b',linewidth=2) axs.plot(nsidcyrs,nsidcdf,color='g',linewidth=2) axs.set_title(field + ' ' + str(season))
fieldr2='tas'; ncfieldr2='tas'; compr2='Amon'; regionr2='eurasiamori'; rstr2='Eur SAT'; rstr2long='Eurasian SAT'; runits2='$^\circ$C'; rkey2='eursat' convr2=1 aconvr2=1 # for agcm sims sear2='DJF' sear='DJF' timeselc='1979-01-01,1989-12-31' timeselp='2002-01-01,2012-12-31' lecol='red' ocol='green' picol = '0.8' acol=ccm.get_linecolor('paperblue') when='14:51:28.762886'; styearsR = [ 8., 7., 2., 8., 8.] # variable SIC styears styearsE=[ 4., 1., 7., 3., 1.]; styearsN=[1.] #when for these: 17:01:16.908687 def load_pifield(fdict,seas,conv=1,subsampyrs=11,numsamp=50, styear=None,anomyears=None,local=False,detrend=True, verb=False,addcyc=False): """ TAKEN FROM load_canesmfield() in canesm_LE_composite.py loads subsampled CGCM data from specified simulation (assumes a long control, e.g. piControl) number of total chunks will be determined by length of sim data and numyrs (ntime / numyrs). First the simulation is chunked into
#rdf=df.ix[:,'R1':'R5'] #edf=df.ix[:,'E1':'E5'] #regressdf = rdf.append(edf) # didn't append like I expected. inserted NaNs in places # this will work: allens=['R1','R2','R3','R4','R5','E1','E2','E3','E4','E5'] # has to be an array! rens=['R1','R2','R3','R4','R5'] eens=['E1','E2','E3','E4','E5'] sub = df.loc[:,allens] subx=sub.ix[0,:] suby=sub.ix[1,:] mm, bb, rval, pval, std_err = sp.stats.linregress(subx,suby) #sube = df.loc[0,['E1','E2','E3','E4','E5']] firebrick=ccm.get_linecolor('firebrick') printtofile=True fig,ax = plt.subplots(1) fig.set_size_inches(6,5) rr = plt.scatter(df.filter(regex='R').values[0],df.filter(regex='R').values[1], color='0.3',marker='o',s=8**2,alpha=0.7) ee = plt.scatter(sub.ix[0,5:],sub.ix[1,5:], color=firebrick,marker='o',s=8**2,alpha=0.7) #plt.scatter(df['HAD'].values[0],df['HAD'].values[1],color=cd['HAD'],marker='s',s=8**2) #ns=plt.scatter(df['NSIDC'].values[0],df['NSIDC'].values[1],color=cd['NSIDC'],marker='o',s=8**2,alpha=0.7) # @@@@ add back? ns=plt.scatter(df['NSIDC'].values[0],df['NSIDC'].values[1],color='green',marker='o',s=8**2,alpha=0.7) #plt.scatter(df['ENS'].values[0],df['ENS'].values[1],color=cd['ENS'],marker='s',s=10**2) #plt.scatter(df['ENSE'].values[0],df['ENSE'].values[1],color=cd['ENSE'],marker='o',s=10**2) axylims = ax.get_ylim()
rmse = np.sqrt(np.square(flddiffdt[skey])) rmseclim = np.sqrt(np.square(flddiffclimdt[skey])) rmsedt[skey] = cutl.calc_regmean(rmse, lat, lon, region) rmseclimdt[skey] = cutl.calc_regmean(rmseclim, lat, lon, region) annrmseclimdt[skey] = cutl.annualize_monthlyts(rmseclimdt[skey]) climrmsedt[skey], rmsestd = cutl.climatologize(rmsedt[skey]) # <codecell> from matplotlib import gridspec colors = ("b", "g", "m", "y", "c", "k", "r", ccm.get_linecolor("mediumpurple4")) cdict = { "iga": "0.5", "gregory_2xco2": "0.3", "kel11": "b", "kel09": "g", "kel14": "m", "kel15": "y", "kel17": "c", "kel18": "k", "kel20": "r", "kel24": ccm.get_linecolor("limegreen"), "kel10": "b", "kel12": "g", "kel16": "m", "kel19": "y",
montrenddf = pd.DataFrame(montrenddt) mndf = pd.DataFrame(monmndt) mxdf = pd.DataFrame(monmxdt) avgdf = pd.DataFrame(monavgdt) superensdf=pd.DataFrame(data=superslopes,index=mons,columns=superkeys[0]) canesm=superensdf.CanESM2 # plot seasonal trends for all ensemble members hmontrenddf = pd.DataFrame(hmontrenddt,index=(1,)) nmontrenddf = pd.DataFrame(nmontrenddt,index=(1,)) superensdf.plot(color='k',alpha=0.5,legend=False) plt.plot(canesm,color='r',linewidth=3) plt.plot(hmontrenddf.values[0],'green',linewidth=3) # not sure why but this has an array w/in an array.... plt.plot(nmontrenddf.values[0],color=ccm.get_linecolor('dodgerblue'),linewidth=3)# not sure why but this has an array w/in an array.... plt.title('SIE 1979-2012 trends (/yr)') plt.xlabel('Month') plt.xlim((0,11)) if printtofile: plt.savefig('SIE_CMIP5_allmemstrend_seacycle_1979-2012_wCanESM2.pdf') #superensdf.hist() # min / max histograms each month fig,axs = plt.subplots(3,4) fig.set_size_inches(12,9) for aii,ax in enumerate(axs.flat): mon = mons[aii] ax.hist(mndf[mon],color='.5',alpha=0.5)