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 Python Tutorial | Learn Python Programming Language Python Tutorial â Python is one of the most popular programming languages. Itâs simple to use, packed with features and supported by a wide range of libraries and frameworks. Its clean syntax makes it beginner-friendly.Python is:A high-level language, used in web development, data science, automatio 10 min read Python Interview Questions and Answers Python is the most used language in top companies such as Intel, IBM, NASA, Pixar, Netflix, Facebook, JP Morgan Chase, Spotify and many more because of its simplicity and powerful libraries. To crack their Online Assessment and Interview Rounds as a Python developer, we need to master important Pyth 15+ min read Non-linear Components In electrical circuits, Non-linear Components are electronic devices that need an external power source to operate actively. Non-Linear Components are those that are changed with respect to the voltage and current. Elements that do not follow ohm's law are called Non-linear Components. Non-linear Co 11 min read Python OOPs Concepts Object Oriented Programming is a fundamental concept in Python, empowering developers to build modular, maintainable, and scalable applications. By understanding the core OOP principles (classes, objects, inheritance, encapsulation, polymorphism, and abstraction), programmers can leverage the full p 11 min read Python Projects - Beginner to Advanced Python is one of the most popular programming languages due to its simplicity, versatility, and supportive community. Whether youâre a beginner eager to learn the basics or an experienced programmer looking to challenge your skills, there are countless Python projects to help you grow.Hereâs a list 10 min read Python Exercise with Practice Questions and Solutions Python Exercise for Beginner: Practice makes perfect in everything, and this is especially true when learning Python. If you're a beginner, regularly practicing Python exercises will build your confidence and sharpen your skills. To help you improve, try these Python exercises with solutions to test 9 min read Python Programs Practice with Python program examples is always a good choice to scale up your logical understanding and programming skills and this article will provide you with the best sets of Python code examples.The below Python section contains a wide collection of Python programming examples. These Python co 11 min read Spring Boot Tutorial Spring Boot is a Java framework that makes it easier to create and run Java applications. It simplifies the configuration and setup process, allowing developers to focus more on writing code for their applications. This Spring Boot Tutorial is a comprehensive guide that covers both basic and advance 10 min read Class Diagram | Unified Modeling Language (UML) A UML class diagram is a visual tool that represents the structure of a system by showing its classes, attributes, methods, and the relationships between them. It helps everyone involved in a projectâlike developers and designersâunderstand how the system is organized and how its components interact 12 min read Enumerate() in Python enumerate() function adds a counter to each item in a list or other iterable. It turns the iterable into something we can loop through, where each item comes with its number (starting from 0 by default). We can also turn it into a list of (number, item) pairs using list().Let's look at a simple exam 3 min read Like