Skip to content

Commit 92f9a3b

Browse files
authored
Add xlabel and ylabel to plot_wfdb (#670)
Fixes #276. `plot_wfdb` now takes `xlabel` and `ylabel`, lists with a label for each channel, and passes them to `plot_items`, which already supported them. Without them the labels are as before: the signal names and units on the y axes, and `time/second` (or the chosen unit) on the bottom x axis. The new arguments come last, so calls that pass arguments by position are unaffected. ```python wfdb.plot_wfdb(record, xlabel=["", "Time (s)"], ylabel=["Lead II (mV)", "Lead V5 (mV)"]) ``` This also fixes `xlabel` in `plot_items`, which has never worked: it checked the number of labels against `signal.shape[1]` after `signal` had become a list of channels, so it raised `AttributeError: 'list' object has no attribute 'shape'`. It now checks against the number of subplots, as `ylabel` does. ## Testing - New tests for the default labels, custom labels in `plot_wfdb`, and `xlabel` in `plot_items`. The last two fail on `main`. - `pytest -m "not network"`: all pass. Ruff and the docs build pass. <sub>@tompollard 🟥 red 🦥 sloth · aspen</sub>
2 parents 493bd7e + 705e4b5 commit 92f9a3b

3 files changed

Lines changed: 50 additions & 4 deletions

File tree

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
**Set axis labels in plot_wfdb, and fix xlabel in plot_items**
2+
``plot_wfdb`` takes ``xlabel`` and ``ylabel`` arguments, lists with a label
3+
for each channel, to replace the default labels (the time unit, and the
4+
signal names and units). ``plot_items`` failed with an ``AttributeError``
5+
whenever ``xlabel`` was given, and now sets the labels.

‎src/wfdb/plot/plot.py‎

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -290,10 +290,10 @@ def plot_items(
290290
# Add title and axis labels.
291291
# First, make sure that xlabel and ylabel inputs are valid
292292
if xlabel:
293-
if len(xlabel) != signal.shape[1]:
293+
if len(xlabel) != n_subplots:
294294
raise Exception(
295295
"The length of the xlabel must be the same as the "
296-
f"signal: {signal.shape[1]} values"
296+
f"signal: {n_subplots} values"
297297
)
298298

299299
if ylabel:
@@ -846,6 +846,8 @@ def plot_wfdb(
846846
figsize=None,
847847
return_fig=False,
848848
sharex="auto",
849+
xlabel=None,
850+
ylabel=None,
849851
):
850852
"""Subplot individual channels of a WFDB record and/or annotation.
851853
@@ -905,6 +907,12 @@ def plot_wfdb(
905907
to False, then each subplot can be panned/zoomed independently. If
906908
set to 'auto' (default), then the X axis will be shared unless
907909
record is multi-frequency and the time units are set to 'samples'.
910+
xlabel : list, optional
911+
A list of strings with the x label of each channel. By default, only
912+
the bottom channel is labelled, with 'time/' and the time unit.
913+
ylabel : list, optional
914+
A list of strings with the y label of each channel. By default, the
915+
labels are the signal names and units, such as 'MLII/mV'.
908916
909917
Returns
910918
-------
@@ -927,7 +935,7 @@ def plot_wfdb(
927935
ann_samp,
928936
ann_sym,
929937
fs,
930-
ylabel,
938+
default_ylabel,
931939
record_name,
932940
sig_units,
933941
) = _get_wfdb_plot_items(record=record, annotation=annotation, plot_sym=plot_sym)
@@ -968,7 +976,8 @@ def plot_wfdb(
968976
ann_sym=ann_sym,
969977
fs=fs,
970978
time_units=time_units,
971-
ylabel=ylabel,
979+
xlabel=xlabel,
980+
ylabel=(ylabel or default_ylabel),
972981
title=(title or record_name),
973982
sig_style=sig_style,
974983
sig_units=sig_units,

‎tests/test_plot.py‎

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,38 @@ def test_annotations_inside_signal_no_warning():
175175
plt.close(fig)
176176

177177

178+
def test_plot_items_xlabel():
179+
fig = wfdb.plot_items(np.zeros((10, 2)), xlabel=["a", "b"], return_fig=True)
180+
plt.close(fig)
181+
assert [ax.get_xlabel() for ax in fig.axes] == ["a", "b"]
182+
183+
184+
def test_plot_wfdb_default_axis_labels():
185+
record = wfdb.rdrecord("sample-data/100", sampto=360)
186+
fig = wfdb.plot_wfdb(record, return_fig=True)
187+
plt.close(fig)
188+
189+
assert [ax.get_ylabel() for ax in fig.axes] == ["MLII/mV", "V5/mV"]
190+
assert [ax.get_xlabel() for ax in fig.axes] == ["", "time/second"]
191+
192+
193+
def test_plot_wfdb_custom_axis_labels():
194+
"""plot_wfdb passes xlabel and ylabel to plot_items (#276)."""
195+
record = wfdb.rdrecord("sample-data/100", sampto=360)
196+
annotation = wfdb.rdann("sample-data/100", "atr", sampto=360)
197+
fig = wfdb.plot_wfdb(
198+
record,
199+
annotation,
200+
xlabel=["", "Time (s)"],
201+
ylabel=["Lead II (mV)", "Lead V5 (mV)"],
202+
return_fig=True,
203+
)
204+
plt.close(fig)
205+
206+
assert [ax.get_ylabel() for ax in fig.axes] == ["Lead II (mV)", "Lead V5 (mV)"]
207+
assert [ax.get_xlabel() for ax in fig.axes] == ["", "Time (s)"]
208+
209+
178210
class TestPlotInternal:
179211
"""Unit tests for internal wfdb.plot.plot functions"""
180212

0 commit comments

Comments
 (0)