Plotting a Spectrogram using Python and Matplotlib Last Updated : 19 Jan, 2022 Comments Improve Suggest changes Like Article Like Report Prerequisites: Matplotlib A spectrogram can be defined as the visual representation of frequencies against time which shows the signal strength at a particular time. In simple words, a spectrogram is nothing but a picture of sound. It is also called voiceprint or voice grams. A spectrogram is shown using many colors which indicates the signal strengths. If the color is bright then it means that the energy of the signal is high. In other words, brightness of the color is directly proportional to the strength of the signal in spectrogram. The spectrograms are actually created using Short-time Fourier Transform(STFT). It helps us to do a time-varying analysis of the signal provided. Anyway, it is not required to get into the depth of this topic. The main concept is that we divide the audio signal into small pieces and then that audio signal is plotted on the graph against time. For this visualization specgram() function is used with the required parameters. Syntax: matplotlib.pyplot.specgram(Data, NFFT=None, Fs=None, Fc=None, detrend=None, window=None, noverlap=None, cmap=None, xextent=None, pad_to=None, sides=None, scale_by_freq=None, mode=None, scale=None, vmin=None, vmax=None, *, data=None, **kwargs) Parameter: Data- This is the sequence of actual data that needs to be plotted.Fs- This is a scaler with a default value of 2.window- This parameter converts the data segment and returns the windowed version of the segment.sides- This specifies the side of the spectrum which should be displayed. It can have three values, namely, "default", "onesided" and "twosided".NFFT- This parameter contains the number of data points used in each block for the FFT.detrend- This parameter contains the function applied to each segment before fitting.scale_by_freq- This parameter is allows for integration over the returned frequency values.mode- This parameter is that what sort of spectrum to use {‘default’, ‘psd’, ‘magnitude’, ‘angle’, ‘phase’}.noverlap- This parameter is the number of points of overlap between blocks.scale- This contains the scaling of the values in the spec and can have three values as ‘default’, ‘linear’ and ‘dB’.Fc : This parameter is the center frequency of x.camp: This parameter is a matplotlib.colors.Colormap instance which allows us to change the colors of the spectrogram.These were the basics of the spectrogram. Now, let's move on to plotting a spectrograph using matplotlib library in python. ApproachImport moduleSet the time difference to take picture of the generated signalGenerate an array of valuesUse the function with correct parametersAdd additional customization to the plotDisplay plotExample: Python3 # Importing libraries using import keyword. import math import numpy as np import matplotlib.pyplot as plt # Set the time difference to take picture of # the the generated signal. Time_difference = 0.0001 # Generating an array of values Time_Array = np.linspace(0, 5, math.ceil(5 / Time_difference)) # Actual data array which needs to be plot Data = 20*(np.sin(3 * np.pi * Time_Array)) # Matplotlib.pyplot.specgram() function to # generate spectrogram plt.specgram(Data, Fs=6, cmap="rainbow") # Set the title of the plot, xlabel and ylabel # and display using show() function plt.title('Spectrogram Using matplotlib.pyplot.specgram() Method') plt.xlabel("DATA") plt.ylabel("TIME") plt.show() Output: Comment More infoAdvertise with us Next Article Plotting a Spectrogram using Python and Matplotlib Z zawaresumedha Follow Improve Article Tags : Python Python-matplotlib Practice Tags : python Similar Reads Plotting cross-spectral density in Python using Matplotlib Matplotlib is a comprehensive library consisting of modules that are used for Data Visualization just like MATLAB. Pyplot is a further module which makes functions and methods executable. Plotting Cross-Spectral Density The cross-spectral density compares two signals, each from different source tak 2 min read Plot the magnitude spectrum in Python using Matplotlib A Signal is an electromagnetic field or an electric current to transmit data. There are various components of a signal such as frequency, amplitude, wavelength, phase, angular frequency and period from which it is described. A periodic signal can be represented using the below sine function: y = A s 3 min read Matplotlib.pyplot.specgram() 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.specgram() Function The specgram() function in pyplot module of matplotlib library is u 3 min read Simple Plot in Python using Matplotlib Creating simple plots is a common step in data visualization. These visual representations help us to understand trends, patterns and relationships within data. Matplotlib is one of the most popular plotting libraries in Python which makes it easy to generate high-quality graphs with just a few line 4 min read Plot the phase spectrum in Python using Matplotlib A Signal is an electromagnetic field or an electric current to transmit data. There are various components of a signal such as frequency, amplitude, wavelength, phase, angular frequency and period from which it is described. A periodic signal can be represented using the below sine function: y = A s 3 min read Plotting Histogram in Python using Matplotlib Histograms are a fundamental tool in data visualization, providing a graphical representation of the distribution of data. They are particularly useful for exploring continuous data, such as numerical measurements or sensor readings. This article will guide you through the process of Plot Histogram 6 min read Plotting Sine and Cosine Graph using Matplotlib in Python Data visualization and Plotting is an essential skill that allows us to spot trends in data and outliers. With the help of plots, we can easily discover and present useful information about the data. In this article, we are going to plot a sine and cosine graph using Matplotlib in Python. Matplotlib 3 min read matplotlib.pyplot.angle_spectrum() 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.acorr() Function: The angle_spectrum() function in pyplot module of matplotlib library 2 min read How to Plot Mfcc in Python Using Matplotlib? Mel-frequency cepstral coefficients (MFCC) are widely used in audio signal processing and speech recognition tasks. They represent the spectral characteristics of an audio signal and are commonly used as features for various machine-learning applications. In this article, we will explore how to comp 2 min read Plot the power spectral density using Matplotlib - Python matplotlib.pyplot.psd() function is used to plot power spectral density. In the Welch's average periodogram method for evaluating power spectral density (say, Pxx), the vector 'x' is divided equally into NFFT segments. Every segment is windowed by the function window and detrended by the function de 6 min read Like