How to save a Librosa spectrogram plot as a specific sized image?

MatplotlibPythonData Visualization

To save a Librosa spectrogram plot as a specific sized image in matplotlib, we can take the following steps −

  • Set the figure size and adjust the padding between and around the subplots.
  • Load an audio file as a floating point time series.
  • Turn off the axes.
  • Compute a mel-scaled spectrogram.
  • Display the spectrogram.
  • To display the figure, use show() method.

Example

from matplotlib import pyplot as plt
from librosa import display
import librosa
import numpy as np

plt.rcParams["figure.figsize"] = [7.50, 3.50]
plt.rcParams["figure.autolayout"] = True

sig, fs = librosa.load('test.wav')
plt.axis('off')
S = librosa.feature.melspectrogram(y=sig, sr=fs)
librosa.display.specshow(librosa.power_to_db(S, ref=np.max))

plt.show()

Output

raja
Published on 03-Aug-2021 12:36:25
Advertisements