0

I have a folder containing multiple wav files (currently say 4). I want to plot the wav, its mfcc and mel spectrogram in a row , so finally a figure with 12 plots(each row with three figure, and hence four rows). I am not able to plot the graph, only extracted the features. Can some one help with this for loop please. I mean how to use subplot command and how to store each figure in loop.

Regards

path=glob.glob('the path having four wav files/*.wav')

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(4,3,.....)
    librosa.display.waveplot(y, sr=sr)
    librosa.display.specshow(librosa.power_to_db(S, ref=np.max))
    librosa.display.specshow(mfcc, x_axis="time",y_axis="mel")
    plt.show()

2 Answers2

0
import matplotlib.pyplot as plt
Arrays=np.random.rand(6,10,10)
N_figures=Arrays.shape[0]
for Id in range(1,N_figures+1):
    Array=Arrays[Id-1]
    plt.subplot(N_figures/2,N_figures/3,Id)
    plt.imshow(Array)

enter image description here

You just need to change imshow with desired plottings. Since I don't have the spectrogram files I've used randomly created NumPy arrays. Your implementation doesn't work because fig, ax = plt.subplot(4,3,.....) is not appropriate to use. Fig and axs returns in the function plt.subplots not plt.subplot.You can also use that method but outside of the for loop and then access the axs elements.

Hakan Akgün
  • 872
  • 5
  • 13
  • path=glob.glob('E:/....../*.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.subplot(12/3,12/4,i+1) librosa.display.specshow(mfcc, x_axis="time",y_axis="mel") plt.subplot(12/3,12/4,i+2) librosa.display.specshow(librosa.power_to_db(S)) plt.subplot(12/3,12/4,i+3) – KRISHNA CHAUHAN Aug 16 '21 at 17:58
  • I dont know how to make it look like a code here in comments? Also I am not getting how to set this outer loop i, which is I think the number of plots. So I need 4X3Xi matrix of 12 plots. Only ith position needs to be set correctly. Please guide. – KRISHNA CHAUHAN Aug 16 '21 at 18:00
  • Do you know how to plot just 1 spectrogram? – Hakan Akgün Aug 16 '21 at 18:37
  • I did that, just axis is creating problem , I tried axis(off) but not working for me. – KRISHNA CHAUHAN Aug 17 '21 at 04:46
  • done using : [axi.set_axis_off() for axi in ax.ravel()] – KRISHNA CHAUHAN Aug 17 '21 at 17:08
0

The final code is :

import matplotlib.pyplot as plt
import librosa
import librosa.display
import glob

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) :
   
    y, sr = librosa.load(path[i], 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
   

    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
   

Tried putting this axis(off after every plot, but somehow its not working)

enter image description here

NcXNaV
  • 1,657
  • 4
  • 14
  • 23
  • can someone plz tell me how to remove the axis from all these plots? Regards – KRISHNA CHAUHAN Aug 17 '21 at 06:40
  • In my figure, it works. Have you put the plt.axis('off') at the end of the for a loop. – Hakan Akgün Aug 17 '21 at 09:17
  • Yes sir did that only. Could you plz see the code snippet and tell me exactly where I should write this. – KRISHNA CHAUHAN Aug 17 '21 at 13:54
  • Right after librosa.display.specshow(librosa.power_to_db(S), x_axis='time', y_axis='log', ax=ax[i, 2]) # spectrogram in row i, column 2 line at the end of the for loop. – Hakan Akgün Aug 17 '21 at 17:38
  • No sir its not working. I think for subplots() it wont work. I just use one more line, after defining figure specification and its done: [axi.set_axis_off() for axi in ax.ravel()] Also yOu can follow this link. https://stackoverflow.com/questions/25862026/turn-off-axes-in-subplots – KRISHNA CHAUHAN Aug 18 '21 at 04:59