How to Plot Mfcc in Python Using Matplotlib?
Last Updated :
28 Apr, 2025
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 compute and visualize MFCC using Python and Matplotlib.
Plot Mfcc in Python Using Matplotlib
Below is the step-by-step approach to plot Mfcc in Python using Matplotlib:
Step 1: Installation
Before starting, install the following libraries with the help of the following commands:
pip install numpy librosa matplotlib
Step 2: Getting Started
Let's start by importing the necessary libraries and loading an audio file. For this example, we'll use the librosa library to load an audio file.
Python3
import librosa
import librosa.display
import matplotlib.pyplot as plt
import numpy as np
Step 3: Load an audio file
Now, we will load an audio file by using the following code.
Python3
audio_file = 'myaudio.wav'
y, sr = librosa.load(audio_file)
Step 4: Compute MFCC
Now, let's compute the MFCC of the audio signal using librosa's mfcc function:
Python3
mfccs = librosa.feature.mfcc(y, sr=sr, n_mfcc=13)
Step 5: Visualize MFCC
To visualize the MFCC, we can use Matplotlib to create a heatmap. Each row in the MFCC matrix represents a different coefficient, and each column represents a frame in the audio signal.
Python3
plt.figure(figsize=(10, 5))
librosa.display.specshow(mfccs, x_axis='time')
plt.colorbar()
plt.title('MFCC')
plt.xlabel('Time')
plt.ylabel('MFCC Coefficients')
plt.show()
Step 6: Customization
You can customize the plot further by adding axis labels, title, and adjusting the color map. Additionally, you can choose the number of MFCC coefficients by modifying the n_mfcc parameter when computing the MFCC.
Python3
plt.figure(figsize=(10, 5))
librosa.display.specshow(mfccs, x_axis='time', cmap='viridis', hop_length=512)
plt.colorbar(format='%+2.0f dB')
plt.title('MFCC')
plt.xlabel('Time (s)')
plt.ylabel('MFCC Coefficients')
plt.show()
Complete Code
Python3
import librosa
import librosa.display
import matplotlib.pyplot as plt
def plot_mfcc(audio_path):
# Load the audio file
y, sr = librosa.load(audio_path)
# Extract MFCC features
mfccs = librosa.feature.mfcc(y=y, sr=sr, n_mfcc=13)
# Plot MFCCs
plt.figure(figsize=(10, 4))
librosa.display.specshow(mfccs, x_axis='time', cmap='viridis')
plt.colorbar(format='%+2.0f dB')
plt.title('MFCC')
plt.xlabel('Time')
plt.ylabel('MFCC Coefficient')
plt.show()
# Example usage
audio_file_path = "path/to/your/audio/file.wav"
plot_mfcc(audio_file_path)
Output:
.png)
Similar Reads
Multiplots in Python using Matplotlib Matplotlib is a Python library that can be used for plotting graphs and figures. Plotting multiplots or multiple plots are often required either for comparing the two curves or show some gradual changes in the multiple plots, and this can be done using Subplots. Subplots are one of the most importan
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
Contour Plot using Matplotlib - Python Contour plots also called level plots are a tool for doing multivariate analysis and visualizing 3-D plots in 2-D space. If we consider X and Y as our variables we want to plot then the response Z will be plotted as slices on the X-Y plane due to which contours are sometimes referred as Z-slices or
2 min read
Matplotlib.pyplot.sci() 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.imshow() in Python matplotlib.pyplot.imshow() function in Python is used to display images in a plot. It is part of the matplotlib library and allows you to visualize images as 2D data. This function is widely used for displaying images, matrices, or heatmaps where each value in the array corresponds to a color. Examp
5 min read
How to Install Matplotlib on 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. In this article, we will look into the various process of installing Matplotlib on Windo
2 min read