24
24
@savefig
25
25
@style_plot
26
26
@check_dependency (plt , 'matplotlib' )
27
- def plot_spectra (freqs , power_spectra , log_freqs = False , log_powers = False ,
27
+ def plot_spectra (freqs , power_spectra , log_freqs = False , log_powers = False , freq_range = None ,
28
28
colors = None , labels = None , ax = None , ** plot_kwargs ):
29
29
"""Plot one or multiple power spectra.
30
30
@@ -38,21 +38,27 @@ def plot_spectra(freqs, power_spectra, log_freqs=False, log_powers=False,
38
38
Whether to plot the frequency axis in log spacing.
39
39
log_powers : bool, optional, default: False
40
40
Whether to plot the power axis in log spacing.
41
+ freq_range : list of [float, float], optional
42
+ Frequency range to plot, defined in linear space.
41
43
colors : list of str, optional, default: None
42
44
Line colors of the spectra.
43
45
labels : list of str, optional, default: None
44
46
Legend labels for the spectra.
45
47
ax : matplotlib.Axes, optional
46
48
Figure axes upon which to plot.
47
49
**plot_kwargs
48
- Keyword arguments to pass into the ``style_plot`` .
50
+ Additional plot related keyword arguments .
49
51
"""
50
52
51
53
ax = check_ax (ax , plot_kwargs .pop ('figsize' , PLT_FIGSIZES ['spectral' ]))
52
54
53
55
# Create the plot
54
56
plot_kwargs = check_plot_kwargs (plot_kwargs , {'linewidth' : 2.0 })
55
57
58
+ # Check for frequency range input, and log if x-axis is in log space
59
+ if freq_range is not None :
60
+ freq_range = np .log10 (freq_range ) if log_freqs else freq_range
61
+
56
62
# Make inputs iterable if need to be passed multiple times to plot each spectrum
57
63
plt_powers = np .reshape (power_spectra , (1 , - 1 )) if np .ndim (power_spectra ) == 1 else \
58
64
power_spectra
@@ -63,7 +69,7 @@ def plot_spectra(freqs, power_spectra, log_freqs=False, log_powers=False,
63
69
labels = repeat (labels ) if not isinstance (labels , list ) else cycle (labels )
64
70
colors = repeat (colors ) if not isinstance (colors , list ) else cycle (colors )
65
71
66
- # Plot
72
+ # Plot power spectra, looping across all spectra to plot
67
73
for freqs , powers , color , label in zip (plt_freqs , plt_powers , colors , labels ):
68
74
69
75
# Set plot data, logging if requested, and collect color, if absent
@@ -74,6 +80,8 @@ def plot_spectra(freqs, power_spectra, log_freqs=False, log_powers=False,
74
80
75
81
ax .plot (freqs , powers , label = label , ** plot_kwargs )
76
82
83
+ ax .set_xlim (freq_range )
84
+
77
85
style_spectrum_plot (ax , log_freqs , log_powers )
78
86
79
87
@@ -102,7 +110,8 @@ def plot_spectra_shading(freqs, power_spectra, shades, shade_colors='r',
102
110
ax : matplotlib.Axes, optional
103
111
Figure axes upon which to plot.
104
112
**plot_kwargs
105
- Keyword arguments to pass into :func:`~.plot_spectra`.
113
+ Additional plot related keyword arguments.
114
+ This can include additional inputs into :func:`~.plot_spectra`.
106
115
107
116
Notes
108
117
-----
@@ -152,7 +161,7 @@ def plot_spectra_yshade(freqs, power_spectra, shade='std', average='mean', scale
152
161
ax : matplotlib.Axes, optional
153
162
Figure axes upon which to plot.
154
163
**plot_kwargs
155
- Keyword arguments to be passed to `plot_spectra` or to the plot call .
164
+ Additional plot related keyword arguments .
156
165
"""
157
166
158
167
if (isinstance (shade , str ) or isfunction (shade )) and power_spectra .ndim != 2 :
0 commit comments