I am following some example code from Librosa documentation in order to display a spectrogram of a STFT. The problem is that the spectrogram never appears, when I run the program in a terminal window, I briefly see a program launch (the same as would launch on a call to plt.show()
) but it disappears immediately.
Do I need to explicitly call a method to show the img
object in the code below? If not, what could be causing the plot to not show?
# read the .wav file
audio, sample_rate = librosa.load("/Users/user/Desktop/voice.wav")
# perform stft using librosa module
audio_stft_1000 = np.abs(librosa.stft(audio, n_fft=1000))
audio_stft_3000 = np.abs(librosa.stft(audio, n_fft=3000))
# create plots according to docs linked above
fig, ax = plt.subplots()
img = librosa.display.specshow(librosa.amplitude_to_db(audio_stft_1000,
ref=np.max),
y_axis='log', x_axis='time', ax=ax)
ax.set_title('Power spectrogram')
fig.colorbar(img, ax=ax, format="%+2.0f dB")
Very simple solution:
plt.show()