diff --git a/changelog.d/plot-wfdb-axis-labels.rst b/changelog.d/plot-wfdb-axis-labels.rst new file mode 100644 index 00000000..3821cf70 --- /dev/null +++ b/changelog.d/plot-wfdb-axis-labels.rst @@ -0,0 +1,5 @@ +**Set axis labels in plot_wfdb, and fix xlabel in plot_items** + ``plot_wfdb`` takes ``xlabel`` and ``ylabel`` arguments, lists with a label + for each channel, to replace the default labels (the time unit, and the + signal names and units). ``plot_items`` failed with an ``AttributeError`` + whenever ``xlabel`` was given, and now sets the labels. diff --git a/src/wfdb/plot/plot.py b/src/wfdb/plot/plot.py index 8bb9c481..8aff8b52 100644 --- a/src/wfdb/plot/plot.py +++ b/src/wfdb/plot/plot.py @@ -290,10 +290,10 @@ def plot_items( # Add title and axis labels. # First, make sure that xlabel and ylabel inputs are valid if xlabel: - if len(xlabel) != signal.shape[1]: + if len(xlabel) != n_subplots: raise Exception( "The length of the xlabel must be the same as the " - f"signal: {signal.shape[1]} values" + f"signal: {n_subplots} values" ) if ylabel: @@ -846,6 +846,8 @@ def plot_wfdb( figsize=None, return_fig=False, sharex="auto", + xlabel=None, + ylabel=None, ): """Subplot individual channels of a WFDB record and/or annotation. @@ -905,6 +907,12 @@ def plot_wfdb( to False, then each subplot can be panned/zoomed independently. If set to 'auto' (default), then the X axis will be shared unless record is multi-frequency and the time units are set to 'samples'. + xlabel : list, optional + A list of strings with the x label of each channel. By default, only + the bottom channel is labelled, with 'time/' and the time unit. + ylabel : list, optional + A list of strings with the y label of each channel. By default, the + labels are the signal names and units, such as 'MLII/mV'. Returns ------- @@ -927,7 +935,7 @@ def plot_wfdb( ann_samp, ann_sym, fs, - ylabel, + default_ylabel, record_name, sig_units, ) = _get_wfdb_plot_items(record=record, annotation=annotation, plot_sym=plot_sym) @@ -968,7 +976,8 @@ def plot_wfdb( ann_sym=ann_sym, fs=fs, time_units=time_units, - ylabel=ylabel, + xlabel=xlabel, + ylabel=(ylabel or default_ylabel), title=(title or record_name), sig_style=sig_style, sig_units=sig_units, diff --git a/tests/test_plot.py b/tests/test_plot.py index b772eed9..22beb96a 100644 --- a/tests/test_plot.py +++ b/tests/test_plot.py @@ -175,6 +175,38 @@ def test_annotations_inside_signal_no_warning(): plt.close(fig) +def test_plot_items_xlabel(): + fig = wfdb.plot_items(np.zeros((10, 2)), xlabel=["a", "b"], return_fig=True) + plt.close(fig) + assert [ax.get_xlabel() for ax in fig.axes] == ["a", "b"] + + +def test_plot_wfdb_default_axis_labels(): + record = wfdb.rdrecord("sample-data/100", sampto=360) + fig = wfdb.plot_wfdb(record, return_fig=True) + plt.close(fig) + + assert [ax.get_ylabel() for ax in fig.axes] == ["MLII/mV", "V5/mV"] + assert [ax.get_xlabel() for ax in fig.axes] == ["", "time/second"] + + +def test_plot_wfdb_custom_axis_labels(): + """plot_wfdb passes xlabel and ylabel to plot_items (#276).""" + record = wfdb.rdrecord("sample-data/100", sampto=360) + annotation = wfdb.rdann("sample-data/100", "atr", sampto=360) + fig = wfdb.plot_wfdb( + record, + annotation, + xlabel=["", "Time (s)"], + ylabel=["Lead II (mV)", "Lead V5 (mV)"], + return_fig=True, + ) + plt.close(fig) + + assert [ax.get_ylabel() for ax in fig.axes] == ["Lead II (mV)", "Lead V5 (mV)"] + assert [ax.get_xlabel() for ax in fig.axes] == ["", "Time (s)"] + + class TestPlotInternal: """Unit tests for internal wfdb.plot.plot functions"""