<jupyter_start><jupyter_text>Plot Power Spectra==================Visualizing power spectra.<jupyter_code># Import matplotlib, which will be used to manage some plots
import matplotlib.pyplot as plt

# Import plotting functions
from fooof.plts.spectra import plot_spectrum, plot_spectra
from fooof.plts.spectra import plot_spectrum_shading, plot_spectra_shading

# Import simulation utilities for creating test data
from fooof.sim.gen import gen_power_spectrum, gen_group_power_spectra
from fooof.sim.params import param_iter, Stepper<jupyter_output><empty_output><jupyter_text>Plotting Power Spectra----------------------The module also includes a plotting sub-module that includes several plottingoptions for visualizing power spectra.These plot functions overlap with what is accessible directly through the FOOOF objects,as the :meth:`~fooof.FOOOF.plot` method. There are extra functions in the module, andextra functionality available in the plotting module.Note that the plots in the module are all built using matplotlib. They all allow forpassing in extra matplotlib parameters for tuning the plot aesthetics, and can also becustomized using matplotlib code and approaches. In this example, we will simulated power spectra to explore the availableplotting options. First, we'll create two spectra, using an example with differentaperiodic components with the same oscillations, including theta, alpha & beta peaks.<jupyter_code># Settings & parameters for the simulations
freq_range = [3, 40]
ap_1 = [0.75, 1.5]
ap_2 = [0.25, 1]
peaks = [[6, 0.2, 1], [10, 0.3, 1], [25, 0.15, 3]]

# Simulate two example power spectra
freqs, powers1 = gen_power_spectrum(freq_range, ap_1, peaks)
freqs, powers2 = gen_power_spectrum(freq_range, ap_2, peaks)<jupyter_output><empty_output><jupyter_text>Plotting Individual Power Spectra---------------------------------First we will start with the core plotting function that plots an individual power spectrum.The :func:`~.plot_spectrum` function takes in an array of frequency values and a 1d array ofof power values, and plots the corresponding power spectrum.This function, as all the functions in the plotting module, takes in optional inputs`log_freqs` and `log_powers` that control whether the frequency and power axesare plotted in log space.<jupyter_code># Create a spectrum plot with a single power spectrum
plot_spectrum(freqs, powers1, log_powers=True)<jupyter_output><empty_output><jupyter_text>Plotting Multiple Power Spectra-------------------------------The :func:`~.plot_spectra` function takes in one or more frequency arrays and one or morearray of associated power values and plots multiple power spectra.Note that the inputs for either can be either 2d arrays, or lists of 1d arrays. You can alsopass in additional optional inputs including `labels`, to specify labels to use in a plotlegend, and `colors` to specify which colors to plot each spectrum in.<jupyter_code># Plot multiple spectra on the same plot, in log-log space, specifying some labels
labels = ['PSD-1', 'PSD-2']
plot_spectra(freqs, [powers1, powers2], log_freqs=True, log_powers=True, labels=labels)<jupyter_output><empty_output><jupyter_text>Plots With Shaded Regions-------------------------In some cases it may be useful to highlight or shade in particular frequency regions,for example, when examining power in particular frequency regions.The :func:`~.plot_spectrum_shading` function takes in a power spectrum and one or moreshaded regions, and plot the power spectrum with the indicated region shaded.The same can be done for multiple power spectra with :func:`~.plot_spectra_shading`.These functions take in an input designating one or more shade regions, each specifiedas [freq_low, freq_high] of the region to shade. They also take in an optional argumentof `shade_colors` which can be used to control the color(s) of the shade regions.<jupyter_code># Plot a single power spectrum, with a shaded region covering alpha
plot_spectrum_shading(freqs, powers1, [8, 12], log_powers=True)
# Plot multiple power spectra, with shades covering theta & beta ranges
plot_spectra_shading(freqs, [powers1, powers2], [[4, 8], [20, 30]],
                     log_powers=True, shade_colors=['green', 'blue'])<jupyter_output><empty_output><jupyter_text>Put it all together-------------------Finally, we can put all these plotting tools together.To do so, note also that all plot functions also take in an optional `ax` argumentthat can specify an axes to plot on. This can be used to place plots in multi-axesfigures, and/or to add to existing plots.Here we will also take advantage of being able to pass in parameters for the underlyingmatplotlib `plot` call to tune the aesthetics of our plot.As a final example then, we will simulate different alpha center frequencies (in faded colors)as compared to a 'canonical' 10 Hz centered alpha, altogether on a plot with a shadedin alpha region.<jupyter_code># Settings & parameters for the simulations
freq_range = [3, 30]
ap_params = [1, 1]

# Simulate a single 10 Hz centered alpha
freqs_al10, powers_al10 = gen_power_spectrum(freq_range, ap_params,
                                             [10, 0.25, 1], nlv=0)

# Simulate spectra stepping across alpha center frequency
cf_steps = Stepper(8, 12.5, 0.5)
freqs_al, powers_al = gen_group_power_spectra(len(cf_steps), freq_range, ap_params,
                                              param_iter([cf_steps, 0.25, 1]))
# Create the plot, plotting onto the same axes object
fig, ax = plt.subplots(figsize=[12, 8])
plot_spectra_shading(freqs_al, powers_al, [8, 12],
                     log_powers=True, alpha=0.6, ax=ax)
plot_spectrum(freqs_al10, powers_al10, log_powers=True,
              color='black', linewidth=3, label='10 Hz Alpha', ax=ax)
plt.title('Comparing Alphas', {'fontsize' : 20, 'fontweight' : 'bold'});<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.0941016024548244, 'fraction_numerical': 0.017047391749062394, 'mean_word_length': 4.592945662535748, 'pattern_counts': {'":': 0, '<': 25, '<?xml version=': 0, '>': 25, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '6', 'dir': 'jupyter-structured-clean-dedup', 'id': '642810', 'license': 'Apache-2.0', 'n_tokens_mistral': 1707, 'n_tokens_neox': 1575, 'n_words': 683, 'path': 'doc/auto_examples/plots/plot_power_spectra.ipynb', 'repo_name': 'varman-m/eeg_notebooks_doc', 'size': '5866'}