import sys, datetime, copy, os from dateutil.relativedelta import relativedelta import numpy as np; np.set_printoptions(threshold=sys.maxsize) import matplotlib; matplotlib.use('Agg') import matplotlib.pyplot as plt import matplotlib.colors as clr from mpl_toolkits.basemap import Basemap import seaborn as sns from matplotlib.colors import ListedColormap from netCDF4 import Dataset # by iterating over all properties of the box plot def define_box_properties(plot_name, color_code, label, idx): for k, v in plot_name.items(): plt.setp(plot_name.get(k), color=color_code) # use plot function to draw a small line to name the legend. plt.plot([], c=color_code, label=label) if idx == 0: plt.legend(loc='upper left',frameon=False,bbox_to_anchor=(0.675, 4.),ncols=2) # #nc = Dataset('/work/users/hmo/remoclic/Phuong/PHD/BC_2023/Tan/NC1/T_R_season.nc','r') #FD_Char_1961-2020_1yrs_Daily_Reg.nc','r') nc = Dataset('/work/users/hmo/remoclic/Phuong/PHD/BC_2023/Tan/NC1/All_Cha_landpoints.nc','r') # cha = 4 ; # mem = 7 ; # point = 11717 ; Model = nc.variables['mem'][:] Season = nc.variables['cha'][:] month = nc.variables['point'][:] r_org = nc.variables['aph'][:] r_qm = nc.variables['org'][:] t_org = nc.variables['qm'][:] #t_qm = nc.variables['tas_qm'][:] xlabels = ['B1','B2','B3','B4','N1','N2','N3'] title = ["DJF","MAM","JJA","SON"] #Amax = [ 7, 150, 4000, 85, 35, 35 ] Amax = [ 20, 100, 100, 3 ] nbox = len(Season) #fig, ax = plt.subplots(nbox,figsize=(6,10),sharey=False) fig, ax = plt.subplots(nbox,figsize=(8,10),sharey=False) plt.subplots_adjust(hspace=0.4) plt.subplots_adjust(top=1) #plt.subplots_adjust(left=0.01) #plt.subplots_adjust(right=0.02) for k in range(len(Season)): # ymax = round(np.nanmax(data)); ymin = round(np.nanmin(data)) ymax = Amax[k]; ymin = 0 yticks = np.linspace(ymin,ymax,5) # https://www.geeksforgeeks.org/how-to-create-boxplots-by-group-in-matplotlib/ # org = ax[k].boxplot(r_org[:,k,:], positions=np.array(np.arange(len(xlabels)))*2.0-0.35, sym='k+', widths=0.6) # qm = ax[k].boxplot(r_qm[:,k,:], positions=np.array(np.arange(len(xlabels)))*2.0+0.35, sym='k+', widths=0.6) org = ax[k].boxplot(r_org[:,:,k], positions=np.array(np.arange(len(xlabels)))*2.0-0.35, sym='k+', widths=0.6) qm = ax[k].boxplot(r_qm[:,:,k], positions=np.array(np.arange(len(xlabels)))*2.0+0.35, sym='k+', widths=0.6) # ax[k].set_title('var'+str(k+1)) ax[k].set_title(title[k]) ax[k].set_xticks(np.arange(0, len(xlabels) * 2, 2), xlabels) ax[k].set_xlim(-2, len(xlabels)*2) ax[k].set_ylim(ymin,ymax) #ax[k].set_yticks(yticks) define_box_properties(org, '#D7191C', 'R_org', k) define_box_properties(qm, '#2C7BB6', 'R_QM', k) #plt.show() #fig.savefig('box_model_new.pdf', bbox_inches="tight", pad_inches=.05, dpi=600) fig.savefig('box_model_new.png', bbox_inches="tight", pad_inches=.05, dpi=600)