Plots
Thin matplotlib wrappers for all DSPkit analysis outputs.
Design
- Every function accepts
ax=None. WhenNone, a new figure is created automatically. - Every function returns the
Axesobject so calls can be chained or the axes customised further. - Extra
**kwargsare forwarded to the underlyingax.plot/ax.pcolormeshcall.
Embedding in multi-panel figures
import matplotlib.pyplot as plt
import matplotlib.gridspec as gridspec
import dspkit as dsp
fig = plt.figure(figsize=(12, 8), constrained_layout=True)
gs = gridspec.GridSpec(2, 2, figure=fig)
dsp.plot_psd(freqs, Pxx, ax=fig.add_subplot(gs[0, 0]))
dsp.plot_coherence(f, Cxy, ax=fig.add_subplot(gs[0, 1]))
dsp.plot_spectrogram(f, t, Zxx, ax=fig.add_subplot(gs[1, :]))
plt.show()
Time-domain
dspkit.plots.plot_signal(t, x, *, ax=None, label=None, envelope=None, title='Signal', xlabel='Time [s]', ylabel='Amplitude', **kwargs)
Plot a time-domain signal.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
t
|
array_like
|
Time vector [s] and signal values. |
required |
x
|
array_like
|
Time vector [s] and signal values. |
required |
ax
|
Axes or None
|
Target axes; a new figure is created when |
None
|
label
|
str or None
|
Legend label for the waveform. |
None
|
envelope
|
array_like or None
|
Instantaneous envelope (e.g. from |
None
|
title
|
str
|
|
'Signal'
|
xlabel
|
str
|
|
'Signal'
|
ylabel
|
str
|
|
'Signal'
|
**kwargs
|
Forwarded to |
{}
|
Returns:
| Name | Type | Description |
|---|---|---|
ax |
Axes
|
|
Source code in dspkit/plots.py
Spectral
dspkit.plots.plot_fft(freqs, amplitude, *, ax=None, db=False, xlim=None, title='FFT Amplitude Spectrum', ylabel=None, **kwargs)
Plot a single-sided amplitude spectrum (output of fft_spectrum).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
freqs
|
array_like
|
Output of |
required |
amplitude
|
array_like
|
Output of |
required |
db
|
bool
|
If |
False
|
xlim
|
(float, float) or None
|
Frequency axis limits. |
None
|
**kwargs
|
Forwarded to |
{}
|
Source code in dspkit/plots.py
dspkit.plots.plot_psd(freqs, Pxx, *, ax=None, db=True, xlim=None, title='Power Spectral Density', ylabel=None, **kwargs)
Plot a power spectral density estimate (output of psd).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
freqs
|
array_like
|
Output of |
required |
Pxx
|
array_like
|
Output of |
required |
db
|
bool
|
If |
True
|
xlim
|
(float, float) or None
|
|
None
|
**kwargs
|
Forwarded to |
{}
|
Source code in dspkit/plots.py
dspkit.plots.plot_csd(freqs, Pxy, *, ax=None, db=True, xlim=None, title='Cross-Spectral Density', ylabel=None, **kwargs)
Plot the magnitude of a cross-spectral density estimate (output of csd).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
freqs
|
array_like
|
Output of |
required |
Pxy
|
array_like
|
Output of |
required |
db
|
bool
|
If |
True
|
xlim
|
(float, float) or None
|
|
None
|
**kwargs
|
Forwarded to |
{}
|
Source code in dspkit/plots.py
dspkit.plots.plot_coherence(freqs, Cxy, *, ax=None, threshold=0.8, xlim=None, title='Magnitude-Squared Coherence', **kwargs)
Plot magnitude-squared coherence (output of coherence).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
freqs
|
array_like
|
|
required |
Cxy
|
array_like
|
|
required |
threshold
|
float or None
|
Horizontal reference line. Default 0.8. |
0.8
|
xlim
|
(float, float) or None
|
|
None
|
Source code in dspkit/plots.py
dspkit.plots.plot_autocorrelation(lags, acf, *, ax=None, n_samples=None, title='Autocorrelation', xlabel='Lag', **kwargs)
Plot the autocorrelation function.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
lags
|
array_like
|
Output of |
required |
acf
|
array_like
|
Output of |
required |
n_samples
|
int or None
|
If provided, overlays ±1.96/sqrt(N) 95 % confidence bands (white-noise null hypothesis). |
None
|
xlabel
|
str
|
Use |
'Lag'
|
Source code in dspkit/plots.py
dspkit.plots.plot_cross_correlation(lags, ccf, *, ax=None, n_samples=None, title='Cross-Correlation', xlabel='Lag', **kwargs)
Plot the cross-correlation function (output of cross_correlation).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
lags
|
array_like
|
Output of |
required |
ccf
|
array_like
|
Output of |
required |
n_samples
|
int or None
|
If provided, overlays ±1.96/sqrt(N) 95 % confidence bands (white-noise / independence null hypothesis). |
None
|
xlabel
|
str
|
Use |
'Lag'
|
Source code in dspkit/plots.py
Time-frequency
dspkit.plots.plot_spectrogram(freqs, times, Zxx, *, ax=None, db=True, floor_db=-60.0, ylim=None, cmap='inferno', title='Spectrogram (STFT)', colorbar_label=None)
Plot an STFT spectrogram.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
freqs
|
array_like
|
Output of |
required |
times
|
array_like
|
Output of |
required |
Zxx
|
array_like
|
Output of |
required |
db
|
bool
|
Display in dB (default |
True
|
floor_db
|
float
|
Colour scale floor relative to peak [dB]. Default -60. |
-60.0
|
ylim
|
(float, float) or None
|
Frequency axis limits. |
None
|
cmap
|
str
|
Matplotlib colormap. Default |
'inferno'
|
Source code in dspkit/plots.py
dspkit.plots.plot_scalogram(freqs, times, W, *, ax=None, db=True, floor_db=-40.0, log_freq=True, ylim=None, cmap='inferno', title='CWT Scalogram', colorbar_label=None)
Plot a CWT scalogram (output of cwt_scalogram).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
freqs
|
array_like
|
Output of |
required |
times
|
array_like
|
Output of |
required |
W
|
array_like
|
Output of |
required |
db
|
bool
|
Display in dB (default |
True
|
floor_db
|
float
|
Colour scale floor relative to peak [dB]. Default -40. |
-40.0
|
log_freq
|
bool
|
Log-scale frequency axis (default |
True
|
ylim
|
(float, float) or None
|
|
None
|
cmap
|
str
|
|
'inferno'
|
Source code in dspkit/plots.py
dspkit.plots.plot_wvd(freqs, times, WVD, *, ax=None, clip_negative=True, db=True, floor_db=-40.0, ylim=None, cmap='inferno', title='Wigner-Ville Distribution', colorbar_label=None)
Plot a Wigner-Ville or Smoothed Pseudo WVD.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
freqs
|
array_like
|
Output of |
required |
times
|
array_like
|
Output of |
required |
WVD
|
array_like
|
Output of |
required |
clip_negative
|
bool
|
Clip negative values to zero before display (default |
True
|
db
|
bool
|
Display in dB (default |
True
|
floor_db
|
float
|
Colour scale floor relative to peak [dB]. Default -40. |
-40.0
|
ylim
|
(float, float) or None
|
|
None
|
cmap
|
str
|
|
'inferno'
|