Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions changelog.d/plot-wfdb-axis-labels.rst
Original file line number Diff line number Diff line change
@@ -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.
17 changes: 13 additions & 4 deletions src/wfdb/plot/plot.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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
-------
Expand All @@ -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)
Expand Down Expand Up @@ -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,
Expand Down
32 changes: 32 additions & 0 deletions tests/test_plot.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"""

Expand Down
Loading