matplotlib.mlab#

Numerical Python functions written for compatibility with MATLAB commands with the same names. Most numerical Python functions can be found in the NumPy and SciPy libraries. What remains here is code for performing spectral computations and kernel density estimations.

Spectral functions#

cohere

Coherence (normalized cross spectral density)

csd

Cross spectral density using Welch's average periodogram

detrend

Remove the mean or best fit line from an array

psd

Power spectral density using Welch's average periodogram

specgram

Spectrogram (spectrum over segments of time)

complex_spectrum

Return the complex-valued frequency spectrum of a signal

magnitude_spectrum

Return the magnitude of the frequency spectrum of a signal

angle_spectrum

Return the angle (wrapped phase) of the frequency spectrum of a signal

phase_spectrum

Return the phase (unwrapped angle) of the frequency spectrum of a signal

detrend_mean

Remove the mean from a line.

detrend_linear

Remove the best fit line from a line.

detrend_none

Return the original line.

class matplotlib.mlab.GaussianKDE(dataset, bw_method=None)[source]#

Bases: object

Representation of a kernel-density estimate using Gaussian kernels.

Parameters:
datasetarray-like

Datapoints to estimate from. In case of univariate data this is a 1-D array, otherwise a 2D array with shape (# of dims, # of data).

bw_method{'scott', 'silverman'} or float or callable, optional

The method used to calculate the estimator bandwidth. If a float, this will be used directly as kde.factor. If a callable, it should take a GaussianKDE instance as only parameter and return a float. If None (default), 'scott' is used.

Attributes:
datasetndarray

The dataset passed to the constructor.

dimint

Number of dimensions.

num_dpint

Number of datapoints.

factorfloat

The bandwidth factor, obtained from kde.covariance_factor, with which the covariance matrix is multiplied.

covariancendarray

The covariance matrix of dataset, scaled by the calculated bandwidth (kde.factor).

inv_covndarray

The inverse of covariance.

Methods

kde.evaluate(points)

(ndarray) Evaluate the estimated pdf on a provided set of points.

kde(points)

(ndarray) Same as kde.evaluate(points)

covariance_factor()[source]#
evaluate(points)[source]#

Evaluate the estimated pdf on a set of points.

Parameters:
points(# of dimensions, # of points)-array

Alternatively, a (# of dimensions,) vector can be passed in and treated as a single point.

Returns:
(# of points,)-array

The values at each point.

Raises:
ValueErrorif the dimensionality of the input points is different

than the dimensionality of the KDE.

scotts_factor()[source]#
silverman_factor()[source]#
matplotlib.mlab.angle_spectrum(x, Fs=None, window=None, pad_to=None, sides=None)#

Compute the angle of the frequency spectrum (wrapped phase spectrum) of x. Data is padded to a length of pad_to and the windowing function window is applied to the signal.

Parameters:
x1-D array or sequence

Array or sequence containing the data

Fsfloat, default: 2

The sampling frequency (samples per time unit). It is used to calculate the Fourier frequencies, freqs, in cycles per time unit.

windowcallable or ndarray, default: window_hanning

A function or a vector of length NFFT. To create window vectors see window_hanning, window_none, numpy.blackman, numpy.hamming, numpy.bartlett, scipy.signal, scipy.signal.get_window, etc. If a function is passed as the argument, it must take a data segment as an argument and return the windowed version of the segment.

sides{'default', 'onesided', 'twosided'}, optional

Which sides of the spectrum to return. 'default' is one-sided for real data and two-sided for complex data. 'onesided' forces the return of a one-sided spectrum, while 'twosided' forces two-sided.

pad_toint, optional

The number of points to which the data segment is padded when performing the FFT. While not increasing the actual resolution of the spectrum (the minimum distance between resolvable peaks), this can give more points in the plot, allowing for more detail. This corresponds to the n parameter in the call to fft. The default is None, which sets pad_to equal to the length of the input signal (i.e. no padding).

Returns:
spectrum1-D array

The angle of the frequency spectrum (wrapped phase spectrum).

freqs1-D array

The frequencies corresponding to the elements in spectrum.

See also

psd

Returns the power spectral density.

complex_spectrum

Returns the complex-valued frequency spectrum.

magnitude_spectrum

Returns the absolute value of the complex_spectrum.

angle_spectrum

Returns the angle of the complex_spectrum.

phase_spectrum

Returns the phase (unwrapped angle) of the complex_spectrum.

specgram

Can return the complex spectrum of segments within the signal.

matplotlib.mlab.cohere(x, y, NFFT=256, Fs=2, detrend=<function detrend_none>, window=<function window_hanning>, noverlap=0, pad_to=None, sides='default', scale_by_freq=None)[source]#

The coherence between x and y. Coherence is the normalized cross spectral density:

\[C_{xy} = \frac{|P_{xy}|^2}{P_{xx}P_{yy}}\]
Parameters:
x, y

Array or sequence containing the data

Fsfloat, default: 2

The sampling frequency (samples per time unit). It is used to calculate the Fourier frequencies, freqs, in cycles per time unit.

windowcallable or ndarray, default: window_hanning

A function or a vector of length NFFT. To create window vectors see window_hanning, window_none, numpy.blackman, numpy.hamming, numpy.bartlett, scipy.signal,