Skip to content

Commit 4dd89db

Browse files
authored
Merge pull request #365 from MIT-LCP/close-broken-figure
Close partially-completed plot windows after an error
2 parents 3b408f2 + aae54d7 commit 4dd89db

File tree

1 file changed

+62
-52
lines changed

1 file changed

+62
-52
lines changed

wfdb/plot/plot.py

Lines changed: 62 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -257,63 +257,73 @@ def plot_items(
257257

258258
# Create figure
259259
fig, axes = create_figure(n_subplots, sharex, sharey, figsize)
260+
try:
261+
if signal is not None:
262+
plot_signal(
263+
signal,
264+
sig_len,
265+
n_sig,
266+
fs,
267+
time_units,
268+
sig_style,
269+
axes,
270+
sampling_freq=sampling_freq,
271+
)
260272

261-
if signal is not None:
262-
plot_signal(
263-
signal,
264-
sig_len,
265-
n_sig,
266-
fs,
267-
time_units,
268-
sig_style,
269-
axes,
270-
sampling_freq=sampling_freq,
271-
)
272-
273-
if ann_samp is not None:
274-
plot_annotation(
275-
ann_samp,
276-
n_annot,
277-
ann_sym,
278-
signal,
279-
n_sig,
280-
fs,
281-
time_units,
282-
ann_style,
283-
axes,
284-
sampling_freq=sampling_freq,
285-
ann_freq=ann_freq,
286-
)
287-
288-
if ecg_grids:
289-
plot_ecg_grids(
290-
ecg_grids,
291-
fs,
292-
sig_units,
293-
time_units,
294-
axes,
295-
sampling_freq=sampling_freq,
296-
)
297-
298-
# Add title and axis labels.
299-
# First, make sure that xlabel and ylabel inputs are valid
300-
if xlabel:
301-
if len(xlabel) != signal.shape[1]:
302-
raise Exception(
303-
"The length of the xlabel must be the same as the "
304-
"signal: {} values".format(signal.shape[1])
273+
if ann_samp is not None:
274+
plot_annotation(
275+
ann_samp,
276+
n_annot,
277+
ann_sym,
278+
signal,
279+
n_sig,
280+
fs,
281+
time_units,
282+
ann_style,
283+
axes,
284+
sampling_freq=sampling_freq,
285+
ann_freq=ann_freq,
305286
)
306287

307-
if ylabel:
308-
if len(ylabel) != n_subplots:
309-
raise Exception(
310-
"The length of the ylabel must be the same as the "
311-
"signal: {} values".format(n_subplots)
288+
if ecg_grids:
289+
plot_ecg_grids(
290+
ecg_grids,
291+
fs,
292+
sig_units,
293+
time_units,
294+
axes,
295+
sampling_freq=sampling_freq,
312296
)
313297

314-
label_figure(
315-
axes, n_subplots, time_units, sig_name, sig_units, xlabel, ylabel, title
316-
)
298+
# Add title and axis labels.
299+
# First, make sure that xlabel and ylabel inputs are valid
300+
if xlabel:
301+
if len(xlabel) != signal.shape[1]:
302+
raise Exception(
303+
"The length of the xlabel must be the same as the "
304+
"signal: {} values".format(signal.shape[1])
305+
)
306+
307+
if ylabel:
308+
if len(ylabel) != n_subplots:
309+
raise Exception(
310+
"The length of the ylabel must be the same as the "
311+
"signal: {} values".format(n_subplots)
312+
)
313+
314+
label_figure(
315+
axes,
316+
n_subplots,
317+
time_units,
318+
sig_name,
319+
sig_units,
320+
xlabel,
321+
ylabel,
322+
title,
323+
)
324+
except BaseException:
325+
plt.close(fig)
326+
raise
317327

318328
if return_fig:
319329
return fig

0 commit comments

Comments
 (0)