1111
1212def plot_items (signal = None , ann_samp = None , ann_sym = None , fs = None ,
1313 time_units = 'samples' , sig_name = None , sig_units = None ,
14- ylabel = None , title = None , sig_style = ['' ], ann_style = ['r*' ],
15- ecg_grids = [], figsize = None , return_fig = False ):
14+ xlabel = None , ylabel = None , title = None , sig_style = ['' ],
15+ ann_style = ['r*' ], ecg_grids = [], figsize = None ,
16+ return_fig = False , return_fig_axes = False ):
1617 """
1718 Subplot individual channels of signals and/or annotations.
1819
@@ -55,6 +56,9 @@ def plot_items(signal=None, ann_samp=None, ann_sym=None, fs=None,
5556 A list of strings specifying the units of each signal channel. Used
5657 with `sig_name` to form y labels, if `ylabel` is not set. This
5758 parameter is required for plotting ECG grids.
59+ xlabel : list, optional
60+ A list of strings specifying the final x labels to be used. If this
61+ option is present, no 'time/'`time_units` is used.
5862 ylabel : list, optional
5963 A list of strings specifying the final y labels. If this option is
6064 present, `sig_name` and `sig_units` will not be used for labels.
@@ -82,8 +86,11 @@ def plot_items(signal=None, ann_samp=None, ann_sym=None, fs=None,
8286
8387 Returns
8488 -------
85- figure : matplotlib figure, optional
89+ fig : matplotlib figure, optional
8690 The matplotlib figure generated. Only returned if the 'return_fig'
91+ or 'return_fig_axes' parameter is set to True.
92+ axes : matplotlib axes, optional
93+ The matplotlib axes generated. Only returned if the 'return_fig_axes'
8794 parameter is set to True.
8895
8996 Examples
@@ -114,12 +121,26 @@ def plot_items(signal=None, ann_samp=None, ann_sym=None, fs=None,
114121 plot_ecg_grids (ecg_grids , fs , sig_units , time_units , axes )
115122
116123 # Add title and axis labels.
117- label_figure (axes , n_subplots , time_units , sig_name , sig_units , ylabel ,
118- title )
124+ # First, make sure that xlabel and ylabel inputs are valid
125+ if xlabel :
126+ if len (xlabel ) != signal .shape [1 ]:
127+ raise Exception ('The length of the xlabel must be the same as the '
128+ 'signal: {} values' .format (signal .shape [1 ]))
129+
130+ if ylabel :
131+ if len (ylabel ) != signal .shape [1 ]:
132+ raise Exception ('The length of the ylabel must be the same as the '
133+ 'signal: {} values' .format (signal .shape [1 ]))
134+
135+ label_figure (axes , n_subplots , time_units , sig_name , sig_units ,
136+ xlabel , ylabel , title )
119137
120138 if return_fig :
121139 return fig
122140
141+ if return_fig_axes :
142+ return fig , axes
143+
123144 plt .show ()
124145
125146
@@ -465,8 +486,8 @@ def calc_ecg_grids(minsig, maxsig, sig_units, fs, maxt, time_units):
465486 return (major_ticks_x , minor_ticks_x , major_ticks_y , minor_ticks_y )
466487
467488
468- def label_figure (axes , n_subplots , time_units , sig_name , sig_units , ylabel ,
469- title ):
489+ def label_figure (axes , n_subplots , time_units , sig_name , sig_units ,
490+ xlabel , ylabel , title ):
470491 """
471492 Add title, and axes labels.
472493
@@ -486,6 +507,9 @@ def label_figure(axes, n_subplots, time_units, sig_name, sig_units, ylabel,
486507 A list of strings specifying the units of each signal channel. Used
487508 with `sig_name` to form y labels, if `ylabel` is not set. This
488509 parameter is required for plotting ECG grids.
510+ xlabel : list, optional
511+ A list of strings specifying the final x labels to be used. If this
512+ option is present, no 'time/'`time_units` is used.
489513 ylabel : list, optional
490514 A list of strings specifying the final y labels. If this option is
491515 present, `sig_name` and `sig_units` will not be used for labels.
@@ -500,6 +524,15 @@ def label_figure(axes, n_subplots, time_units, sig_name, sig_units, ylabel,
500524 if title :
501525 axes [0 ].set_title (title )
502526
527+ # Determine x label
528+ # Explicit labels take precedence if present. Otherwise, construct labels
529+ # using signal time units
530+ if not xlabel :
531+ axes [- 1 ].set_xlabel ('/' .join (['time' , time_units [:- 1 ]]))
532+ else :
533+ for ch in range (n_subplots ):
534+ axes [ch ].set_xlabel (xlabel [ch ])
535+
503536 # Determine y label
504537 # Explicit labels take precedence if present. Otherwise, construct labels
505538 # using signal names and units
@@ -523,8 +556,6 @@ def label_figure(axes, n_subplots, time_units, sig_name, sig_units, ylabel,
523556 for ch in range (n_subplots ):
524557 axes [ch ].set_ylabel (ylabel [ch ])
525558
526- axes [- 1 ].set_xlabel ('/' .join (['time' , time_units [:- 1 ]]))
527-
528559
529560def plot_wfdb (record = None , annotation = None , plot_sym = False ,
530561 time_units = 'samples' , title = None , sig_style = ['' ],
0 commit comments