matplotlib Python的安装问题

J4c*_*4cK 485 python macos matplotlib anaconda

我在安装matplotlib软件包后无法将matplotlib.pyplot作为plt导入.任何建议都将非常感激.

>>> 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.
Run Code Online (Sandbox Code Playgroud)

J4c*_*4cK 1308

问题原因 在mac os图像渲染中,matplotlib的结尾(默认情况下使用Cocoa的API渲染的后端是什么).有Qt4Agg和GTKAgg,作为后端不是默认值.设置macosx的后端与其他windows或linux os相比有所不同.

我通过以下方式解决此问题:

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

从这个链接你可以尝试不同的图表.

  • 有些用户可能不想更改所有脚本的后端.这个页面 - http://matplotlib.org/faq/usage_faq.html#what-is-a-backend - 告诉另一种方式:包括语句`import matplotlib as mpl`然后`mpl.use('TkAgg') `之后,然后进行pyplot的导入. (268认同)
  • 作为一个后续问题,为什么我的根文件夹中有一个文件夹.matplotlib而不是我安装它的虚拟环境? (18认同)
  • 是的,这个有效!如果有人想了解发生了什么,请参阅http://matplotlib.org/faq/usage_faq.html#what-is-a-backend (10认同)
  • Python 3.3.6,我得到这个:`file"/opt/local/Library/Frameworks/Python.framework/Versions/3.3/lib/python3.3/tkinter/__init__.py",第40行,在<module> import中_tkinter#如果失败,则可能没有为Tk ImportError配置Python:没有名为'_tkinter'的模块 (6认同)
  • 谢谢你:DI使用了virtualenv和你的修复,它对我有用! (2认同)