How to use subplot commands to plot multiple features using librosa?

1,386 views
Skip to first unread message

krishna chauhan

unread,
Aug 16, 2021, 2:15:43 PM8/16/21
to librosa
Hi there I have a folder saved as 'path' where 4 wav files are stored, So I am trying to plot in figure matrix of 4 rows and 3 columns for every wav files three corresponding plots as waveform, mfcc and spectrogram.
using the code below, somehow not able to set these i parameters plz guide.
Regards

path=glob.glob('E:/Python_On_All_Dataset/Four emotion_for plot/*.wav') 
for i in range(0,13):
    for p in path:
        y, sr = librosa.load(p, sr=16000)
        mfcc=librosa.feature.mfcc(y) 
        S = librosa.feature.melspectrogram(y, sr)
       #fig, ax = plt.subplot()
         
        librosa.display.waveplot(y, sr=sr)  
        #plt.show()
        plt.subplot(12/3,12/4,i+1)
        
        librosa.display.specshow(mfcc, x_axis="time",y_axis="mel")
        #plt.show()
        plt.subplot(12/3,12/4,i+2)
        
        librosa.display.specshow(librosa.power_to_db(S))
        #librosa.display.specshow((S))
        plt.subplot(12/3,12/4,i+3)
        plt. axis('off')

Brian McFee

unread,
Aug 16, 2021, 2:20:29 PM8/16/21
to librosa
The easiest way to do this is to use matplotlib's object API.  Something like the following:

fig, ax = plt.subplots(nrows=4, ncols=3, sharex=True)

for i in range(4):
   # load wave #i
   librosa.display.waveshow(y, sr, ax=ax[i, 0])  # put wave in row i, column 0
   librosa.display.specshow(mfccs, x_axis='time', ax=ax[i, 1]) # mfcc in row i, column 1
   librosa.display.specshow(librosa.power_to_db(S), x_axis='time', y_axis='log', ax=ax[i, 2])  # spectrogram in row i, column 2

KRISHNA CHAUHAN

unread,
Aug 16, 2021, 2:31:48 PM8/16/21
to librosa
Thank you sir, Tried this waveshow is not working so I replaced by waveplot, Now I am getting something like this:(In attachment) How to remove this color in wave, like I want only blue color, also how to remove these axis, as its not looking good or if I can reduce axis font,
Regards

Fig.png

KRISHNA CHAUHAN

unread,
Aug 16, 2021, 2:34:53 PM8/16/21
to librosa
And sir this is the revised code, I think its only plotting last wave as all plots are appearing similar,

path=glob.glob('E:/Python_On_All_Dataset/Four emotion_for plot/*.wav') 
fig, ax =  plt.subplots(nrows=4, ncols=3, sharex=True)
for i in range(4):
    for p in path:
        y, sr = librosa.load(p, sr=16000)
        librosa.display.waveplot(y, sr, ax=ax[i, 0])  # put wave in row i, column 0
        plt.axis('off')

        mfcc=librosa.feature.mfcc(y) 
        librosa.display.specshow(mfcc, x_axis='time', ax=ax[i, 1]) # mfcc in row i, column 1
        plt.axis('off')

        S = librosa.feature.melspectrogram(y, sr)
        librosa.display.specshow(librosa.power_to_db(S), x_axis='time', y_axis='log', ax=ax[i, 2])  # spectrogram in row i, column 2
        plt.axis('off')

Reply all
Reply to author
Forward
0 new messages