mac环境下,运行matplotlib时出现ImportError: Python is not installed as a framework

在mac环境下,运行matplotlib时出现ImportError: Python is not installed as a framework. 错误提示。

具体错误信息如下:The Mac OS X backend will not be able to function correctly if Python is not installed as a framework. See the Python documentation for more information on installing Python as a framework on Mac OS X. Please either reinstall Python as a framework, or try one of the other backends. If you are using (Ana)Conda please install python.app and replace the use of 'python' with 'pythonw'. See 'Working with Matplotlib on OSX' in the Matplotlib FAQ for more information. 

这是因为matplotlibrc文件中没有内容,或者backend(后端)为macosx。

Agg支持文件写入,但不支持窗口显示(绘图),所以要改为其他GUI后端:TkAgg、Wx、QtAgg、Qt4Agg等

解决方法一:较麻烦,每次新建项目均需要修改

在终端环境下:找到matplotlibrc文件,路径:该项目虚拟环境virtualenvs/XXXX/lib/python3.7/site-packages/matplotlib/mpl-data/matplotlibrc

$ vim ~/.matplotlib/mpl-data/matplotlibrc

输入backend: TkAgg

输入冒号:,然后输入wq(写入退出)

以上也是plt.show()不显示图的解决办法

解决方法二:使用命令(但不改变matplotlibrc配置文件中的内容)

在项目中的.py文件开头,添加以下代码:

import matplotlib
matplotlib.rcParams[
'backend'] = 'TkAgg'

注:import seaborn as sns也可能出现文章开头提示的类似错误,解决方法:import seaborn as sns需写在以上两行代码之后。

  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

199铱

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值