1212from fooof .core .modutils import safe_import , check_dependency
1313from fooof .plts .settings import PLT_FIGSIZES
1414from fooof .plts .style import style_spectrum_plot , style_plot
15- from fooof .plts .utils import check_ax , add_shades , savefig
15+ from fooof .plts .utils import check_ax , add_shades , savefig , check_plot_kwargs
1616
1717plt = safe_import ('.pyplot' , 'matplotlib' )
1818
1919###################################################################################################
2020###################################################################################################
2121
22- @savefig
23- @style_plot
24- @check_dependency (plt , 'matplotlib' )
25- def plot_spectrum (freqs , power_spectrum , log_freqs = False , log_powers = False ,
26- color = None , label = None , ax = None , ** plot_kwargs ):
27- """Plot a power spectrum.
28-
29- Parameters
30- ----------
31- freqs : 1d array
32- Frequency values, to be plotted on the x-axis.
33- power_spectrum : 1d array
34- Power values, to be plotted on the y-axis.
35- log_freqs : bool, optional, default: False
36- Whether to plot the frequency axis in log spacing.
37- log_powers : bool, optional, default: False
38- Whether to plot the power axis in log spacing.
39- label : str, optional, default: None
40- Legend label for the spectrum.
41- color : str, optional, default: None
42- Line color of the spectrum.
43- ax : matplotlib.Axes, optional
44- Figure axis upon which to plot.
45- **plot_kwargs
46- Keyword arguments to pass into the ``style_plot``.
47- """
48-
49- ax = check_ax (ax , plot_kwargs .pop ('figsize' , PLT_FIGSIZES ['spectral' ]))
50-
51- # Set plot data & labels, logging if requested
52- plt_freqs = np .log10 (freqs ) if log_freqs else freqs
53- plt_powers = np .log10 (power_spectrum ) if log_powers else power_spectrum
54-
55- # Create the plot
56- ax .plot (plt_freqs , plt_powers , linewidth = 2.0 , color = color , label = label )
57-
58- style_spectrum_plot (ax , log_freqs , log_powers )
59-
60-
6122@savefig
6223@style_plot
6324@check_dependency (plt , 'matplotlib' )
6425def plot_spectra (freqs , power_spectra , log_freqs = False , log_powers = False ,
6526 colors = None , labels = None , ax = None , ** plot_kwargs ):
66- """Plot multiple power spectra on the same plot .
27+ """Plot one or multiple power spectra.
6728
6829 Parameters
6930 ----------
70- freqs : 2d array or 1d array or list of 1d array
31+ freqs : 1d or 2d array or list of 1d array
7132 Frequency values, to be plotted on the x-axis.
72- power_spectra : 2d array or list of 1d array
33+ power_spectra : 1d or 2d array or list of 1d array
7334 Power values, to be plotted on the y-axis.
7435 log_freqs : bool, optional, default: False
7536 Whether to plot the frequency axis in log spacing.
7637 log_powers : bool, optional, default: False
7738 Whether to plot the power axis in log spacing.
78- labels : list of str, optional, default: None
79- Legend labels for the spectra.
8039 colors : list of str, optional, default: None
8140 Line colors of the spectra.
41+ labels : list of str, optional, default: None
42+ Legend labels for the spectra.
8243 ax : matplotlib.Axes, optional
8344 Figure axes upon which to plot.
8445 **plot_kwargs
@@ -87,64 +48,44 @@ def plot_spectra(freqs, power_spectra, log_freqs=False, log_powers=False,
8748
8849 ax = check_ax (ax , plot_kwargs .pop ('figsize' , PLT_FIGSIZES ['spectral' ]))
8950
51+ # Create the plot
52+ plot_kwargs = check_plot_kwargs (plot_kwargs , {'linewidth' : 2.0 })
53+
9054 # Make inputs iterable if need to be passed multiple times to plot each spectrum
91- freqs = repeat (freqs ) if isinstance (freqs , np .ndarray ) and freqs .ndim == 1 else freqs
55+ plt_powers = np .reshape (power_spectra , (1 , - 1 )) if np .ndim (power_spectra ) == 1 else \
56+ power_spectra
57+ plt_freqs = repeat (freqs ) if isinstance (freqs , np .ndarray ) and freqs .ndim == 1 else freqs
9258
93- colors = repeat (colors ) if not isinstance (colors , list ) else cycle (colors )
59+ # Set labels
60+ labels = plot_kwargs .pop ('label' ) if 'label' in plot_kwargs .keys () and labels is None else labels
9461 labels = repeat (labels ) if not isinstance (labels , list ) else cycle (labels )
62+ colors = repeat (colors ) if not isinstance (colors , list ) else cycle (colors )
9563
96- for freq , power_spectrum , color , label in zip (freqs , power_spectra , colors , labels ):
97- plot_spectrum (freq , power_spectrum , log_freqs , log_powers ,
98- color = color , label = label , ax = ax )
99-
100- style_spectrum_plot (ax , log_freqs , log_powers )
101-
102-
103- @savefig
104- @check_dependency (plt , 'matplotlib' )
105- def plot_spectrum_shading (freqs , power_spectrum , shades , shade_colors = 'r' ,
106- add_center = False , ax = None , ** plot_kwargs ):
107- """Plot a power spectrum with a shaded frequency region (or regions).
108-
109- Parameters
110- ----------
111- freqs : 1d array
112- Frequency values, to be plotted on the x-axis.
113- power_spectrum : 1d array
114- Power values, to be plotted on the y-axis.
115- shades : list of [float, float] or list of list of [float, float]
116- Shaded region(s) to add to plot, defined as [lower_bound, upper_bound].
117- shade_colors : str or list of string
118- Color(s) to plot shades.
119- add_center : bool, optional, default: False
120- Whether to add a line at the center point of the shaded regions.
121- ax : matplotlib.Axes, optional
122- Figure axes upon which to plot.
123- **plot_kwargs
124- Keyword arguments to pass into :func:`~.plot_spectrum`.
125- """
126-
127- ax = check_ax (ax , plot_kwargs .pop ('figsize' , PLT_FIGSIZES ['spectral' ]))
64+ # Plot
65+ for freqs , powers , color , label in zip (plt_freqs , plt_powers , colors , labels ):
12866
129- plot_spectrum (freqs , power_spectrum , ax = ax , ** plot_kwargs )
67+ # Set plot data, logging if requested, and collect color, if absent
68+ freqs = np .log10 (freqs ) if log_freqs else freqs
69+ powers = np .log10 (powers ) if log_powers else powers
70+ if color :
71+ plot_kwargs ['color' ] = color
13072
131- add_shades ( ax , shades , shade_colors , add_center , plot_kwargs . get ( 'log_freqs' , False ) )
73+ ax . plot ( freqs , powers , label = label , ** plot_kwargs )
13274
133- style_spectrum_plot (ax , plot_kwargs .get ('log_freqs' , False ),
134- plot_kwargs .get ('log_powers' , False ))
75+ style_spectrum_plot (ax , log_freqs , log_powers )
13576
13677
13778@savefig
13879@check_dependency (plt , 'matplotlib' )
13980def plot_spectra_shading (freqs , power_spectra , shades , shade_colors = 'r' ,
14081 add_center = False , ax = None , ** plot_kwargs ):
141- """Plot a group of power spectra with a shaded frequency region (or regions).
82+ """Plot one or multiple power spectra with a shaded frequency region (or regions).
14283
14384 Parameters
14485 ----------
145- freqs : 2d array or 1d array or list of 1d array
86+ freqs : 1d or 2d array or list of 1d array
14687 Frequency values, to be plotted on the x-axis.
147- power_spectra : 2d array or list of 1d array
88+ power_spectra : 1d or 2d array or list of 1d array
14889 Power values, to be plotted on the y-axis.
14990 shades : list of [float, float] or list of list of [float, float]
15091 Shaded region(s) to add to plot, defined as [lower_bound, upper_bound].
0 commit comments