def plot_results(clustered,uncert,expname,lat5,lon5,outfigpath,title=None): # Set some defaults ucolors = ('Blues','Purples','Greys','Blues','Reds','Oranges','Greens') proj = ccrs.PlateCarree(central_longitude=180) cmap = cm.get_cmap("jet",nclusters) fig,ax = plt.subplots(1,1,subplot_kw={'projection':proj}) ax = slutil.add_coast_grid(ax) gl = ax.gridlines(ccrs.PlateCarree(central_longitude=0),draw_labels=True, linewidth=2, color='gray', alpha=0.5, linestyle="dotted",lw=0.75) gl.xlabels_top = False gl.ylabels_right = False pcm = ax.pcolormesh(lon5,lat5,clustered,cmap=cmap,transform=ccrs.PlateCarree())#,cmap='Accent')#@,cmap='Accent') plt.colorbar(pcm,ax=ax,orientation='horizontal') if title is None: ax.set_title("Clustering Results \n nclusters=%i %s" % (nclusters,expname)) else: ax.set_title(title) plt.savefig("%sCluster_results_n%i_%s.png"%(outfigpath,nclusters,expname),dpi=200,transparent=True) # Plot raw uncertainty fig,ax = plt.subplots(1,1,subplot_kw={'projection':proj}) ax = slutil.add_coast_grid(ax) pcm = plt.pcolormesh(lon5,lat5,uncert,cmap='copper',transform=ccrs.PlateCarree()) ax.set_title(r"Uncertainty $(<\sigma^{2}_{out,x}>/<\sigma^{2}_{in,x}>)$") fig.colorbar(pcm,ax=ax,fraction=0.02) plt.savefig(outfigpath+"Uncertainty.png",dpi=200) uncertraw = uncert.copy() # Apply Thompson and Merrifield thresholds uncert[uncert>2] = 2 uncert[uncert<0.5]=0 # Plot Cluster Uncertainty fig1,ax1 = plt.subplots(1,1,subplot_kw={'projection':proj}) ax1 = slutil.add_coast_grid(ax1) for i in range(nclusters): cid = i+1 if (i+1) > len(ucolors): ci=i%len(ucolors) else: ci=i cuncert = uncert.copy() cuncert[clustered!=cid] *= np.nan ax1.pcolormesh(lon5,lat5,cuncert,vmin=0,vmax=2,cmap=ucolors[ci],transform=ccrs.PlateCarree()) #fig.colorbar(pcm,ax=ax) if title is None: ax1.set_title("Clustering Output (nclusters=%i) %s "% (nclusters,expname)) else: ax1.set_title(title) plt.savefig(outfigpath+"Cluster_with_Shaded_uncertainties_%s.png" % expname,dpi=200) return fig,ax,fig1,ax1
#% Remove NaN points and Examine Low pass filter slars = sla_lp.reshape(ntimer, nlat5 * nlon5) # --- # Locate points where values are all zero # --- tsum = slars.sum(0) zero_pts = np.where(tsum == 0)[0] ptmap = np.array(tsum == 0) slars[:, zero_pts] = np.nan ptmap = ptmap.reshape(nlat5, nlon5) # Map removed points fig, ax = plt.subplots( 1, 1, subplot_kw={'projection': ccrs.PlateCarree(central_longitude=0)}) ax = slutil.add_coast_grid(ax) pcm = ax.pcolormesh(lon5, lat5, ptmap, cmap='bone', transform=ccrs.PlateCarree(), alpha=0.88) fig.colorbar(pcm, ax=ax) ax.set_title("Removed Zero Points") # --- # Visualize Filter Transfer Function # --- okdata, knan, okpts = proc.find_nan(slars, 0) npts5 = okdata.shape[1] lpdata = okdata.copy()
# Recover clusterout for silhouette plotting remmask = rempts_in.copy() remmask[~np.isnan(remmask )] = np.nan # Convert all removed points to NaN remmask[np.isnan(rempts_in)] = 1 clusterout, knan, okpts = proc.find_nan( (clusternew * remmask).flatten(), 0) # Ugly Fix, but not sure why sometimes s_in size doesnt match clusterin (non-nan) if len(clusterout) != len(s_in): clusterout, knan, okpts = proc.find_nan((clusternew).flatten(), 0) # Upper Row, the clustering result ax = axs[coll, 0] #fig,ax = plt.subplots(1,1,subplot_kw={'projection':proj}) ax = slutil.add_coast_grid(ax, leftlab=False, botlab=False) pcm = ax.pcolormesh(lon5, lat5, clusternew, cmap=cmapn, transform=ccrs.PlateCarree()) fig.colorbar(pcm, ax=ax, fraction=0.025) ax.set_title("Clusters (Year %s)" % (yearstr)) #plt.savefig("%sCESM1PIC_%s_ClustersMap_k%s%i_%s.png"%(outfigpath,expname,kmode,ii,yearstr),dpi=200,bbox_inches='tight') # Plot 1: the silhoutte value map ax = axs[coll, 1] cmap = "RdBu_r" silmap = np.zeros(nlat5 * nlon5) * np.nan silmap[okpts] = s_in silmap = silmap.reshape(nlat5, nlon5)
tw = 18 datname = "CESM_PIC_remGMSL%i" % (rem_gmsl) outname = "%sSSHA_LP_%s_order%i_cutoff%i.npz" % (datpath,datname,order,tw) ld = np.load(outname,allow_pickle=True) cesm_sla = ld['sla_lp'] ctimes = ld['times'] #%% Make a plot comparing their differences proj = ccrs.PlateCarree(central_longitude=180) fig,axs = plt.subplots(1,2,figsize=(10,3.5),subplot_kw={'projection':proj}) vm = .1 cmap = 'copper' ax = axs[0] ax = slutil.add_coast_grid(ax=ax,proj=proj) pcm = ax.pcolormesh(lon5,lat5,aviso_sla.std(0),vmin=0,vmax=vm,transform=ccrs.PlateCarree(),cmap=cmap) #fig.colorbar(pcm,ax=ax) ax.set_title("AVISO (1993-2013)") ax = axs[1] ax = slutil.add_coast_grid(ax=ax,proj=proj) pcm = ax.pcolormesh(lon5,lat5,cesm_sla.std(0),vmin=0,vmax=vm,transform=ccrs.PlateCarree(),cmap=cmap) ax.set_title("CESM1 Preindustrial Control (400-2200)") fig.colorbar(pcm,ax=axs.ravel().tolist(),orientation='horizontal',shrink=0.45,pad=0.08) plt.suptitle("$1\sigma$ SSH Anomalies (m)",y=.95) plt.savefig("%sAVISO-CESMPIC-Compare.png"%(outfigpath),dpi=200,transparent=True)
stat = sscore labs = True fig, axs = plt.subplots(2, 2, subplot_kw={'projection': proj}, figsize=(10, 7)) for m in range(2): name = mnl[m] instat = sscore[m] inclust = clusters[m] kmin = np.argmin(instat) kmax = np.argmax(instat) ax = axs[m, 0] ax = slutil.add_coast_grid(ax, proj=proj, leftlab=labs, botlab=labs) pcm = ax.pcolormesh(lon, lat, inclust[kmin, :, :], cmap=cmapn, transform=ccrs.PlateCarree()) ax.set_title("%s \n Min Silhouette Score (%.2e)" % (name, instat[kmin])) ax = axs[m, 1] ax = slutil.add_coast_grid(ax, proj=proj, leftlab=labs, botlab=labs) pcm = ax.pcolormesh(lon, lat, inclust[kmax, :, :], cmap=cmapn, transform=ccrs.PlateCarree()) ax.set_title("%s \n Max Silhouette Score (%.2e)" % (name, instat[kmax]))