Skip to content

Commit 4a1ca34

Browse files
committed
fix merge
2 parents c0019a2 + cdf2919 commit 4a1ca34

File tree

13 files changed

+132
-39
lines changed

13 files changed

+132
-39
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

specparam/objs/fit.py

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

specparam/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']))

specparam/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']))

specparam/plts/group.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ def plot_group(group, **plot_kwargs):
2828
group : SpectralGroupModel
2929
Object containing results from fitting a group of power spectra.
3030
**plot_kwargs
31-
Keyword arguments to apply to the plot.
31+
Additional plot related keyword arguments, with styling options managed by ``style_plot``.
3232
3333
Raises
3434
------
@@ -72,7 +72,7 @@ def plot_group_aperiodic(group, ax=None, **plot_kwargs):
7272
ax : matplotlib.Axes, optional
7373
Figure axes upon which to plot.
7474
**plot_kwargs
75-
Keyword arguments to pass into the ``style_plot``.
75+
Additional plot related keyword arguments, with styling options managed by ``style_plot``.
7676
"""
7777

7878
if group.aperiodic_mode == 'knee':
@@ -97,7 +97,7 @@ def plot_group_goodness(group, ax=None, **plot_kwargs):
9797
ax : matplotlib.Axes, optional
9898
Figure axes upon which to plot.
9999
**plot_kwargs
100-
Keyword arguments to pass into the ``style_plot``.
100+
Additional plot related keyword arguments, with styling options managed by ``style_plot``.
101101
"""
102102

103103
plot_scatter_2(group.get_params('error'), 'Error',
@@ -117,7 +117,7 @@ def plot_group_peak_frequencies(group, ax=None, **plot_kwargs):
117117
ax : matplotlib.Axes, optional
118118
Figure axes upon which to plot.
119119
**plot_kwargs
120-
Keyword arguments to pass into the ``style_plot``.
120+
Additional plot related keyword arguments, with styling options managed by ``style_plot``.
121121
"""
122122

123123
plot_hist(group.get_params('peak_params', 0)[:, 0], 'Center Frequency',

specparam/plts/model.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ def plot_model(model, plot_peaks=None, plot_aperiodic=True, freqs=None, power_sp
5656
data_kwargs, model_kwargs, aperiodic_kwargs, peak_kwargs : None or dict, optional
5757
Keyword arguments to pass into the plot call for each plot element.
5858
**plot_kwargs
59-
Keyword arguments to apply to the plot.
59+
Additional plot related keyword arguments, with styling options managed by ``style_plot``.
6060
6161
Notes
6262
-----
@@ -163,7 +163,7 @@ def _add_peaks_shade(model, plt_log, ax, **plot_kwargs):
163163
ax : matplotlib.Axes
164164
Figure axes upon which to plot.
165165
**plot_kwargs
166-
Keyword arguments to pass into the ``fill_between``.
166+
Keyword arguments to pass into ``fill_between``.
167167
"""
168168

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

specparam/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']))

specparam/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

specparam/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:
@@ -83,7 +84,7 @@ def plot_spectra(freqs, power_spectra, log_freqs=False, log_powers=False, freq_r
8384

8485
ax.set_xlim(freq_range)
8586

86-
style_spectrum_plot(ax, log_freqs, log_powers)
87+
style_spectrum_plot(ax, log_freqs, log_powers, grid)
8788

8889

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

130132
style_spectrum_plot(ax, plot_kwargs.get('log_freqs', False),
131-
plot_kwargs.get('log_powers', False))
133+
plot_kwargs.get('log_powers', False),
134+
plot_kwargs.get('grid', True))
132135

133136

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

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

175180
ax = check_ax(ax, plot_kwargs.pop('figsize', PLT_FIGSIZES['spectral']))
181+
grid = plot_kwargs.pop('grid', True)
176182

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

212-
style_spectrum_plot(ax, log_freqs, log_powers)
218+
style_spectrum_plot(ax, log_freqs, log_powers, grid)

specparam/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 specparam.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)