Skip to content

Commit 4e673f2

Browse files
authored
Merge pull request #300 from fooof-tools/psdgrid
[MNT] - Make grid an updateable argument in PSD plots
2 parents a022997 + 3874dc8 commit 4e673f2

File tree

11 files changed

+96
-30
lines changed

11 files changed

+96
-30
lines changed

doc/api.rst

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -326,6 +326,27 @@ Annotated plots that describe the model and fitting process.
326326
plot_annotated_model
327327
plot_annotated_peak_search
328328

329+
Plot Utilities & Styling
330+
~~~~~~~~~~~~~~~~~~~~~~~~
331+
332+
Plot related utilies for styling and managing plots.
333+
334+
.. currentmodule:: fooof.plts.style
335+
336+
.. autosummary::
337+
:toctree: generated/
338+
339+
check_style_options
340+
341+
.. currentmodule:: fooof.plts.utils
342+
343+
.. autosummary::
344+
:toctree: generated/
345+
346+
check_ax
347+
recursive_plot
348+
save_figure
349+
329350
Utilities
330351
---------
331352

fooof/objs/fit.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -407,7 +407,7 @@ def report(self, freqs=None, power_spectrum=None, freq_range=None,
407407
Only relevant / effective if `freqs` and `power_spectrum` passed in in this call.
408408
**plot_kwargs
409409
Keyword arguments to pass into the plot method.
410-
Plot options with a name conflict be passed by pre-pending 'plot_'.
410+
Plot options with a name conflict be passed by pre-pending `plot_`.
411411
e.g. `freqs`, `power_spectrum` and `freq_range`.
412412
413413
Notes

fooof/plts/aperiodic.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ def plot_aperiodic_params(aps, colors=None, labels=None, ax=None, **plot_kwargs)
3333
ax : matplotlib.Axes, optional
3434
Figure axes upon which to plot.
3535
**plot_kwargs
36-
Keyword arguments to pass into the ``style_plot``.
36+
Additional plot related keyword arguments, with styling options managed by ``style_plot``.
3737
"""
3838

3939
ax = check_ax(ax, plot_kwargs.pop('figsize', PLT_FIGSIZES['params']))
@@ -83,7 +83,7 @@ def plot_aperiodic_fits(aps, freq_range, control_offset=False,
8383
ax : matplotlib.Axes, optional
8484
Figure axes upon which to plot.
8585
**plot_kwargs
86-
Keyword arguments to pass into the ``style_plot``.
86+
Additional plot related keyword arguments, with styling options managed by ``style_plot``.
8787
"""
8888

8989
ax = check_ax(ax, plot_kwargs.pop('figsize', PLT_FIGSIZES['params']))

fooof/plts/error.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ def plot_spectral_error(freqs, error, shade=None, log_freqs=False, ax=None, **pl
3333
ax : matplotlib.Axes, optional
3434
Figure axes upon which to plot.
3535
**plot_kwargs
36-
Keyword arguments to pass into the ``style_plot``.
36+
Additional plot related keyword arguments, with styling options managed by ``style_plot``.
3737
"""
3838

3939
ax = check_ax(ax, plot_kwargs.pop('figsize', PLT_FIGSIZES['spectral']))

fooof/plts/fg.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@ def plot_fg(fg, save_fig=False, file_name=None, file_path=None, **plot_kwargs):
3333
Name to give the saved out file.
3434
file_path : Path or str, optional
3535
Path to directory to save to. If None, saves to current directory.
36+
**plot_kwargs
37+
Additional plot related keyword arguments, with styling options managed by ``style_plot``.
3638
3739
Raises
3840
------
@@ -76,7 +78,7 @@ def plot_fg_ap(fg, ax=None, **plot_kwargs):
7678
ax : matplotlib.Axes, optional
7779
Figure axes upon which to plot.
7880
**plot_kwargs
79-
Keyword arguments to pass into the ``style_plot``.
81+
Additional plot related keyword arguments, with styling options managed by ``style_plot``.
8082
"""
8183

8284
if fg.aperiodic_mode == 'knee':
@@ -101,7 +103,7 @@ def plot_fg_gf(fg, ax=None, **plot_kwargs):
101103
ax : matplotlib.Axes, optional
102104
Figure axes upon which to plot.
103105
**plot_kwargs
104-
Keyword arguments to pass into the ``style_plot``.
106+
Additional plot related keyword arguments, with styling options managed by ``style_plot``.
105107
"""
106108

107109
plot_scatter_2(fg.get_params('error'), 'Error',
@@ -121,7 +123,7 @@ def plot_fg_peak_cens(fg, ax=None, **plot_kwargs):
121123
ax : matplotlib.Axes, optional
122124
Figure axes upon which to plot.
123125
**plot_kwargs
124-
Keyword arguments to pass into the ``style_plot``.
126+
Additional plot related keyword arguments, with styling options managed by ``style_plot``.
125127
"""
126128

127129
plot_hist(fg.get_params('peak_params', 0)[:, 0], 'Center Frequency',

fooof/plts/fm.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ def plot_fm(fm, plot_peaks=None, plot_aperiodic=True, freqs=None, power_spectrum
6363
data_kwargs, model_kwargs, aperiodic_kwargs, peak_kwargs : None or dict, optional
6464
Keyword arguments to pass into the plot call for each plot element.
6565
**plot_kwargs
66-
Keyword arguments to pass into the ``style_plot``.
66+
Additional plot related keyword arguments, with styling options managed by ``style_plot``.
6767
6868
Notes
6969
-----
@@ -169,7 +169,7 @@ def _add_peaks_shade(fm, plt_log, ax, **plot_kwargs):
169169
ax : matplotlib.Axes
170170
Figure axes upon which to plot.
171171
**plot_kwargs
172-
Keyword arguments to pass into the ``fill_between``.
172+
Keyword arguments to pass into ``fill_between``.
173173
"""
174174

175175
defaults = {'color' : PLT_COLORS['periodic'], 'alpha' : 0.25}

fooof/plts/periodic.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ def plot_peak_params(peaks, freq_range=None, colors=None, labels=None, ax=None,
3535
ax : matplotlib.Axes, optional
3636
Figure axes upon which to plot.
3737
**plot_kwargs
38-
Keyword arguments to pass into the ``style_plot``.
38+
Additional plot related keyword arguments, with styling options managed by ``style_plot``.
3939
"""
4040

4141
ax = check_ax(ax, plot_kwargs.pop('figsize', PLT_FIGSIZES['params']))
@@ -86,7 +86,7 @@ def plot_peak_fits(peaks, freq_range=None, colors=None, labels=None, ax=None, **
8686
ax : matplotlib.Axes, optional
8787
Figure axes upon which to plot.
8888
**plot_kwargs
89-
Keyword arguments to pass into the plot call.
89+
Additional plot related keyword arguments, with styling options managed by ``style_plot``.
9090
"""
9191

9292
ax = check_ax(ax, plot_kwargs.pop('figsize', PLT_FIGSIZES['params']))

fooof/plts/settings.py

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,8 @@
2828
'linestyle' : ['ls', 'linestyle']}
2929

3030
# Plot style arguments are those that can be defined on an axis object
31-
AXIS_STYLE_ARGS = ['title', 'xlabel', 'ylabel', 'xlim', 'ylim']
31+
AXIS_STYLE_ARGS = ['title', 'xlabel', 'ylabel', 'xlim', 'ylim',
32+
'xticks', 'yticks', 'xticklabels', 'yticklabels']
3233

3334
# Line style arguments are those that can be defined on a line object
3435
LINE_STYLE_ARGS = ['alpha', 'lw', 'linewidth', 'ls', 'linestyle',
@@ -40,8 +41,13 @@
4041
# Custom style arguments are those that are custom-handled by the plot style function
4142
CUSTOM_STYLE_ARGS = ['title_fontsize', 'label_size', 'tick_labelsize',
4243
'legend_size', 'legend_loc']
43-
STYLERS = ['axis_styler', 'line_styler', 'custom_styler']
44-
STYLE_ARGS = AXIS_STYLE_ARGS + LINE_STYLE_ARGS + CUSTOM_STYLE_ARGS + STYLERS
44+
45+
# Define list of available style functions - these can also be replaced by arguments
46+
STYLERS = ['axis_styler', 'line_styler', 'collection_styler', 'custom_styler']
47+
48+
# Collect the full set of possible style related input keyword arguments
49+
STYLE_ARGS = \
50+
AXIS_STYLE_ARGS + LINE_STYLE_ARGS + COLLECTION_STYLE_ARGS + CUSTOM_STYLE_ARGS + STYLERS
4551

4652
## Define default values for plot aesthetics
4753
# These are all custom style arguments

fooof/plts/spectra.py

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -47,13 +47,14 @@ def plot_spectra(freqs, power_spectra, log_freqs=False, log_powers=False, freq_r
4747
ax : matplotlib.Axes, optional
4848
Figure axes upon which to plot.
4949
**plot_kwargs
50-
Additional plot related keyword arguments.
50+
Additional plot related keyword arguments, with styling options managed by ``style_plot``.
51+
For spectra plots, boolean input `grid` can be used to control if the figure has a grid.
5152
"""
5253

54+
# Create the plot & collect plot kwargs of interest
5355
ax = check_ax(ax, plot_kwargs.pop('figsize', PLT_FIGSIZES['spectral']))
54-
55-
# Create the plot
5656
plot_kwargs = check_plot_kwargs(plot_kwargs, {'linewidth' : 2.0})
57+
grid = plot_kwargs.pop('grid', True)
5758

5859
# Check for frequency range input, and log if x-axis is in log space
5960
if freq_range is not None:
@@ -82,7 +83,7 @@ def plot_spectra(freqs, power_spectra, log_freqs=False, log_powers=False, freq_r
8283

8384
ax.set_xlim(freq_range)
8485

85-
style_spectrum_plot(ax, log_freqs, log_powers)
86+
style_spectrum_plot(ax, log_freqs, log_powers, grid)
8687

8788

8889
# Alias `plot_spectrum` to `plot_spectra` for backwards compatibility
@@ -110,8 +111,9 @@ def plot_spectra_shading(freqs, power_spectra, shades, shade_colors='r',
110111
ax : matplotlib.Axes, optional
111112
Figure axes upon which to plot.
112113
**plot_kwargs
113-
Additional plot related keyword arguments.
114-
This can include additional inputs into :func:`~.plot_spectra`.
114+
Additional plot related keyword arguments, with styling options managed by ``style_plot``.
115+
For spectra plots, boolean input `grid` can be used to control if the figure has a grid.
116+
This can also include additional inputs into :func:`~.plot_spectra`.
115117
116118
Notes
117119
-----
@@ -127,7 +129,8 @@ def plot_spectra_shading(freqs, power_spectra, shades, shade_colors='r',
127129
add_shades(ax, shades, shade_colors, add_center, plot_kwargs.get('log_freqs', False))
128130

129131
style_spectrum_plot(ax, plot_kwargs.get('log_freqs', False),
130-
plot_kwargs.get('log_powers', False))
132+
plot_kwargs.get('log_powers', False),
133+
plot_kwargs.get('grid', True))
131134

132135

133136
# Alias `plot_spectrum_shading` to `plot_spectra_shading` for backwards compatibility
@@ -165,13 +168,16 @@ def plot_spectra_yshade(freqs, power_spectra, shade='std', average='mean', scale
165168
ax : matplotlib.Axes, optional
166169
Figure axes upon which to plot.
167170
**plot_kwargs
168-
Additional plot related keyword arguments.
171+
Additional plot related keyword arguments, with styling options managed by ``style_plot``.
172+
For spectra plots, boolean input `grid` can be used to control if the figure has a grid.
173+
This can also include additional inputs into :func:`~.plot_spectra`.
169174
"""
170175

171176
if (isinstance(shade, str) or isfunction(shade)) and power_spectra.ndim != 2:
172177
raise ValueError('Power spectra must be 2d if shade is not given.')
173178

174179
ax = check_ax(ax, plot_kwargs.pop('figsize', PLT_FIGSIZES['spectral']))
180+
grid = plot_kwargs.pop('grid', True)
175181

176182
# Set plot data & labels, logging if requested
177183
plt_freqs = np.log10(freqs) if log_freqs else freqs
@@ -208,4 +214,4 @@ def plot_spectra_yshade(freqs, power_spectra, shade='std', average='mean', scale
208214
ax.fill_between(plt_freqs, lower_shade, upper_shade,
209215
alpha=alpha, color=color, **plot_kwargs)
210216

211-
style_spectrum_plot(ax, log_freqs, log_powers)
217+
style_spectrum_plot(ax, log_freqs, log_powers, grid)

fooof/plts/style.py

Lines changed: 34 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,23 @@
66
import matplotlib.pyplot as plt
77

88
from fooof.plts.settings import (AXIS_STYLE_ARGS, LINE_STYLE_ARGS, COLLECTION_STYLE_ARGS,
9-
STYLE_ARGS, LABEL_SIZE, LEGEND_SIZE, LEGEND_LOC,
10-
TICK_LABELSIZE, TITLE_FONTSIZE)
9+
CUSTOM_STYLE_ARGS, STYLE_ARGS, TICK_LABELSIZE, TITLE_FONTSIZE,
10+
LABEL_SIZE, LEGEND_SIZE, LEGEND_LOC)
1111

1212
###################################################################################################
1313
###################################################################################################
1414

15-
def style_spectrum_plot(ax, log_freqs, log_powers):
15+
def check_style_options():
16+
"""Check the list of valid style arguments that can be passed into plot functions."""
17+
18+
print('Valid style arguments:')
19+
for label, options in zip(['Axis', 'Line', 'Collection', 'Custom'],
20+
[AXIS_STYLE_ARGS, LINE_STYLE_ARGS,
21+
COLLECTION_STYLE_ARGS, CUSTOM_STYLE_ARGS]):
22+
print(' {:10s} {}'.format(label, ', '.join(options)))
23+
24+
25+
def style_spectrum_plot(ax, log_freqs, log_powers, grid=True):
1626
"""Apply style and aesthetics to a power spectrum plot.
1727
1828
Parameters
@@ -23,6 +33,8 @@ def style_spectrum_plot(ax, log_freqs, log_powers):
2333
Whether the frequency axis is plotted in log space.
2434
log_powers : bool
2535
Whether the power axis is plotted in log space.
36+
grid : bool, optional, default: True
37+
Whether to add grid lines to the plot.
2638
"""
2739

2840
# Get labels, based on log status
@@ -33,7 +45,7 @@ def style_spectrum_plot(ax, log_freqs, log_powers):
3345
ax.set_xlabel(xlabel, fontsize=20)
3446
ax.set_ylabel(ylabel, fontsize=20)
3547
ax.tick_params(axis='both', which='major', labelsize=16)
36-
ax.grid(True)
48+
ax.grid(grid)
3749

3850
# If labels were provided, add a legend
3951
if ax.get_legend_handles_labels()[0]:
@@ -227,9 +239,24 @@ def style_plot(func, *args, **kwargs):
227239
By default, this function applies styling with the `apply_style` function. Custom
228240
functions for applying style can be passed in using `apply_style` as a keyword argument.
229241
230-
The `apply_style` function calls sub-functions for applying style different plot elements,
231-
and these sub-functions can be overridden by passing in alternatives for `axis_styler`,
232-
`line_styler`, and `custom_styler`.
242+
The `apply_style` function calls sub-functions for applying different plot elements, including:
243+
244+
- `axis_styler`: apply style options to an axis
245+
- `line_styler`: applies style options to lines objects in a plot
246+
- `collection_styler`: applies style options to collections objects in a plot
247+
- `custom_style`: applies custom style options
248+
249+
Each of these sub-functions can be overridden by passing in alternatives.
250+
251+
To see the full set of style arguments that are supported, run the following code:
252+
253+
>>> from fooof.plts.style import check_style_options
254+
>>> check_style_options()
255+
Valid style arguments:
256+
Axis title, xlabel, ylabel, xlim, ylim, xticks, yticks, xticklabels, yticklabels
257+
Line alpha, lw, linewidth, ls, linestyle, marker, ms, markersize
258+
Collection alpha, edgecolor
259+
Custom title_fontsize, label_size, tick_labelsize, legend_size, legend_loc
233260
"""
234261

235262
@wraps(func)

0 commit comments

Comments
 (0)