Skip to content

Commit c650c36

Browse files
author
Benjamin Moody
committed
plot_wfdb: add sharex argument.
This argument allows the caller to specify whether the horizontal axes should be synchronized across all channels (subplots) in a plot. In general, this behavior is desirable as it lets the viewer see the temporal relationship between channels. However, in the case where the record is multi-frequency *and* we are displaying sample numbers on the horizontal axis, matplotlib unfortunately does not appear to have a way to synchronize the axes automatically. Therefore, in the default "auto" case, enable X sharing as long as the time units for all channels are the same.
1 parent 0457e2d commit c650c36

File tree

1 file changed

+23
-0
lines changed

1 file changed

+23
-0
lines changed

wfdb/plot/plot.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -863,6 +863,7 @@ def plot_wfdb(
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)