运行Librosa代码报错:AttributeError: 'Line2D'对象无'max_sr'属性
解决AttributeError: 'Line2D' object has no property 'max_sr'问题
错误原因
max_sr并非librosa.display.waveshow的有效参数,当你传入该参数时,函数会尝试将其作为matplotlib的Line2D对象属性进行设置,但Line2D不存在此属性,因此触发报错。
解决方法
如果你需要将音频以1000Hz的采样率展示,需先对音频进行降采样处理,再传入waveshow:
from matplotlib import pyplot as plt import librosa # 对原始音频进行降采样,目标采样率设为1000Hz y_resampled = librosa.resample(y, orig_sr=sr, target_sr=1000) plt.figure(figsize=(15,2)) # 传入降采样后的音频和对应的采样率 librosa.display.waveshow(y=y_resampled, sr=1000, alpha=0.25, color='blue') plt.show()
补充说明
- 直接移除
max_sr=1000参数可消除报错,但无法实现降采样展示的需求。 - 确保你的librosa版本为最新稳定版,避免因版本差异导致的参数兼容问题。
内容的提问来源于stack exchange,提问作者Isha Pathania
相关产品推荐
相关产品推荐

