2014-02-14 100 views
265

安装matplotlib包无法导入matplotlib.pyplot为plt后,我有问题。任何建议将不胜感激。matplotlib安装问题蟒蛇Python

>>> import matplotlib.pyplot as plt 
Traceback (most recent call last): 
    File "<stdin>", line 1, in <module> 
    File "//anaconda/lib/python2.7/site-packages/matplotlib-1.3.1-py2.7-macosx-10.5-x86_64.egg/matplotlib/pyplot.py", line 98, in <module> 
    _backend_mod, new_figure_manager, draw_if_interactive, _show = pylab_setup() 
    File "//anaconda/lib/python2.7/site-packages/matplotlib-1.3.1-py2.7-macosx-10.5-x86_64.egg/matplotlib/backends/__init__.py", line 28, in pylab_setup 
    globals(),locals(),[backend_name],0) 
    File "//anaconda/lib/python2.7/site-packages/matplotlib-1.3.1-py2.7-macosx-10.5-x86_64.egg/matplotlib/backends/backend_macosx.py", line 21, in <module> 
    from matplotlib.backends import _macosx 
**RuntimeError**: 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. 
+0

此[链接](http://stackoverflow.com/questions/4130355/python-matplotlib-framework-under-macosx)报告您在这里介绍的同一问题。 – Jack

+0

运行pythonw而不是python为我工作 – phoenix

回答

781

问题原因 在Mac OS图像渲染回到matplotlib结束(什么 - 是 - 一 - 后端,可使用可可的API默认情况下)。有Qt4Agg和GTKAgg,作为后端不是默认设置。设置不同于其他窗口或linux操作系统的macosx的后端。

我解决以下方式这个问题:

  • 我假设你已经安装了PIP matplotlib,有你的根目录中称为~/.matplotlib
  • 创建一个文件~/.matplotlib/matplotlibrc那里,并添加以下代码:backend: TkAgg

从这个link你可以尝试不同的图。

+4

是的,这个工程!如果有人想知道发生了什么,请参阅http://matplotlib.org/faq/usage_faq.html#what-is-a-backend –

+112

有些用户可能不想更改所有脚本的后端。这个页面 - http://matplotlib.org/faq/usage_faq.html#what-is-a-backend - 告诉我们另一种方式:包含语句'import matplotlib as mpl' then'mpl.use('TkAgg') “之后,然后为pyplot进行导入。 –

+6

作为后续问题,为什么我的根文件夹中有一个.matplotlib文件夹,而不是我安装它的虚拟环境中? – PDN