#!/usr/bin/env python # Wenchang Yang (wenchang@princeton.edu) # Tue Feb 7 14:27:44 EST 2023 if __name__ == '__main__': import sys,os from misc.timer import Timer tt = Timer(f'[{os.getcwd()}] start ' + ' '.join(sys.argv)) import sys, os.path, os, glob, datetime import xarray as xr, numpy as np, pandas as pd, matplotlib.pyplot as plt #more imports from misc import get_kws_from_argv # if __name__ == '__main__': tt.check('end import') # #start from here basin = 'global' ws = 17 ws = get_kws_from_argv('ws', ws) ws = int(ws) long_name = f'{basin}TC{ws} plus2K' models = ['AM2.5', 'HIRAM', 'AM2.5C360'] dss = [] for model in models: ifile = f'data/wyplot_dTC_{basin}{ws}{model}_{basin}{ws}{model}plus2K.nc' #wyplot_dTC_global17FLOR_global17FLORktc2.nc ds = xr.open_dataset(ifile) dss.append(ds) ds = xr.concat(dss, dim=pd.Index(models, name='model')) units = '%' ds['dTC'] = ds['x1m_minus_x2m']/ds['x2m']*100 ds['dTC'].attrs['units'] = units ds['dTCe'] = ds['err']/ds['x2m']*100 ds['dTCe'].attrs['units'] = units df = ds['dTC'].to_pandas() df_e = ds['dTCe'].to_pandas() df_ref = ds['x2m'].to_pandas() df_new = ds['x1m'].to_pandas() if __name__ == '__main__': from wyconfig import * #my plot settings n_models = ds.model.size colors = [f'C{ii}' for ii in range(n_models)] df.plot.bar(color=colors, rot=0, yerr=df_e, capsize=3) ax = plt.gca() for ii in range(n_models): ax.text(ii, df[ii], f'{df[ii]/100:.1%} $\pm$ {df_e[ii]/100:.1%}\n{df_ref[ii]:.1f}to{df_new[ii]:.1f}\n\n', ha='center', va='bottom') ax.axhline(0, color='gray', ls='--') ax.set_ylabel(units) ax.set_xlabel('') ax.set_title(long_name) #if ws==17: ax.set_ylim(-20,20) if ws==17: ax.set_ylim(-20,70) if ws==29: ax.set_ylim(-40,120) #savefig if 'savefig' in sys.argv or 's' in sys.argv: figname = __file__.replace('.py', f'_{basin}{ws}.png') if 'overwritefig' in sys.argv or 'o' in sys.argv: wysavefig(figname, overwritefig=True) else: wysavefig(figname) tt.check(f'**Done**') print() if 'notshowfig' in sys.argv or 'n' in sys.argv: pass else: if 'plt' in globals(): plt.show()