def plot_two_variables(lat1, lon1, var1, lat2, lon2, var2, figsize=None, region_limit=None, cl_res=None, xtick=np.arange(-180, 180.1, 5), ytick=np.arange(-90, 90.1, 5), xtick1=None, ytick1=None, xtick2=None, ytick2=None, title1=None, title2=None, vmin1=None, vmax1=None, vmin2=None, vmax2=None, cl_color2='black', lw1=1, lw2=1, unit1='', unit2='', y_offset=-0.08, cmap1=None, cmap2=None): """ Plot two variables Parameters ---------- """ out_data = {} out_data['pout'] = [] # figure and ax_list if figsize is None: figsize = (10, 5) fig = plt.figure(figsize=figsize) ax_list = [] # ticks if xtick1 is None: xtick1 = xtick if ytick1 is None: ytick1 = ytick if xtick2 is None: xtick2 = xtick if ytick2 is None: ytick2 = [] # colorbar paramters h = 0.03 # plot var1 ax1 = add_geoaxes(fig, 121, title=title1, xtick=xtick1, ytick=ytick1, cl_res=cl_res, lw=lw1) pout1 = cartopy_plot(lon1, lat1, var1, ax=ax1, vmin=vmin1, vmax=vmax1, cbar=False, cmap=cmap1) ax1.reset_position() ax_list.append(ax1) out_data['pout'].append(pout1) pos1 = pout1['ax'].get_position() cax1 = fig.add_axes([pos1.x0, pos1.y0 + y_offset, pos1.width, h]) cb1 = plt.colorbar(pout1['mesh'], cax=cax1, orientation='horizontal', extend='max') cb1.set_label(unit1) # plot var2 ax2 = add_geoaxes(fig, 122, title=title2, xtick=xtick2, ytick=ytick2, cl_res=cl_res, lw=lw2, cl_color=cl_color2) ax_list.append(ax2) pout2 = cartopy_plot(lon2, lat2, var2, ax=ax2, vmin=vmin2, vmax=vmax2, cbar=False, cmap=cmap2) out_data['pout'].append(pout2) pos2 = pout2['ax'].get_position() cax2 = fig.add_axes([pos2.x0, pos2.y0 + y_offset, pos2.width, h]) cb2 = plt.colorbar(pout2['mesh'], cax=cax2, orientation='horizontal', extend='max') cb2.set_label(unit2) # set region limit if region_limit is not None: for i in range(len(ax_list)): ax = ax_list[i] ax.set_xlim(region_limit[1], region_limit[3]) ax.set_ylim(region_limit[0], region_limit[2]) return out_data
import matplotlib.pyplot as plt import numpy as np from mylib.cartopy_plot import add_geoaxes, pcolormesh from mylib.pro_modis.read_modis import get_modis_1km_rgb #filename = './MYD021KM.A2015344.0725.006.2015344193947.hdf' filename = 'MYD021KM.A2018301.1825.061.2018302153109.hdf' # get data lat, lon, rgb = get_modis_1km_rgb(filename) # plot fig = plt.figure() xtick = np.arange(-180, 180.1, 5) ytick = np.arange(-90, 90.1, 5) ax = add_geoaxes(fig, 111, xtick=xtick, ytick=ytick) title = '.'.join(filename.split('/')[-1].split('.')[0:4]) pcolormesh(ax, lon, lat, rgb, title=title) figname = './' + title + '.png' plt.savefig(figname, format='png', dpi=300)
# get latitude and longitude lon_c, lat_c = np.meshgrid(mo_lon, mo_lat) lat_e, lon_e = calculate_pixel_edge2(lat_c, lon_c) #print(np.min(lat_c), np.max(lat_c)) #print(np.min(lon_c), np.max(lon_c)) # #print(np.min(lat_e), np.max(lat_e)) #print(np.min(lon_e), np.max(lon_e)) # fig fig = plt.figure(figsize=(6,7)) # average of AOD ave_AOD = np.mean(AOD, axis=0) ax1 = add_geoaxes(fig, 211) cb1_prop = dict() cb1_prop['orientation'] = 'horizontal' cb1_prop['shrink'] = 0.7 cb1_prop['aspect'] = 25 cb1_prop['pad'] = 0.12 cb1_prop['extend'] = 'max' res1 = pcolormesh(ax1, lon_e, lat_e, ave_AOD, vmin=0.0, vmax=1.0, cbar=True, cbar_prop=cb1_prop) cb1 = res1['cb'] cb1.set_label('Average AOD [unitless]') # AOD trend trend = np.zeros(ave_AOD.shape) trend_sigma = np.zeros(ave_AOD.shape) for i in range(ave_AOD.shape[0]):
def compare_two_varilables(filename, varname1, varname2, vmin=0.0, vmax=0.8, diff_min=None, diff_max=None, seperate_cbar=False, region_limit=None, sc_xlabel='', sc_ylabel='', sc_max=1.0, verbose=True): """ """ # read data coord_varns = ['Latitude_e', 'Longitude_e'] some_varns = [varname1, varname2] varnames = coord_varns + some_varns data_dict = read_nc(filename, varnames, verbose=verbose) # region_limit if region_limit is not None: lat_e_1D = data_dict['Latitude_e'][:,0] lon_e_1D = data_dict['Longitude_e'][0,:] # index i1 = get_center_index(lat_e_1D, region_limit[0]) i2 = get_center_index(lat_e_1D, region_limit[2]) j1 = get_center_index(lon_e_1D, region_limit[1]) j2 = get_center_index(lon_e_1D, region_limit[3]) for varn in varnames: if varn in coord_varns: data_dict[varn] = data_dict[varn][i1:i2+2,j1:j2+2] if varn in some_varns: data_dict[varn] = data_dict[varn][i1:i2+1,j1:j2+1] # plot fig = plt.figure(figsize=(8,7)) plt.subplots_adjust(top=0.98) ax_list = [] xtick = [100, 110, 120] ytick = [30, 40, 50] xtick_list = [xtick, xtick, xtick] ytick_list = [ytick, [], ytick] for i in range(len(xtick_list)): ax = add_geoaxes(fig, int('22'+str(i+1)), xtick=xtick_list[i], ytick=ytick_list[i]) ax_list.append(ax) lat_e = data_dict['Latitude_e'] lon_e = data_dict['Longitude_e'] # colorbar height h = 0.02 # variables for i in range(len(some_varns)): varn = some_varns[i] var = data_dict[varn] ax = ax_list[i] var_out = cartopy_plot(lon_e, lat_e, var, ax=ax, vmin=vmin, vmax=vmax, cbar=seperate_cbar, cmap=deepcopy(WhGrYlRd_map)) add_China_province(ax) # colorbar for variables ax1 = ax_list[0] ax2 = ax_list[1] cax_v = h_2_ax(fig, ax1, ax2, y_off=-0.06, height=h) plt.colorbar(var_out['mesh'], cax=cax_v, orientation='horizontal') right_center_label(cax_v, '[DU]') # difference ax = ax_list[2] var_diff = data_dict[varname2] - data_dict[varname1] diff_out = cartopy_plot(lon_e, lat_e, var_diff, ax=ax, vmin=diff_min, vmax=diff_max, cbar=seperate_cbar, cmap=plt.get_cmap('seismic')) add_China_province(ax) # colorbar of difference ax = ax_list[2] cax_diff = h_1_ax(fig, ax, y_off=-0.06, height=h) plt.colorbar(diff_out['mesh'], cax=cax_diff, orientation='horizontal') right_center_label(cax_diff, '[DU]') # scatter plot ax_sc = fig.add_subplot(224) ax_sc.set_aspect('equal') ax_sc.set_xlabel(sc_xlabel) ax_sc.set_ylabel(sc_ylabel) ax_sc.set_xlim([0,1.0]) ax_sc.set_ylim([0,1.0]) label_ul = ['R', 'linear_eq', 'rmse', 'nmb', 'mb', 'N'] scatter(ax_sc, data_dict[varname1], data_dict[varname2], s=3, label_ul=label_ul) # region if region_limit is not None: for ax in ax_list: ax.set_xlim(region_limit[1], region_limit[3]) ax.set_ylim(region_limit[0], region_limit[2])
def plot_NO2_combine(dir_mean, dir_month, start_year, end_year, year, month, verbose=False, region_limit=[-90.0, -180.0, 90.0, 180.0], dir_fig=None, vmin_mean=0.0, vmax_mean=0.5, vmin_ano=-0.1, vmax_ano=0.1, vmin_ano_per=-50.0, vmax_ano_per=50.0): """ Plot mutli-year mean of monthly NO2, monthly NO2 anomaly and monthly NO2 relative anomaly. Parameters ---------- dir_mean : str Directory of mutli-year mean of monthly NO2 dir_month : str Directory of monthly NO2 start_year : str Used to generate filename and longname end_year : str Used to generate filename and longname year : str Used to generate filename and longname month : str Used to generate filename and longname verbose : logical output more information region_limit : list The region for plot. [min_lat, min_lon, max_lat, max_lon] dir_fig : str or None If it is str, save figure to the directory """ # used to convert unit toDU = 2.69e16 # colorbar parameters pad = 0.01 aspect = 15 # read monthly data file_month = dir_month + 'OMI_NO2_' + year + '-' + month \ + '_monthly.nc' NO2_month = io.read_month_OMI_NO2_L3(file_month, verbose=verbose) NO2_month = NO2_month / toDU # read mutli-year mean of monthly NO2 NO2_mean = io.read_multi_year_month_OMI_NO2_L3(dir_mean, start_year, end_year, month, verbose=verbose) NO2_mean = NO2_mean / toDU # NO2 anoanmly NO2_ano = NO2_month - NO2_mean # NO2 realtive anoamly NO2_ano_percent = NO2_ano / NO2_mean * 100.0 # generate grid nlat_c, nlon_c = 720, 1440 lat_e, lon_e, lat_c, lon_c = generate_grid(nlat_c, nlon_c) # get region data i1 = get_center_index(lat_e, region_limit[0]) i2 = get_center_index(lat_e, region_limit[2]) j1 = get_center_index(lon_e, region_limit[1]) j2 = get_center_index(lon_e, region_limit[3]) NO2_mean = NO2_mean[i1:i2 + 1, j1:j2 + 1] NO2_ano = NO2_ano[i1:i2 + 1, j1:j2 + 1] NO2_ano_percent = NO2_ano_percent[i1:i2 + 1, j1:j2 + 1] lat_e = lat_e[i1:i2 + 2] lon_e = lon_e[j1:j2 + 2] # begin plot #fig = plt.figure(figsize=(4, 8)) fig = plt.figure() xtick = np.arange(-180, 180.1, 20) ytick = np.arange(-90, 90.1, 10) ax_list = [] # mutli-year mean of monthly NO2 title1 = start_year + '-' + end_year + ', ' + month \ + r', NO$_2$' ax1 = add_geoaxes(fig, 311, xtick=[], ytick=ytick, title=title1) ax_list.append(ax1) pout1 = pcolormesh(ax1, lon_e, lat_e, NO2_mean, vmin=vmin_mean, vmax=vmax_mean) cbar1 = fig.colorbar(pout1['mesh'], ax=ax1, extend='max', pad=pad, aspect=aspect) cbar1.set_label(r'[DU]') # anomaly title2 = year + ', ' + month + ', anomaly' ax2 = add_geoaxes(fig, 312, xtick=[], ytick=ytick, title=title2) ax_list.append(ax2) pout2 = pcolormesh(ax2, lon_e, lat_e, NO2_ano, cmap=plt.get_cmap('seismic'), vmin=vmin_ano, vmax=vmax_ano) cbar2 = fig.colorbar(pout2['mesh'], ax=ax2, extend='both', pad=pad, aspect=aspect) cbar2.set_label(r'[DU]') # relative anomaly title3 = year + ', ' + month + ', relative anomaly' ax3 = add_geoaxes(fig, 313, xtick=xtick, ytick=ytick, title=title3) ax_list.append(ax3) pout3 = pcolormesh(ax3, lon_e, lat_e, NO2_ano_percent, cmap=plt.get_cmap('seismic'), vmin=vmin_ano_per, vmax=vmax_ano_per) cbar3 = fig.colorbar(pout3['mesh'], ax=ax3, extend='both', pad=pad, aspect=aspect) cbar3.set_label(r'[%]') # set axes states_provinces = cfeature.NaturalEarthFeature( category='cultural', name='admin_1_states_provinces_lines', scale='50m', facecolor='none') for ax in ax_list: ax.add_feature(cfeature.BORDERS) ax.add_feature(states_provinces, edgecolor='k', linewidth=0.5) ax.add_feature(cfeature.COASTLINE, zorder=200) ax.add_feature(cfeature.OCEAN, color='w', zorder=100) ax.set_xlim((region_limit[1], region_limit[3])) ax.set_ylim((region_limit[0], region_limit[2])) plt.subplots_adjust(hspace=0.3) # save figure if dir_fig is not None: figname = dir_fig + 'NO2_anomaly_' + year + '-' + month \ + '_from_' + start_year + '-' + end_year + '.png' plt.savefig(figname, format='png', dpi=300) return fig