matplotlib报AttributeError无pyplot属性,升级及plt.clf无效如何解决?
Matplotlib 3.5.7 报错 AttributeError 且绘图重复问题解决
问题描述
已升级并确认Matplotlib版本为3.5.7,但运行音频绘图代码时,依旧抛出AttributeError: module 'matplotlib' has no attribute 'pyplot'错误,同时绘图会重复显示两次;重新运行单元格还会再显示一次重复的图,尝试plt.clf()无法解决问题。
复现代码
import numpy as np import librosa import librosa.display import matplotlib.pyplot as plt import os from os import listdir from os.path import isfile, join data_dir = 'LJSpeech-1.1/wavs' data_files = [join(data_dir, f) for f in listdir(data_dir) if isfile(join(data_dir, f)) if 'wav' in f] data_files.sort() print('wave files : ', len(data_files)) print(data_files[0]) # return values # wav : 오디오 신호 # sr : sample rate, default=22050 wav, sr = librosa.core.load(data_files[1]) print('wav :', wav) print('sample rate :', sr, 'length :', len(wav)) print('duration :', len(wav) / sr, 'seconds') plt.figure(figsize=[18, 3]) plt.plot(wav) plt.ylim([-1, 1]) plt.show() # wav 들어보기 import IPython.display as ipd ipd.Audio(wav, rate=sr)
报错信息
AttributeError Traceback (most recent call last) Cell In[3], line 21 19 plt.plot(wav) 20 plt.ylim([-1, 1]) ---> 21 plt.show() 23 # wav 들어보기 24 import IPython.display as ipd File ~/miniconda3/envs/audio_practice/lib/python3.8/site-packages/matplotlib/pyplot.py:446, in show(*args, **kwargs) 402 """ 403 Display all open figures. 404 (...) 443 explicitly there. 444 """ 445 _warn_if_gui_out_of_main_thread() --> 446 return _get_backend_mod().show(*args, **kwargs) File ~/miniconda3/envs/audio_practice/lib/python3.8/site-packages/matplotlib_inline/backend_inline.py:99, in show(close, block) 96 # only call close('all') if any to close 97 # close triggers gc.collect, which can be slow 98 if close and Gcf.get_all_fig_managers(): ---> 99 matplotlib.pyplot.close('all') ... 225 return props[name].__get__(instance) --> 226 raise AttributeError( 227 f"module {cls.__module__!r} has no attribute {name!r}") AttributeError: module 'matplotlib' has no attribute 'pyplot' Output is truncated. View as a scrollable element or open in a text editor. Adjust cell output settings... --------------------------------------------------------------------------- AttributeError Traceback (most recent call last) File ~/miniconda3/envs/audio_practice/lib/python3.8/site-packages/matplotlib_inline/backend_inline.py:99, in show(close, block) 96 # only call close('all') if any to close 97 # close triggers gc.collect, which can be slow 98 if close and Gcf.get_all_fig_managers(): ---> 99 matplotlib.pyplot.close('all') File ~/miniconda3/envs/audio_practice/lib/python3.8/site-packages/matplotlib/_api/__init__.py:226, in caching_module_getattr..__getattr__(name) 224 if name in props: 225 return props[name].__get__(instance) --> 226 raise AttributeError( 227 f"module {cls.__module__!r} has no attribute {name!r}") AttributeError: module 'matplotlib' has no attribute 'pyplot'
解决步骤
排查模块命名冲突
- 检查当前工作目录,确保没有
matplotlib.py或pyplot.py这类与官方模块同名的文件,避免覆盖官方库。 - 运行以下代码确认Matplotlib模块路径:
输出应为虚拟环境或系统Python目录下的官方文件,而非当前目录下的自定义文件。import matplotlib print(matplotlib.__file__)
- 检查当前工作目录,确保没有
修复后端调用异常
- 报错出现在
matplotlib_inline后端调用plt.close('all')时,可尝试显式指定后端,在代码开头添加:import matplotlib matplotlib.use('Agg') # 也可尝试'TkAgg'等其他后端 import matplotlib.pyplot as plt - 强制重新安装
matplotlib-inline包:pip install --force-reinstall matplotlib-inline
- 报错出现在
解决绘图重复问题
- 在创建新图形前显式关闭所有已有图形:
plt.close('all') plt.figure(figsize=[18, 3]) plt.plot(wav) plt.ylim([-1, 1]) plt.show() - 确保Jupyter中
%matplotlib inline魔法命令只执行一次,不要在多个单元格重复运行。
- 在创建新图形前显式关闭所有已有图形:
重置虚拟环境
- 若以上方法无效,创建全新虚拟环境重新安装依赖:
conda create -n new_audio_env python=3.8 conda activate new_audio_env pip install matplotlib==3.5.7 librosa numpy ipython
- 若以上方法无效,创建全新虚拟环境重新安装依赖:
内容的提问来源于stack exchange,提问作者SYJeong
相关产品推荐
相关产品推荐

