matplotlib Python的安装问题

安装matplotlib软件包后无法导入matplotlib.pyplot作为plt我有问题。 任何build议将不胜感激。

>>> 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. 

问题原因在mac os图像渲染matplotlib的后端(默认情况下使用Cocoa的API呈现什么是后端)。 有Qt4Agg和GTKAgg作为后端不是默认的。 设置不同于其他窗口或Linux操作系统的macosx的后端。

我按照以下方式解决此问题:

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

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