Pythonでスペクトログラム表示する
音声について調べるためにPythonでスペクトログラムを出すコードを書いているのですが、
エラーが出ていて、このエラーの意味と解決策を教えていただきたいです。
発生している問題・エラー
Traceback (most recent call last):
File "c:\Users\.DESKTOP-BS9K2U1\Desktop\Python\voice.py", line 32, in <module>
plt.colorbar(format='%+2.0f dB')
File "C:\Users\.DESKTOP-BS9K2U1\AppData\Local\Programs\Python\Python310\lib\site-packages\matplotlib\pyplot.py", line 2137, in colorbar
ret = gcf().colorbar(mappable, cax=cax, ax=ax, **kwargs)
File "C:\Users\.DESKTOP-BS9K2U1\AppData\Local\Programs\Python\Python310\lib\site-packages\matplotlib\figure.py", line 1279, in colorbar
and isinstance(ax, mpl.axes._base._AxesBase)
File "C:\Users\.DESKTOP-BS9K2U1\AppData\Local\Programs\Python\Python310\lib\site-packages\matplotlib\_api\__init__.py", line 226, in __getattr__
raise AttributeError(
AttributeError: module 'matplotlib' has no attribute 'axes'. Did you mean: 'axis'?
該当するソースコード
import librosa
import librosa.display
import matplotlib.pyplot as plt
import numpy as np
# ファイル読み込み
filename = r'C:\Users\.DESKTOP-BS9K2U1\Desktop\Python\test.wav'
y,sr = librosa.load(filename)
# y = 音声データが入る
# sr = サンプリングレート値 (1秒間の音声データ数)が入る
print('サンプリングレート:',sr) #サンプリングレート
print('音声:',y)
print('トータルサンプル数:',len(y)) #サンプル数
print('秒数:',len(y)/sr) #秒数
#時間領域でのグラフ
plt.figure(figsize=(6,2))
librosa.display.waveshow(y=y, sr=sr)
plt.ylabel('amplitude')
plt.xlabel('frequency')
plt.savefig("tenki.png")
plt.show()
D = librosa.stft(y) # STFT
S, phase = librosa.magphase(D) # 複素数を強度と位相へ変換
Sdb = librosa.amplitude_to_db(S) # 強度をdb単位へ変換
# スペクトログラムを表示
plt.figure(figsize=(8, 4))
librosa.display.specshow(Sdb,sr=sr,x_axis='time', y_axis='log')
plt.colorbar(format='%+2.0f dB')
plt.savefig('Spectrogram.png')
plt.show()
0 likes