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
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!
I had a similar problem importing a file in Pycharm's console window with the following code:
import os
import pandas as pd
if os.name != 'posix':
import matplotlib
matplotlib.use('TkAgg')
...
def create_pareto_chart(df: pd.DataFrame, topax: matplotlib.axes.Axes):
...
with the following error message:
File "C:\Users\Rober\PycharmProjects\Remora\Utils.py", line 84, in <module>
def create_pareto_chart(delay_df: pd.DataFrame, topax: matplotlib.axes.Axes):
^^^^^^^^^^^^^^^
File "C:\Users\Rober\PycharmProjects\Remora\venv\Lib\site-packages\matplotlib\_api\__init__.py", line 226, in __getattr__
raise AttributeError(
AttributeError: module 'matplotlib' has no attribute 'axes'
Based on the bug report at https://github.com/matplotlib/matplotlib/issues/25506#issuecomment-1518359815, the issue is lazy loading by Pycharm (in my case) and librosa (in your case), where the submodules are not found. Importing matplotlib.pyplot before matplotlib dependencies solves the problem even with the latest version:
import os
import pandas as pd
import matplotlib
import matplotlib.pyplot as plt
if os.name != 'posix':
matplotlib.use('TkAgg')
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With