Matplotlib.pyplot.csd() in Python Last Updated : 31 May, 2025 Comments Improve Suggest changes Like Article Like Report csd() method in Matplotlib is used to compute and plot the Cross Spectral Density (CSD) of two signals. It helps analyze the frequency domain relationship between two time series, revealing how the power of one signal is distributed relative to the other signal across frequencies. It's key features include:Plots the magnitude squared coherence between the signals as a function of frequency.Returns the frequency and cross spectral density values for further analysis.Useful in signal processing and time series analysis to identify common frequency components.Example: Python import numpy as np import matplotlib.pyplot as plt t = np.linspace(0, 1, 500, endpoint=False) x = np.sin(2 * np.pi * 50 * t) + np.random.randn(t.size) * 0.5 y = np.sin(2 * np.pi * 50 * t) + np.random.randn(t.size) * 0.5 Pxy, freqs = plt.csd(x, y, NFFT=256, Fs=500) plt.title('Cross Spectral Density') plt.show() OutputUsing Matplotlib.pyplot.csd()Explanation:np.linspace(0, 1, 500, endpoint=False) creates a 500-point time array at 500 Hz sampling.x and y are noisy 50 Hz sine waves simulating real signals.plt.csd(x, y, NFFT=256, Fs=500) computes the Cross Spectral Density with 256-point FFT blocks.Returns Pxy (CSD) and freqs (frequencies) for frequency analysis.Syntax of Matplotlib.pyplot.csd()matplotlib.pyplot.csd(x, y, NFFT=None, Fs=None, detrend=None, window=None, noverlap=None, pad_to=None, scale_by_freq=None, sides='default', hold=None, data=None, **kwargs)Parameters:ParameterDescriptionx, yInput time series data arrays to compute the CSD between. NFFT Number of data points used in each FFT block. Fs float Sampling frequency of the time series.detrend Specifies detrending method applied to each segment ('constant' subtracts the mean).windowWindow function applied to each segment (e.g., 'hann' for Hanning window).noverlapNumber of points to overlap between segments.pad_to Number of points to zero-pad the data segment before FFT.scale_by_freq Whether to scale the resulting spectrum by the frequency resolution. sides Which sides of the spectrum to return. hold Whether to hold the current plot (deprecated in newer Matplotlib versions).data An alternative to passing x and y directly,allows selecting columns from a DataFrame.kwargs Passed to the plot function for customizing the plot (e.g., color, linestyle).Returns:Pxy (ndarray ): The cross spectral density of x and y.frequencies (ndarray): The frequencies corresponding to the CSD values.ExamplesExample 1: We compute the cross spectral density of two noisy cosine signals using a Hanning window and 50% overlap between segments to reduce spectral leakage and smooth the result. Python import numpy as np import matplotlib.pyplot as plt from matplotlib.mlab import window_hanning t = np.linspace(0, 1, 1000, endpoint=False) x = np.cos(2 * np.pi * 20 * t) + np.random.randn(t.size) * 0.3 y = np.cos(2 * np.pi * 20 * t) + np.random.randn(t.size) * 0.3 Pxy, freqs = plt.csd(x, y, NFFT=128, Fs=1000, window=window_hanning, noverlap=64) plt.show() OutputUsing Matplotlib.pyplot.csd()Explanation:np.linspace(0, 1, 1000, endpoint=False) creates a 1000-point time array at 1000 Hz sampling.x and y are noisy 20 Hz cosine waves simulating real signals.plt.csd(x, y, NFFT=128, Fs=1000, window=window_hanning, noverlap=64) computes the Cross Spectral Density using 128-point FFT blocks with a Hanning window and 64-point overlap to reduce spectral leakage.Example 2: We compute the cross spectral density of two noisy sine signals using frequency scaling and a one-sided spectrum to highlight how signal power varies with frequency. Python t = np.linspace(0, 2, 1000, endpoint=False) x = np.sin(2 * np.pi * 10 * t) + np.random.randn(t.size) * 0.2 y = np.sin(2 * np.pi * 10 * t) + np.random.randn(t.size) * 0.2 Pxy, freqs = plt.csd(x, y, NFFT=256, Fs=500, scale_by_freq=True, sides='onesided') plt.title('CSD with Frequency Scaling and One-sided Spectrum') plt.show() OutputUsing Matplotlib.pyplot.csd()Explanation:np.linspace(0, 2, 1000, endpoint=False) creates a 1000-point time array over 2 seconds.x and y are noisy 10 Hz sine waves simulating real signals.plt.csd() computes Cross Spectral Density with 256-point FFT blocks, scales by frequency, and returns a one-sided spectrum for clearer frequency analysis.Example 3: We compute the cross spectral density of two noisy sine signals using detrending to remove DC offset and zero padding to enhance frequency resolution. Python t = np.linspace(0, 1, 512, endpoint=False) x = np.sin(2 * np.pi * 40 * t) + 0.4 * np.random.randn(t.size) y = np.sin(2 * np.pi * 40 * t) + 0.4 * np.random.randn(t.size) Pxy, freqs = plt.csd(x, y, NFFT=256, Fs=512, detrend='constant', pad_to=512) plt.title('CSD with Detrending and Zero Padding') plt.show() OutputUsing Matplotlib.pyplot.csd()Explanation:np.linspace(0, 1, 512, endpoint=False) creates a 512-point time array at 512 Hz sampling.x and y are noisy 40 Hz sine waves simulating real signals.plt.csd() computes Cross Spectral Density with 256-point FFT blocks, detrends by removing the mean, and zero-pads to improve frequency resolution. Comment More infoAdvertise with us Next Article Matplotlib.pyplot.csd() in Python S SHUBHAMSINGH10 Follow Improve Article Tags : Python Python-matplotlib Practice Tags : python Similar Reads Matplotlib.pyplot.gca() in Python Matplotlib is a library in Python and it is a numerical - mathematical extension for the NumPy library. Pyplot is a state-based interface to a Matplotlib module that provides a MATLAB-like interface.  matplotlib.pyplot.gca() Function The gca() function in pyplot module of matplotlib library is used 2 min read Matplotlib.pyplot.hsv() in Python Matplotlib is a library in Python and it is numerical - mathematical extension for NumPy library. Pyplot is a state-based interface to a Matplotlib module which provides a MATLAB-like interface. matplotlib.pyplot.hsv() Function The hsv() function in pyplot module of matplotlib library is used to set 2 min read Matplotlib.pyplot.gcf() in Python Matplotlib is an amazing visualization library in Python for 2D plots of arrays. Matplotlib is a multi-platform data visualization library built on NumPy arrays and designed to work with the broader SciPy stack. matplotlib.pyplot.gcf() matplotlib.pyplot.gcf() is primarily used to get the current fi 2 min read Matplotlib.pyplot.gci() in Python Matplotlib is a library in Python and it is numerical â mathematical extension for NumPy library. Pyplot is a state-based interface to a Matplotlib module which provides a MATLAB-like interface. There are various plots which can be used in Pyplot are Line Plot, Contour, Histogram, Scatter, 3D Plot, 2 min read Matplotlib.pyplot.cla() in Python Matplotlib is a library in Python and it is numerical - mathematical extension for NumPy library. Pyplot is a state-based interface to a Matplotlib module which provides a MATLAB-like interface. There are various plots which can be used in Pyplot are Line Plot, Contour, Histogram, Scatter, 3D Plot, 1 min read Matplotlib.pyplot.clf() in Python Matplotlib is a library in Python and it is numerical - mathematical extension for NumPy library. Pyplot is a state-based interface to a Matplotlib module which provides a MATLAB-like interface. There are various plots which can be used in Pyplot are Line Plot, Contour, Histogram, Scatter, 3D Plot, 1 min read Matplotlib.pyplot.draw() in Python Matplotlib is a library in Python and it is numerical - mathematical extension for NumPy library. Pyplot is a state-based interface to a Matplotlib module which provides a MATLAB-like interface. matplotlib.pyplot.draw() Function The draw() function in pyplot module of matplotlib library is used to r 1 min read Matplotlib.pyplot.axes() in Python axes() method in Matplotlib is used to create a new Axes instance (i.e., a plot area) within a figure. This allows you to specify the location and size of the plot within the figure, providing more control over subplot layout compared to plt.subplot(). It's key features include:Creates a new Axes at 3 min read Matplotlib.pyplot.grid() in Python matplotlib.pyplot.grid() function in Matplotlib is used to toggle the visibility of grid lines in a plot. Grid lines help improve readability of a plot by adding reference lines at major and/or minor tick positions along the axes. Example:Pythonimport matplotlib.pyplot as plt fig, ax = plt.subplots( 3 min read Matplotlib.pyplot.clim() in Python Matplotlib is a library in Python and it is numerical - mathematical extension for NumPy library. Pyplot is a state-based interface to a Matplotlib module which provides a MATLAB-like interface. There are various plots which can be used in Pyplot are Line Plot, Contour, Histogram, Scatter, 3D Plot, 2 min read Like