Skip to content

Commit 26c89bb

Browse files
committed
Split out spectrum plot styling, including matching axis label to data scale
1 parent 726fdd6 commit 26c89bb

File tree

2 files changed

+64
-34
lines changed

2 files changed

+64
-34
lines changed

fooof/plts/spectra.py

Lines changed: 36 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -7,16 +7,20 @@
77

88
import numpy as np
99

10-
from fooof.plts.utils import check_ax, add_shades
1110
from fooof.core.modutils import safe_import, check_dependency
1211

12+
from fooof.plts.settings import DEFAULT_FIGSIZE
13+
from fooof.plts.utils import check_ax, add_shades
14+
from fooof.plts.style import check_n_style, style_spectrum_plot
15+
1316
plt = safe_import('.pyplot', 'matplotlib')
1417

1518
###################################################################################################
1619
###################################################################################################
1720

1821
@check_dependency(plt, 'matplotlib')
19-
def plot_spectrum(freqs, power_spectrum, log_freqs=False, log_powers=False, ax=None, **kwargs):
22+
def plot_spectrum(freqs, power_spectrum, log_freqs=False, log_powers=False,
23+
ax=None, style_plot=style_spectrum_plot, **kwargs):
2024
"""Plot a power spectrum.
2125
2226
Parameters
@@ -31,34 +35,32 @@ def plot_spectrum(freqs, power_spectrum, log_freqs=False, log_powers=False, ax=N
3135
Whether or not to take the log of the power axis before plotting.
3236
ax : matplotlib.Axes, optional
3337
Figure axes upon which to plot.
38+
style_plot : callable, optional, default: style_spectrum_plot
39+
A function to call to apply styling & aesthetics to the plot.
3440
**kwargs
3541
Keyword arguments to be passed to the plot call.
3642
"""
3743

3844
# Create plot axes, if not provided
3945
if not ax:
40-
_, ax = plt.subplots(figsize=(12, 10))
46+
_, ax = plt.subplots(figsize=DEFAULT_FIGSIZE)
4147

4248
# Set plot data & labels, logging if requested
43-
freqs_label, plt_freqs = _log_helper(log_freqs, 'Frequency', freqs)
44-
powers_label, plt_powers = _log_helper(log_powers, 'Power', power_spectrum)
49+
plt_freqs = np.log10(freqs) if log_freqs else freqs
50+
plt_powers = np.log10(power_spectrum) if log_powers else power_spectrum
4551

46-
# Create the plot
47-
ax.plot(plt_freqs, plt_powers, **kwargs)
48-
49-
# Aesthetics and axis labels
50-
ax.set_xlabel(freqs_label, fontsize=20)
51-
ax.set_ylabel(powers_label, fontsize=20)
52-
ax.tick_params(axis='both', which='major', labelsize=16)
53-
ax.grid(True)
52+
# Set default plot settings, that only apply if not over-written in kwargs
53+
if 'linewidth' not in kwargs:
54+
kwargs['linewidth'] = 2.0
5455

55-
# If labels were provided, add a legend
56-
if ax.get_legend_handles_labels()[0]:
57-
ax.legend(prop={'size': 16})
56+
# Create the plot & style
57+
ax.plot(plt_freqs, plt_powers, **kwargs)
58+
check_n_style(style_plot, ax, log_freqs, log_powers)
5859

5960

6061
@check_dependency(plt, 'matplotlib')
61-
def plot_spectra(freqs, power_spectra, log_freqs=False, log_powers=False, ax=None, **kwargs):
62+
def plot_spectra(freqs, power_spectra, log_freqs=False, log_powers=False,
63+
ax=None, style_plot=style_spectrum_plot, **kwargs):
6264
"""Plot multiple power spectra on the same plot.
6365
6466
Parameters
@@ -73,17 +75,22 @@ def plot_spectra(freqs, power_spectra, log_freqs=False, log_powers=False, ax=Non
7375
Whether or not to take the log of the power axis before plotting.
7476
ax : matplotlib.Axes, optional
7577
Figure axes upon which to plot.
78+
style_plot : callable, optional, default: style_spectrum_plot
79+
A function to call to apply styling & aesthetics to the plot.
7680
**kwargs
7781
Keyword arguments to be passed to the plot call.
7882
"""
7983

