Skip to content

Commit 41df936

Browse files
committed
add signal styling option in plotrec
1 parent d69bce7 commit 41df936

File tree

2 files changed

+30
-7
lines changed

2 files changed

+30
-7
lines changed

README.rst

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -322,7 +322,8 @@ Plotting Data
322322

323323
::
324324

325-
plotrec(record=None, title = None, annotation = None, annch = [0], timeunits='samples', figsize=None, returnfig = False, ecggrids=[]):
325+
plotrec(record=None, title = None, annotation = None, annch = [0], timeunits='samples',
326+
sigstyle='', figsize=None, returnfig = False, ecggrids=[]):
326327

327328
Example Usage:
328329

@@ -341,6 +342,7 @@ Input Arguments:
341342
- ``annotation`` (default=None): An Annotation object. The annsamp attribute locations will be overlaid on the signal.
342343
- ``annch`` (default=[0]): A list of channels on which to plot the annotation samples.
343344
- ``timeunits`` (default='samples'): String specifying the x axis unit. Allowed options are: 'samples', 'seconds', 'minutes', and 'hours'.
345+
- ``sigstyle`` (default=''): String, or list of strings, specifying the styling of the matplotlib plot for the signals. If 'sigstyle' is a string, each channel will have the same style. If it is a list, each channel's style will correspond to the list element. ie. sigtype=['r','b','k'].
344346
- ``figsize`` (default=None): Tuple pair specifying the width, and height of the figure. Same as the 'figsize' argument passed into matplotlib.pyplot's figure() function.
345347
- ``returnfig`` (default=False): Specifies whether the figure is to be returned as an output argument
346348
- ``ecggrids`` (default=[]): List of integers specifying channels in which to plot ecg grids. May be set to [] for no channels, or 'all' for all channels. Major grids at 0.5mV, and minor grids at 0.125mV. All channels to be plotted with grids must have units equal to 'uV', 'mV', or 'V'.

wfdb/plot/plots.py

Lines changed: 27 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,13 @@
77

88
# Plot a WFDB Record's signals
99
# Optionally, overlay annotation locations
10-
def plotrec(record=None, title = None, annotation = None, annch = [0], timeunits='samples', figsize=None, returnfig = False, ecggrids=[]):
10+
def plotrec(record=None, title = None, annotation = None, annch = [0], timeunits='samples', sigstyle='', figsize=None, returnfig = False, ecggrids=[]):
1111
""" Subplot and label each channel of a WFDB Record.
1212
Optionally, subplot annotation locations over selected channels.
1313
1414
Usage:
15-
plotrec(record=None, title = None, annotation = None, annch = [0], timeunits='samples', returnfig=False)
15+
plotrec(record=None, title = None, annotation = None, annch = [0], timeunits='samples', sigstyle='',
16+
figsize=None, returnfig = False, ecggrids=[])
1617
1718
Input arguments:
1819
- record (required): A wfdb Record object. The p_signals attribute will be plotted.
@@ -21,6 +22,9 @@ def plotrec(record=None, title = None, annotation = None, annch = [0], timeunits
2122
- annch (default=[0]): A list of channels on which to plot the annotation samples.
2223
- timeunits (default='samples'): String specifying the x axis unit.
2324
Allowed options are: 'samples', 'seconds', 'minutes', and 'hours'.
25+
- sigstyle (default=''): String, or list of strings, specifying the styling of the matplotlib plot for the signals.
26+
If 'sigstyle' is a string, each channel will have the same style. If it is a list, each channel's style will
27+
correspond to the list element. ie. sigtype=['r','b','k']
2428
- figsize (default=None): Tuple pair specifying the width, and height of the figure. Same as the 'figsize' argument
2529
passed into matplotlib.pyplot's figure() function.
2630
- returnfig (default=False): Specifies whether the figure is to be returned as an output argument
@@ -42,10 +46,17 @@ def plotrec(record=None, title = None, annotation = None, annch = [0], timeunits
4246

4347
# Check the validity of items used to make the plot
4448
# Return the x axis time values to plot for the record (and annotation if any)
45-
t, tann = checkplotitems(record, title, annotation, annch, timeunits)
49+
t, tann = checkplotitems(record, title, annotation, annch, timeunits, sigstyle)
4650

4751
siglen, nsig = record.p_signals.shape
4852

53+
# Expand list styles
54+
if type(sigstyle) == str:
55+
sigtyle = [sigstyle]*record.nsig
56+
else:
57+
if len(sigstyle) < record.nsig:
58+
sigstyle = sigstyle+['']*(record.nsig-len(sigstyle))
59+
4960
# Expand ecg grid channels
5061
if ecggrids == 'all':
5162
ecggrids = range(0, record.nsig)
@@ -56,7 +67,7 @@ def plotrec(record=None, title = None, annotation = None, annch = [0], timeunits
5667
for ch in range(nsig):
5768
# Plot signal channel
5869
ax = fig.add_subplot(nsig, 1, ch+1)
59-
ax.plot(t, record.p_signals[:,ch], zorder=3)
70+
ax.plot(t, record.p_signals[:,ch], sigstyle[ch], zorder=3)
6071

6172
if (title is not None) and (ch==0):
6273
plt.title(title)
@@ -157,7 +168,7 @@ def calc_ecg_grids(minsig, maxsig, units, fs, maxt, timeunits):
157168

158169
# Check the validity of items used to make the plot
159170
# Return the x axis time values to plot for the record (and annotation if any)
160-
def checkplotitems(record, title, annotation, annch, timeunits):
171+
def checkplotitems(record, title, annotation, annch, timeunits, sigstyle):
161172

162173
# signals
163174
if type(record) != records.Record:
@@ -201,11 +212,21 @@ def checkplotitems(record, title, annotation, annch, timeunits):
201212
else:
202213
if type(record.signame) != list or len(record.signame)!= nsig:
203214
raise ValueError("The 'signame' parameter must be a list of strings, with length equal to the number of signal channels")
204-
215+
205216
# title
206217
if title is not None and type(title) != str:
207218
raise TypeError("The 'title' field must be a string")
208219

220+
# signal line style
221+
if type(sigstyle) == str:
222+
pass
223+
elif type(sigstyle) == list:
224+
if len(sigstyle) > record.nsig:
225+
raise ValueError("The 'sigstyle' list cannot have more elements than the number of record channels")
226+
else:
227+
raise TypeError("The 'sigstyle' field must be a string or a list of strings")
228+
229+
209230
# Annotations if any
210231
if annotation is not None:
211232
if type(annotation) != annotations.Annotation:

0 commit comments

Comments
 (0)