matplotlib Python的安装问题

2024-03-28 19:36:56 发布

您现在位置:Python中文网/ 问答频道 /正文

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

Tags: inbackendmatplotliblibpackagesaslinesite
1条回答
网友
1楼 · 发布于 2024-03-28 19:36:56

问题原因 在mac os中,matplotlib的图像渲染后端(默认情况下,使用Cocoa的API进行渲染的后端是什么)。有Qt4Agg和GTKAgg,作为后端不是默认的。设置与其他windows或linux操作系统不同的macosx后端。

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

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

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

相关问题 更多 >