def _adjust_ts_axis(ax, inps): ax.tick_params(which='both', direction='in', labelsize=inps.font_size, bottom=True, top=True, left=True, right=True) ax = pp.auto_adjust_xaxis_date(ax, inps.yearList, fontsize=inps.font_size)[0] ax.set_xlabel('Time [years]', fontsize=inps.font_size) ax.set_ylabel('Displacement [{}]'.format(inps.disp_unit), fontsize=inps.font_size) ax.set_ylim(inps.ylim) return ax
def pixel_ts(lat, lon, font_size, fig_dpi): ''' format : pixel_ts(lat, lon, font_size, fig_dpi) plots the time-series at a pixel ''' dates, dis = ut.read_timeseries_lalo(lat, lon, ts_file=ts_file, lookup_file=geom_file) fig, ax = plt.subplots(nrows=1, ncols=1, figsize=[6, 3]) ax.scatter(dates, dis, marker='^', s=6**2, facecolors='none', edgecolors='k', linewidth=1.) # axis format pp.auto_adjust_xaxis_date(ax, dates, fontsize=font_size) ax.tick_params(which='both', direction='in', labelsize=font_size, bottom=True, top=True, left=True, right=True) ax.set_xlabel('Time [yr]', fontsize=font_size) ax.set_ylabel('LOS displacement [m]', fontsize=font_size) # output out_file = proj_dir+ '/pic/lat' + str(lat) + 'lon' + str(lon) + '.png' plt.savefig(out_file, bbox_inches='tight', dpi=fig_dpi) print('save to file: '+out_file)
def main(iargs=None): inps = cmd_line_parse(iargs) print('\n*************** Spatial Average ******************') mean_list, date_list = ut.spatial_average(inps.file, datasetName=inps.datasetName, maskFile=inps.mask_file, saveList=True) atr = readfile.read_attribute(inps.file) k = atr['FILE_TYPE'] if inps.disp_fig and k == 'timeseries': dates, datevector = ptime.date_list2vector(date_list) # plot fig = plt.figure() ax = fig.add_subplot(111) ax.plot(dates, mean_list, '-o')#, lw=2, ms=16, alpha=0.7) #, mfc='crimson') ax.set_title('Spatial Average', fontsize=12) ax = pp.auto_adjust_xaxis_date(ax, datevector)[0] ax.set_xlabel('Time [years]', fontsize=12) ax.set_ylabel('Mean', fontsize=12) plt.show() return
def plot_point_timeseries(self, yx): """Plot point displacement time-series at pixel [y, x] Parameters: yx : list of 2 int Returns: ts_dis : 1D np.array in size of (num_date,) for the 1st file m_strs : list of strings for the est. time func. param. for the 1st file """ ax = self.ax_pts ax.cla() # plot scatter in different size for different files num_file = len(self.ts_data) if num_file <= 2: ms_step = 4 elif num_file == 3: ms_step = 3 elif num_file == 4: ms_step = 2 elif num_file >= 5: ms_step = 1 # get local Y/X coord for the subsetted and multilooked 3D data cube (y, x) = subset_and_multilook_yx(yx, self.pix_box, self.multilook_num) for i in range(num_file-1, -1, -1): # get displacement data ts_dis = self.ts_data[i][:, y, x] # fit time func m_strs, ts_fit, ts_fit_lim = fit_time_func( model=self.model, date_list=self.date_list, ts_dis=ts_dis, unit_fac=self.unit_fac, G_fit=self.G_fit, seconds=self.seconds) if self.zero_first: off = ts_dis[self.zero_idx] ts_dis -= off ts_fit -= off if self.offset: ts_dis += self.offset * (num_file - 1 - i) ts_fit += self.offset * (num_file - 1 - i) # plot if not np.all(np.isnan(ts_dis)): ppar = argparse.Namespace() ppar.label = self.file_label[i] ppar.ms = self.marker_size - ms_step * (num_file - 1 - i) ppar.mfc = pp.mplColors[num_file - 1 - i] if self.mask[y, x] != 0 else 'gray' self.ts_plot_func(ax, ts_dis, self, ppar) # plot model prediction if self.plot_model: fpar = argparse.Namespace() fpar.linewidth = 3 fpar.color = 'C1' if num_file == 1 else ppar.mfc fpar.fontsize = self.font_size if not self.plot_model_conf_int: ts_fit_lim = None plot_ts_fit(ax, ts_fit, self, fpar, m_strs=m_strs, ts_fit_lim=ts_fit_lim) # axis format ax.tick_params(which='both', direction='in', labelsize=self.font_size, bottom=True, top=True, left=True, right=True) pp.auto_adjust_xaxis_date(ax, self.yearList, fontsize=self.font_size) ax.set_ylabel('Displacement [{}]'.format(self.disp_unit), fontsize=self.font_size) ax.set_ylim(self.ylim) if self.tick_right: ax.yaxis.tick_right() ax.yaxis.set_label_position("right") # title title = get_ts_title(yx[0], yx[1], self.coord) title += ' (masked out)' if self.mask[y, x] == 0 else '' if self.disp_title: ax.set_title(title, fontsize=self.font_size) # legend if len(self.ts_data) > 1: ax.legend() # Print to terminal vprint('\n---------------------------------------') vprint(title) float_formatter = lambda x: [float('{:.2f}'.format(i)) for i in x] vprint(float_formatter(ts_dis)) if not np.all(np.isnan(ts_dis)): # min/max displacement vprint('time-series range: [{:.2f}, {:.2f}] {}'.format(np.nanmin(ts_dis), np.nanmax(ts_dis), self.disp_unit)) # time func param vprint('time function parameters:') for m_str in m_strs: vprint(f' {m_str}') # update figure self.fig_pts.canvas.draw() return ts_dis, m_strs
def update_timeseries(y, x, plot_number, data_only=False): '''Plot point time series displacement at pixel [y, x] Inputs: y : int, y coordinate to update x : int, x coordinate to update plot_number : int, plot number (1/2) to update data_only : bool, compute and return data only, or set remainder of plot variables Outputs: d_ts : [float], timeseries data at x, y point ''' global fig_ts, ax_ts, second_plot_axis, inps, dateList, h5, k, inps, tims, fig_v, date_num, d_ts set_scatter_coords(plot_number, x, y) if plot_number == 1: axis = ax_ts else: axis = second_plot_axis d_ts = [] for i, date in enumerate(dateList): d = h5['timeseries'][i][y, x] if inps.ref_yx: d -= h5['timeseries'][i][inps.ref_yx[0], inps.ref_yx[1]] d_ts.append(d * inps.unit_fac) if inps.zero_first: d_ts -= d_ts[inps.zero_idx] # Returns computed data without setting any plot or figure parameters if data_only: return d_ts axis.cla() if inps.error_file: axis = plot_timeseries_errorbar(ax_ts, d_ts, inps) else: axis, scatter = plot_timeseries_scatter(axis, d_ts, inps, plot_number) scatter.set_label('2') if inps.ylim: axis.set_ylim(inps.ylim) for tick in axis.yaxis.get_major_ticks(): tick.label.set_fontsize(inps.font_size) # Title title_ts = set_axis_title(x, y) if inps.disp_title: axis.set_title(title_ts) axis = pp.auto_adjust_xaxis_date(axis, tims, fontsize=inps.font_size)[0] axis.set_xlabel('Time', fontsize=inps.font_size) axis.set_ylabel('Displacement [%s]' % inps.disp_unit, fontsize=inps.font_size) fig_v.canvas.draw() # Print to terminal print('\n---------------------------------------') print(title_ts) print(d_ts) # Slope estimation estimate_slope() return d_ts
def plot_rms_bar(ax, date_list, rms, cutoff=3., font_size=12, tick_year_num=1, legend_loc='best', disp_legend=True, disp_side_plot=True, disp_thres_text=False, ylabel=r'Residual Phase $\hat \phi_{resid}$ RMS [mm]'): """ Bar plot Phase Residual RMS Parameters: ax : Axes object date_list : list of string in YYYYMMDD format rms : 1D np.array of float for RMS value in mm cutoff : cutoff value of MAD outlier detection tick_year_num : int, number of years per major tick legend_loc : 'upper right' or (0.5, 0.5) Returns: ax : Axes object """ dates, datevector = ptime.date_list2vector(date_list) dates = np.array(dates) try: bar_width = min(ut.most_common(np.diff(dates).tolist(), k=2)) * 3 / 4 except: bar_width = np.min(np.diff(dates).tolist()) * 3 / 4 rms = np.array(rms) # Plot all dates ax.bar(dates, rms, bar_width.days, color=pp.mplColors[0]) # Plot reference date ref_idx = np.argmin(rms) ax.bar(dates[ref_idx], rms[ref_idx], bar_width.days, color=pp.mplColors[1], label='Reference date') # Plot exclude dates rms_threshold = ut.median_abs_deviation_threshold(rms, center=0., cutoff=cutoff) ex_idx = rms > rms_threshold if not np.all(ex_idx == False): ax.bar(dates[ex_idx], rms[ex_idx], bar_width.days, color='darkgray', label='Exclude date') # Plot rms_threshold line (ax, xmin, xmax) = pp.auto_adjust_xaxis_date(ax, datevector, font_size, every_year=tick_year_num) ax.plot(np.array([xmin, xmax]), np.array([rms_threshold, rms_threshold]), '--k', label='Median Abs Dev * {}'.format(cutoff)) # axis format ax = pp.auto_adjust_yaxis(ax, np.append(rms, rms_threshold), font_size, ymin=0.0) ax.set_xlabel('Time [years]', fontsize=font_size) ax.set_ylabel(ylabel, fontsize=font_size) ax.tick_params(which='both', direction='in', labelsize=font_size, bottom=True, top=True, left=True, right=True) # 2nd axes for circles if disp_side_plot: divider = make_axes_locatable(ax) ax2 = divider.append_axes("right", "10%", pad="2%") ax2.plot(np.ones(rms.shape, np.float32) * 0.5, rms, 'o', mfc='none', color=pp.mplColors[0]) ax2.plot(np.ones(rms.shape, np.float32)[ref_idx] * 0.5, rms[ref_idx], 'o', mfc='none', color=pp.mplColors[1]) if not np.all(ex_idx == False): ax2.plot(np.ones(rms.shape, np.float32)[ex_idx] * 0.5, rms[ex_idx], 'o', mfc='none', color='darkgray') ax2.plot(np.array([0, 1]), np.array([rms_threshold, rms_threshold]), '--k') ax2.set_ylim(ax.get_ylim()) ax2.set_xlim([0, 1]) ax2.tick_params(which='both', direction='in', labelsize=font_size, bottom=True, top=True, left=True, right=True) ax2.get_xaxis().set_ticks([]) ax2.get_yaxis().set_ticklabels([]) if disp_legend: ax.legend(loc=legend_loc, frameon=False, fontsize=font_size) # rms_threshold text if disp_thres_text: ymin, ymax = ax.get_ylim() yoff = (ymax - ymin) * 0.1 if (rms_threshold - ymin) > 0.5 * (ymax - ymin): yoff *= -1. ax.annotate('Median Abs Dev * {}'.format(cutoff), xy=(xmin + (xmax - xmin) * 0.05, rms_threshold + yoff), color='k', xycoords='data', fontsize=font_size) return ax
def plot_rms_bar(ax, date_list, rms, cutoff=3., font_size=12, tick_year_num=1, legend_loc='best', disp_legend=True, disp_side_plot=True, disp_thres_text=True, ylabel=r'Residual Phase $\hat \phi_{resid}$ RMS [mm]'): """ Bar plot Phase Residual RMS Parameters: ax : Axes object date_list : list of string in YYYYMMDD format rms : 1D np.array of float for RMS value in mm cutoff : cutoff value of MAD outlier detection tick_year_num : int, number of years per major tick legend_loc : 'upper right' or (0.5, 0.5) Returns: ax : Axes object """ dates, datevector = ptime.date_list2vector(date_list) dates = np.array(dates) try: bar_width = min(ut.most_common(np.diff(dates).tolist(), k=2))*3/4 except: bar_width = np.min(np.diff(dates).tolist())*3/4 rms = np.array(rms) # Plot all dates ax.bar(dates, rms, bar_width.days, color=pp.mplColors[0]) # Plot reference date ref_idx = np.argmin(rms) ax.bar(dates[ref_idx], rms[ref_idx], bar_width.days, color=pp.mplColors[1], label='Reference date') # Plot exclude dates rms_threshold = ut.median_abs_deviation_threshold(rms, center=0., cutoff=cutoff) ex_idx = rms > rms_threshold if not np.all(ex_idx==False): ax.bar(dates[ex_idx], rms[ex_idx], bar_width.days, color='darkgray', label='Exclude date') # Plot rms_threshold line (ax, xmin, xmax) = pp.auto_adjust_xaxis_date(ax, datevector, font_size, every_year=tick_year_num) ax.plot(np.array([xmin, xmax]), np.array([rms_threshold, rms_threshold]), '--k', label='RMS threshold') # axis format ax = pp.auto_adjust_yaxis(ax, np.append(rms, rms_threshold), font_size, ymin=0.0) ax.set_xlabel('Time [years]', fontsize=font_size) ax.set_ylabel(ylabel, fontsize=font_size) ax.tick_params(which='both', direction='in', labelsize=font_size, bottom=True, top=True, left=True, right=True) # 2nd axes for circles if disp_side_plot: divider = make_axes_locatable(ax) ax2 = divider.append_axes("right", "10%", pad="2%") ax2.plot(np.ones(rms.shape, np.float32) * 0.5, rms, 'o', mfc='none', color=pp.mplColors[0]) ax2.plot(np.ones(rms.shape, np.float32)[ref_idx] * 0.5, rms[ref_idx], 'o', mfc='none', color=pp.mplColors[1]) if not np.all(ex_idx==False): ax2.plot(np.ones(rms.shape, np.float32)[ex_idx] * 0.5, rms[ex_idx], 'o', mfc='none', color='darkgray') ax2.plot(np.array([0, 1]), np.array([rms_threshold, rms_threshold]), '--k') ax2.set_ylim(ax.get_ylim()) ax2.set_xlim([0, 1]) ax2.tick_params(which='both', direction='in', labelsize=font_size, bottom=True, top=True, left=True, right=True) ax2.get_xaxis().set_ticks([]) ax2.get_yaxis().set_ticklabels([]) if disp_legend: ax.legend(loc=legend_loc, frameon=False, fontsize=font_size) # rms_threshold text if disp_thres_text: ymin, ymax = ax.get_ylim() yoff = (ymax - ymin) * 0.1 if (rms_threshold - ymin) > 0.5 * (ymax - ymin): yoff *= -1. ax.annotate('Median Abs Dev * {}'.format(cutoff), xy=(xmin + (xmax-xmin)*0.05, rms_threshold + yoff ), color='k', xycoords='data', fontsize=font_size) return ax
n=geo_box[1], e=geo_box[2], s=geo_box[3]) cmd += ' -c bwr_r -v -1.0 1.0 --cbar-loc bottom --cbar-nbins 5 --cbar-ext both --cbar-size 5% ' cmd += ' --dem {} --dem-nocontour '.format(dem_file) cmd += ' --lalo-step 0.2 --lalo-loc 1 0 1 0 --scalebar 0.3 0.80 0.05 --notitle --fontsize 12 ' cmd += ' --noverbose' d_v, atr, inps = view.prep_slice(cmd) ax[0], inps, im, cbar = view.plot_slice(ax[0], d_v, atr, inps) ax[0].plot(lon, lat, "k^", mfc='none', mew=1., ms=6) # point of interest cbar.set_label("LOS Velocity [cm/yr]", fontsize=font_size) ax[1].scatter(dates, d_ts, marker='o', s=4**2) # , color='k') pp.auto_adjust_xaxis_date(ax[1], obj.yearList, fontsize=font_size) ax[1].set_xlabel('Time [years]', fontsize=font_size) ax[1].set_ylabel('LOS Displacement [cm]', fontsize=font_size) ax[1].yaxis.set_minor_locator(ticker.AutoMinorLocator()) ax[1].tick_params(which='both', direction='in', labelsize=font_size, bottom=True, top=True, left=True, right=True) ax[1].set_title(cur_point, fontsize=font_size) fig.subplots_adjust(wspace=-0.4) fig.tight_layout() #plt.savefig('{}_POI.png'.format(proj_name), bbox_inches='tight', transparent=True, dpi=fig_dpi)