matplotlib报错:AttributeError: Unknown property wrap

在Ubuntu 14.04环境下,使用python2.7运行matplotlib demo时遇到AttributeError,由于系统自带matplotlib版本为1.3.1,不支持wrap属性。参考Stack Overflow解答,了解到问题在于matplotlib版本低于1.5。通过手动下载并安装matplotlib 1.5.1解决此问题,详细步骤包括下载源码、解压、运行build和install命令。

环境:ubuntu14.04, python2.7

在运行demo时报错:http://matplotlib.org/examples/text_labels_and_annotations/autowrap_demo.html

查找到原因:http://stackoverflow.com/questions/34375618/the-use-of-kwarg-wrap-for-a-pyplot-text-artist-in-matplotlib

“You are not using 1.5 if the keyword is not working”

自己查了下版本:

>>> import  matplotlib
>>> matplotlib.__version__
'1.3.1'

果然版本太低


之前是用命令sudo apt-get install python-matplotlib来安装的,查了下,发现ubuntu14.04的python-matplotlib包是1.3的,要到ubuntu16.04才升级到1.5

所以,只能手动安装matplotlib

教程(未测试):http://www.cnblogs.com/darkknightzh/p/5681059.html

1. 下载网址:https://sourceforge.net/projects/matplotlib/files/matplotlib/matplotlib-1.5.1/
选择matplotlib-1.5.1.tar.gz
2. 解压后,cd到该文件夹中。而后输入
python setup.py build
sudo python setup.py install
等待安装完成。

### 错误原因 该错误提示 `AttributeError: module 'backend_interagg' has no attribute 'FigureCanvas'` 通常是由 Matplotlib 后端配置不正确引起的。Matplotlib 使用不同的后端来渲染图形,默认情况下会尝试使用一个合适的后端,但在某些环境下(如特定的 IDE 或 Jupyter Notebook),可能会因为后端设置不当而导致此错误 [^2]。 具体而言,Matplotlib 在运行时尝试加载 `backend_interagg` 模块,但该模块缺少 `FigureCanvas` 类,导致程序无法继续执行绘图任务 [^3]。 --- ### 解决方案 #### 1. 更改 Matplotlib 后端为 Agg 或 TkAgg 可以在代码中显式指定使用 `'Agg'` 或 `'TkAgg'` 等稳定后端,以避免默认后端带来的兼容性问题。建议在导入 `matplotlib.pyplot` 之前使用 `matplotlib.use()` 方法设置后端: ```python import matplotlib matplotlib.use('Agg') # 设置为非交互式后端,适用于生成图像文件 import matplotlib.pyplot as plt ``` 如果希望在桌面环境中显示图形,则可以使用 `'TkAgg'` 后端: ```python import matplotlib matplotlib.use('TkAgg') import matplotlib.pyplot as plt ``` 这种方式确保了即使在没有图形界面支持的环境中,也能顺利生成图像文件 [^3]。 #### 2. 修改 Matplotlib 配置文件 可以通过修改 `matplotlibrc` 文件中的 `backend` 参数来永久更改默认后端。找到配置文件路径(可通过以下命令获取): ```bash python -c "import matplotlib; print(matplotlib.get_configdir())" ``` 然后编辑 `matplotlibrc` 文件,将其中的 `backend` 行改为: ``` backend : Agg ``` 或根据需要选择其他后端,例如 `TkAgg`、`Agg`、`PDF` 等 [^1]。 #### 3. 升级或重新安装 Matplotlib 有时旧版本的 Matplotlib 存在兼容性问题,升级到最新版本可能解决该错误: ```bash pip install --upgrade matplotlib ``` 若升级无效,可尝试卸载后重新安装: ```bash pip uninstall matplotlib pip install matplotlib ``` 这有助于修复因安装损坏或依赖缺失导致的问题 [^1]。 #### 4. 取消 PyCharm 中“在工具窗口中显示绘图”选项 在 PyCharm 等 IDE 中,启用“Show plots in tool window”功能可能导致后端冲突。可在设置中禁用该选项: 进入 **Settings > Tools > Python Scientific**,取消勾选 **Show plots in tool window** 。 --- ### 总结与最佳实践 - 若仅需生成图像文件而不显示,推荐使用 `'Agg'` 后端。 - 若需交互式绘图,应选择 `'TkAgg'` 或系统支持的 GUI 后端。 - 对于服务器或无图形界面环境,`'Agg'` 是最稳定的选择。 - 若问题持续存在,优先考虑升级或重装 Matplotlib---
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值