安装matplotlib Python时出现问题

13 浏览
0 Comments

安装matplotlib Python时出现问题

这个问题已经在这里有了答案:

python matplotlib framework under macosx?

我在安装matplotlib包后发现无法导入matplotlib.pyplot as plt。任何建议将非常感激。

>>> import matplotlib.pyplot as plt
Traceback (most recent call last):
  File "", line 1, in 
  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 
    _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 
    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.

admin 更改状态以发布 2023年5月23日
0
0 Comments

问题原因

在mac os中,matplotlib的图像渲染后端使用Cocoa API作为默认输入,而Qt4Agg和GTKAgg并不是默认的后端。与其他windows或者linux系统相比,macosx的后端设置是不同的。

解决方案

  • 我假设您已经安装了matplotlib pip,那么您的根目录下会有一个名为~/.matplotlib的文件夹。
  • 在那里创建一个文件~/.matplotlib/matplotlibrc,并添加以下代码:backend: TkAgg

通过这个链接,您可以尝试不同的图表。

0