8084
ax = check_ax(ax)
8185
for power_spectrum in power_spectra:
82-
plot_spectrum(freqs, power_spectrum, log_freqs, log_powers, ax=ax, **kwargs)
86+
plot_spectrum(freqs, power_spectrum, log_freqs, log_powers,
87+
style_plot=None, ax=ax, **kwargs)
88+
check_n_style(style_plot, ax, log_freqs, log_powers)
8389

8490

8591
@check_dependency(plt, 'matplotlib')
86-
def plot_spectrum_shading(freqs, power_spectrum, shades, add_center=False, ax=None, **kwargs):
92+
def plot_spectrum_shading(freqs, power_spectrum, shades, add_center=False,
93+
ax=None, style_plot=style_spectrum_plot, **kwargs):
8794
"""Plot a power spectrum with a shaded frequency region (or regions).
8895
8996
Parameters
@@ -98,17 +105,21 @@ def plot_spectrum_shading(freqs, power_spectrum, shades, add_center=False, ax=No
98105
Whether to add a line at the center point of the shaded regions.
99106
ax : matplotlib.Axes, optional
100107
Figure axes upon which to plot.
108+
style_plot : callable, optional, default: style_spectrum_plot
109+
A function to call to apply styling & aesthetics to the plot.
101110
**kwargs
102111
Keyword arguments to be passed to the plot call.
103112
"""
104113

105114
ax = check_ax(ax)
106-
plot_spectrum(freqs, power_spectrum, ax=ax, **kwargs)
115+
plot_spectrum(freqs, power_spectrum, style_plot=None, ax=ax, **kwargs)
107116
add_shades(ax, shades, add_center, kwargs.get('log_freqs', False))
117+
check_n_style(style_plot, ax, kwargs.get('log_freqs', False), kwargs.get('log_powers', False))
108118

109119

110120
@check_dependency(plt, 'matplotlib')
111-
def plot_spectra_shading(freqs, power_spectra, shades, add_center=False, ax=None, **kwargs):
121+
def plot_spectra_shading(freqs, power_spectra, shades, add_center=False,
122+
ax=None, style_plot=style_spectrum_plot, **kwargs):
112123
"""Plot a group of power spectra with a shaded frequency region (or regions).
113124
114125
Parameters
@@ -123,22 +134,13 @@ def plot_spectra_shading(freqs, power_spectra, shades, add_center=False, ax=None
123134
Whether to add a line at the center point of the shaded regions.
124135
ax : matplotlib.Axes, optional
125136
Figure axes upon which to plot.
137+
style_plot : callable, optional, default: style_spectrum_plot
138+
A function to call to apply styling & aesthetics to the plot.
126139
**kwargs
127140
Keyword arguments to be passed to the plot call.
128141
"""
129142

130143
ax = check_ax(ax)
131-
plot_spectra(freqs, power_spectra, ax=ax, **kwargs)
144+
plot_spectra(freqs, power_spectra, ax=ax, style_plot=None, **kwargs)
132145
add_shades(ax, shades, add_center, kwargs.get('log_freqs', False))
133-
134-
135-
##
136-
##
137-
138-
def _log_helper(log_data, label, data):
139-
140-
if log_data:
141-
data = np.log10(data)
142-
label = 'log(' + label + ')'
143-
144-
return label, data
146+
check_n_style(style_plot, ax, kwargs.get('log_freqs', False), kwargs.get('log_powers', False))

fooof/plts/style.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
"""Helper functions that apply style and decoration to plots."""
2+
3+
###################################################################################################
4+
###################################################################################################
5+
6+
def check_n_style(style_func, *args):
7+
""""Check is a style function has been passed, and apply if so."""
8+
9+
if style_func:
10+
style_func(*args)
11+
12+
13+
def style_spectrum_plot(ax, log_freqs, log_powers):
14+
"""Define to styling for a power spectrum plot."""
15+
16+
# Get labels, based on log status
17+
xlabel = 'Frequency' if not log_freqs else 'log(Frequency)'
18+
ylabel = 'Power' if not log_powers else 'log(Power)'
19+
20+
# Aesthetics and axis labels
21+
ax.set_xlabel(xlabel, fontsize=20)
22+
ax.set_ylabel(ylabel, fontsize=20)
23+
ax.tick_params(axis='both', which='major', labelsize=16)
24+
ax.grid(True)
25+
26+
# If labels were provided, add a legend
27+
if ax.get_legend_handles_labels()[0]:
28+
ax.legend(prop={'size': 16})

0 commit comments

Comments
 (0)