Skip to content

Commit

Permalink
tsview: improved auto figsize for geocoded TS file (#1071)
Browse files Browse the repository at this point in the history
+ tsview:
   - timeseriesViewer.plot(): consider the SNWE for geocoded file when plotting in geo coordinate, for a more suited figure size
   - get_model_param_str(): do not show the intercept in the model fitting result, as it's not useful

+ view
   - update_inps_with_file_metadata(): add "_nws" suffix if showing a single subplot without whitespace
   - display pi symbol if min/max is very close 1e-3 via the following changes:
      - utils.plot.auto_adjust_colormap_lut_and_disp_limit(): convert near-pi value to pi if they are very close <1e-3
      - utils.plot.plot_colorbar(): display pi symbol on the colorbar if displaying limits are very close to -pi/pi (<1e-3)
  • Loading branch information
yunjunz authored Aug 15, 2023
1 parent 62b369d commit 4f09c58
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 17 deletions.
9 changes: 7 additions & 2 deletions src/mintpy/tsview.py
Original file line number Diff line number Diff line change
Expand Up @@ -486,7 +486,7 @@ def get_model_param_str(model, ds_dict, disp_unit='cm'):
ds_unit_dict[ds_name] = '/'.join(units)

# list of dataset names
ds_names = [x for x in ds_dict.keys() if not x.endswith('Std')]
ds_names = [x for x in ds_dict.keys() if not x.endswith('Std') and x not in ['intercept']]
w_key = max(len(x) for x in ds_names)
w_val = max(len(f'{x[0]:.2f}') for x in ds_dict.values())

Expand Down Expand Up @@ -692,8 +692,13 @@ def plot(self):

# Figure 1 - Cumulative Displacement Map
if not self.figsize_img:
if self.geo_box and self.fig_coord == 'geo':
w, n, e, s = self.geo_box
ds_shape = (e - w, n - s)
else:
ds_shape = self.ts_data[0].shape[-2:]
self.figsize_img = pp.auto_figure_size(
ds_shape=self.ts_data[0].shape[-2:],
ds_shape=ds_shape,
disp_cbar=True,
disp_slider=True,
print_msg=False)
Expand Down
39 changes: 24 additions & 15 deletions src/mintpy/utils/plot.py
Original file line number Diff line number Diff line change
Expand Up @@ -115,18 +115,18 @@ def auto_figure_size(ds_shape, scale=1.0, disp_cbar=False, disp_slider=False,
"""
# figure shape
fig_shape = list(ds_shape)[::-1]
if disp_cbar:
fig_shape[0] *= (1 + cbar_ratio)
if disp_slider:
fig_shape[1] *= (1 + slider_ratio)
fig_shape[0] *= 1 if not disp_cbar else 1 + cbar_ratio
fig_shape[1] *= 1 if not disp_slider else 1 + slider_ratio

# get scale to meet the min/max figure size constrain
fig_scale = min(min_figsize_single / min(fig_shape),
max_figsize_single / max(fig_shape),
max_figsize_height / fig_shape[1])
fig_scale = min(
min_figsize_single / min(fig_shape),
max_figsize_single / max(fig_shape),
max_figsize_height / fig_shape[1],
)

# fig_shape/scale --> fig_size
fig_size = [x*fig_scale*scale for x in fig_shape]
fig_size = [x * fig_scale * scale for x in fig_shape]
fig_size = [float(f'{x:.1f}') for x in fig_size]
if print_msg:
print(f'figure size : [{fig_size[0]}, {fig_size[1]}]')
Expand Down Expand Up @@ -420,6 +420,10 @@ def auto_adjust_colormap_lut_and_disp_limit(data, num_multilook=1, max_discrete_
vlim = [np.nanmin(data_mli), np.nanmax(data_mli)]
unique_values = None

# convert near-pi value to pi
vlim[0] = vlim[0] if abs(vlim[0] + np.pi) / np.pi >= 0.001 else np.pi * -1
vlim[1] = vlim[1] if abs(vlim[1] - np.pi) / np.pi >= 0.001 else np.pi

return cmap_lut, vlim, unique_values


Expand Down Expand Up @@ -1375,13 +1379,15 @@ def plot_insar_vs_gps_scatter(vel_file, csv_file='gps_enu2los.csv', msk_file=Non


def plot_colorbar(inps, im, cax):

# expand vlim by 0.01% to account for potential numerical precision leak
# e.g. wrapped phase
epsilon = (inps.vlim[1] - inps.vlim[0]) * 0.0001
vmin = inps.vlim[0] - epsilon
vmax = inps.vlim[1] + epsilon

# extend
if not inps.cbar_ext:
# expand vlim by 0.1% to account for potential numerical precision leak
# e.g. wrapped phase
epsilon = (inps.vlim[1] - inps.vlim[0]) * 0.001
vmin = inps.vlim[0] - epsilon
vmax = inps.vlim[1] + epsilon
if vmin <= inps.dlim[0] and vmax >= inps.dlim[1]: inps.cbar_ext='neither'
elif vmin > inps.dlim[0] and vmax >= inps.dlim[1]: inps.cbar_ext='min'
elif vmin <= inps.dlim[0] and vmax < inps.dlim[1]: inps.cbar_ext='max'
Expand All @@ -1395,9 +1401,12 @@ def plot_colorbar(inps, im, cax):

# plot colorbar
unique_values = getattr(inps, 'unique_values', None)
if inps.wrap and (inps.wrap_range[1] - inps.wrap_range[0]) == 2.*np.pi:
if abs(vmin + np.pi) / np.pi < 0.001 and abs(vmax - np.pi) / np.pi < 0.001:
cbar = plt.colorbar(im, cax=cax, orientation=orientation, ticks=[-np.pi, 0, np.pi])
cbar.ax.set_yticklabels([r'-$\pi$', '0', r'$\pi$'])
if orientation == 'vertical':
cbar.ax.set_yticklabels([r'-$\pi$', '0', r'$\pi$'])
else:
cbar.ax.set_xticklabels([r'-$\pi$', '0', r'$\pi$'])

elif unique_values is not None and len(inps.unique_values) <= 5:
# show the exact tick values
Expand Down
2 changes: 2 additions & 0 deletions src/mintpy/view.py
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,8 @@ def update_inps_with_file_metadata(inps, metadata):
if not inps.outfile:
# ignore whitespaces in the filename
fbase = inps.fig_title.replace(' ', '')
if not inps.disp_whitespace:
fbase += '_nws'
inps.outfile = [f'{fbase}{inps.fig_ext}']

inps = update_figure_setting(inps)
Expand Down

0 comments on commit 4f09c58

Please sign in to comment.