Skip to content

Commit a0d8d86

Browse files
author
Benjamin Moody
committed
Merge pull request #390 into main
In plot_wfdb, add an argument sharex which controls whether X axes are shared between subplots. (This means that multiple channels will always appear time-aligned with each other, even if one channel starts or ends with a block of NaNs. It also means that if you use the pan/zoom buttons to navigate, the channels will stay aligned with each other.) sharex=True is generally desirable behavior; however, if the time units are sample numbers and the record is multi-frequency, then it's not possible (AFAICT) with matplotlib to have the axes synchronized while displaying different units on different subplots. So we disable sharex by default in that case. Finally, plot_wfdb will use time_units="seconds" by default, which I think is useful because it's consistent (across databases) and familiar.
2 parents 6318b12 + 5242043 commit a0d8d86

File tree

1 file changed

+24
-1
lines changed

1 file changed

+24
-1
lines changed

wfdb/plot/plot.py

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -856,13 +856,14 @@ def plot_wfdb(
856856
record=None,
857857
annotation=None,
858858
plot_sym=False,
859-
time_units="samples",
859+
time_units="seconds",
860860
title=None,
861861
sig_style=[""],
862862
ann_style=["r*"],
863863
ecg_grids=[],
864864
figsize=None,
865865
return_fig=False,
866+
sharex="auto",
866867
):
867868
"""
868869
Subplot individual channels of a WFDB record and/or annotation.
@@ -917,6 +918,12 @@ def plot_wfdb(
917918
'figsize' argument passed into matplotlib.pyplot's `figure` function.
918919
return_fig : bool, optional
919920
Whether the figure is to be returned as an output argument.
921+
sharex : bool or 'auto', optional
922+
Whether the X axis should be shared between all subplots. If set
923+
to True, then all signals will be aligned with each other. If set
924+
to False, then each subplot can be panned/zoomed independently. If
925+
set to 'auto' (default), then the X axis will be shared unless
926+
record is multi-frequency and the time units are set to 'samples'.
920927
921928
Returns
922929
-------
@@ -954,6 +961,21 @@ def plot_wfdb(
954961
else:
955962
sampling_freq = None
956963

964+
if sharex == "auto":
965+
# If the sampling frequencies are equal, or if we are using
966+
# hours/minutes/seconds as the time unit, then share the X axes so
967+
# that the channels are synchronized. If time units are 'samples'
968+
# and sampling frequencies are not uniform, then sharing X axes
969+
# doesn't work and may even be misleading.
970+
if (
971+
time_units == "samples"
972+
and isinstance(sampling_freq, list)
973+
and any(f != sampling_freq[0] for f in sampling_freq)
974+
):
975+
sharex = False
976+
else:
977+
sharex = True
978+
957979
if annotation and annotation.fs is not None:
958980
ann_freq = annotation.fs
959981
elif record:
@@ -977,6 +999,7 @@ def plot_wfdb(
977999
return_fig=return_fig,
9781000
sampling_freq=sampling_freq,
9791001
ann_freq=ann_freq,
1002+
sharex=sharex,
9801003
)
9811004

9821005

0 commit comments

Comments
 (0)