Error when trying to display colorbar using matplotlib library Python 3.9

Question:

I’m currently learning to use the librosa library and when trying to display a colorbar on an associated spectrogram, I get an inexplicable error. I’m not that familiar with matplotlib I’ve searched everywhere for a solution and I can’t help but feel I’m missing something here.

The following code:

n_fft = 2048 #number of samples (window of performing an FFT)
hop_length = 512 #amount shifting each fourier transform to the right

stft = librosa.core.stft(signal, hop_length=hop_length, n_fft=n_fft)

spectogram = np.abs(stft)

log_spectogram = librosa.amplitude_to_db(spectogram)

librosa.display.specshow(log_spectogram, sr=sr, hop_length=hop_length)
plt.xlabel("Time")
plt.ylabel("Frequency")
plt.colorbar()
plt.show()

causes an AttributeError:

AttributeError: module 'matplotlib' has no attribute 'axes'. Did you mean: 'axis'?

However, if I remove the line

plt.colorbar()

I get the following result, which is OK, but I really need the colorbar.

SpectrogramNoColorBar

I need this:

SpectrogramWithColorBar

I tried using the object oriented interface for displaying the spectrogram as in the librosa documentation : https://librosa.org/doc/main/auto_examples/plot_display.html, but to no success.

Please help me.

EDIT:
Here is the full code of my script:

import librosa, librosa.display
import matplotlib.pyplot as plt
import numpy as np

file = "Audio_ML\blues_sample.wav"
#waveform
#loading the audio file with sample rate=22050(fine for audio data)
signal, sr = librosa.load(file, sr=22050) # -> signal(numpy array) containing sr*T -> 22050 * 96
# librosa.display.waveshow(signal, sr=sr)
# plt.xlabel("Time")
# plt.ylabel("Amplitude")
# plt.show()

#FTT -> spectrum (FAST FOURIER TRANSFORM TO GO FROM TIME DOMAIN TO FREQUENCY DOMAIN)
fft = np.fft.fft(signal)

magnitude = np.abs(fft) #magnitudes of each frequency
frequency = np.linspace(0, sr, len(magnitude))

left_frequency = frequency[:int(len(frequency)/2)]
left_magnitude = magnitude[:int(len(frequency)/2)]

# plt.plot(left_frequency, left_magnitude)
# plt.xlabel("Frequency")
# plt.ylabel("Magnitude")
# plt.show()

#STFT -> spectogram (SHORT TIME FOURIER TRANSFORM)
n_fft = 2048 #number of samples (window of performing an FFT)
hop_length = 512 #amount shifting each fourier transform to the right

stft = librosa.core.stft(signal, hop_length=hop_length, n_fft=n_fft)

spectogram = np.abs(stft)

log_spectogram = librosa.amplitude_to_db(spectogram)

librosa.display.specshow(log_spectogram, sr=sr, hop_length=hop_length)
plt.xlabel("Time")
plt.ylabel("Frequency")
plt.colorbar()
plt.show()

#MFCCs
MFCCs = librosa.feature.mfcc(y=signal, n_fft=n_fft, hop_length=hop_length, n_mfcc=13)

I am using matplotlib 3.7 and librosa 0.10.0

Asked By: paul7aa

||

Answers:

It seems the problem was to do with the most recent version of matplotlib.
I managed to fix the problem by downgrading matplotlib 3.7.0 to matplotlib 3.6.0 using

pip install matplotlib==3.6.0

I got the desired colorbar after doing this.

Thank you for trying to help!

Answered By: paul7aa
Categories: questions Tags: , , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.