0

I want to save mfcc spectrograms plot without displaying in Jupyter notebook output. I've tried to save mfcc spectrograms using following code, despite plots are deflecting in output.

import librosa.display 

x, sr= librosa.load(file_name, sr= sr)

mfcc_feature= librosa.feature.mfcc(x, sr = sr)

fig= plt.figure(figsize=(12,8))

mfcc_image= librosa.display.specshow(mfcc_feature, sr= sr, y_axis= 'linear')

ax.axes.get_xaxis().set_visible(False)

ax.axes.get_yaxis().set_visible(False)

ax.set_frame_on(False) 

ax.set_xlabel(None)

ax.set_ylabel(None)


#save the plots in testing folder

plt.savefig('mfcc_image.png')
Yu Hao
  • 119,891
  • 44
  • 235
  • 294

1 Answers1

0

To create a plot without it showing automatically in Jupyter, create the figure using the object-oriented interface.

x, sr= librosa.load(file_name, sr=sr)
mfcc_feature= librosa.feature.mfcc(x, sr=sr)

fig, ax = plt.subplots(1, figsize=(12,8))
mfcc_image=librosa.display.specshow(mfcc_feature, ax=ax, sr=sr, y_axis='linear')
ax.axes.get_xaxis().set_visible(False)
ax.axes.get_yaxis().set_visible(False)
ax.set_frame_on(False)
ax.set_xlabel(None)
ax.set_ylabel(None)
#save the plots in testing folder
fig.savefig('mfcc_image.png')

But if you just want to save the MFCC data as an image, you do not have to plot, but can save the values directly. You can use some of the example code from here: How can I save a Librosa spectrogram plot as a specific sized image?

Jon Nordby
  • 5,494
  • 1
  • 21
  • 50