Skip to content

Commit

Permalink
utils.plot: fix _tkinter.TclError via plt.switch_backend('Agg') (
Browse files Browse the repository at this point in the history
…#947)

Fix the `_tkinter.TckError: couldn't connect to display "localhost:38.0"` error by setting the matplotlib backend to Agg, in the following two functions, when saving figures without displaying:

+ `plot_num_triplet_with_nonzero_integer_ambiguity()`, used in the `smallbaselineApp.py --dostep quick_overview` step

+ `plot_timeseries_rms()`, used in the `smallbaselineApp.py --dostep timeseries_RMS` step

Co-authored-by: Zhang Yunjun <yunjunzgeo@gmail.com>
  • Loading branch information
falkamelung and yunjunz authored Jan 20, 2023
1 parent ce1aed0 commit ed3ce70
Showing 1 changed file with 10 additions and 2 deletions.
12 changes: 10 additions & 2 deletions src/mintpy/utils/plot.py
Original file line number Diff line number Diff line change
Expand Up @@ -931,14 +931,18 @@ def plot_coherence_matrix(ax, date12List, cohList, date12List_drop=[], p_dict={}
return ax, coh_mat, im


def plot_num_triplet_with_nonzero_integer_ambiguity(fname, display=False, font_size=12, fig_size=[9,3]):
def plot_num_triplet_with_nonzero_integer_ambiguity(fname, disp_fig=False, font_size=12, fig_size=[9,3]):
"""Plot the histogram for the number of triplets with non-zero integer ambiguity.
Fig. 3d-e in Yunjun et al. (2019, CAGEO).
Parameters: fname - str, path to the numTriNonzeroIntAmbiguity.h5 file.
"""

# matplotlib backend setting
if not disp_fig:
plt.switch_backend('Agg')

# read data
data, atr = readfile.read(fname)
vmax = int(np.nanmax(data))
Expand Down Expand Up @@ -983,7 +987,7 @@ def plot_num_triplet_with_nonzero_integer_ambiguity(fname, display=False, font_s
print('plot and save figure to file', out_fig)
fig.savefig(out_fig, bbox_inches='tight', transparent=True, dpi=300)

if display:
if disp_fig:
plt.show()
else:
plt.close(fig)
Expand All @@ -1003,6 +1007,10 @@ def plot_timeseries_rms(rms_file, cutoff=3, out_fig=None, disp_fig=True,
fig_size - list of 2 float, figure size in inch
"""

# matplotlib backend setting
if not disp_fig:
plt.switch_backend('Agg')

# read date / RMS info from text file
fc = np.loadtxt(rms_file, dtype=bytes).astype(str)
rms_list = fc[:, 1].astype(np.float32) * 1000.
Expand Down

0 comments on commit ed3ce70

Please sign in to comment.