【问题标题】:Installation Issue with matplotlib Python [duplicate]matplotlib Python的安装问题[重复]
【发布时间】:2014-03-14 03:01:17
【问题描述】:

我在安装 ma​​tplotlib 包后遇到问题,无法将 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.

【问题讨论】:

  • link 报告您在此处提出的同一问题。
  • 运行 pythonw 而不是 python 对我有用

标签: python macos matplotlib anaconda


【解决方案1】:

问题原因

在 matplotlib 的 mac os 图像渲染后端(默认使用 Cocoa 的 API 渲染的后端是什么)。有 Qt4Agg 和 GTKAgg 并且作为后端不是默认的。设置与其他windows或linux操作系统不同的macosx后端。

解决方案

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

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

【讨论】:

  • 是的,这行得通!如果有人想了解发生了什么,请参阅matplotlib.org/faq/usage_faq.html#what-is-a-backend
  • 有些用户可能不想更改所有脚本的后端。这个页面——matplotlib.org/faq/usage_faq.html#what-is-a-backend——告诉了另一种方式:包括声明import matplotlib as mpl,然后是mpl.use('TkAgg'),然后导入pyplot。
  • 作为后续问题,为什么在我的根文件夹中而不是在我安装它的虚拟环境中有一个文件夹 .matplotlib?
  • Python 3.3.6,我明白了:File "/opt/local/Library/Frameworks/Python.framework/Versions/3.3/lib/python3.3/tkinter/__init__.py", line 40, in &lt;module&gt; import _tkinter # If this fails your Python may not be configured for Tk ImportError: No module named '_tkinter'
  • 谢谢你:D 我用 virtualenv 和你的修复,它对我有用!
猜你喜欢
  • 2021-01-24
  • 1970-01-01
  • 2021-11-29
  • 2021-02-17
  • 1970-01-01
  • 2011-01-28
  • 2020-08-09
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多