macosx下的python matplotlib框架?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4130355/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
python matplotlib framework under macosx?
提问by Neil G
I am getting this error:
我收到此错误:
/sw/lib/python2.7/site-packages/matplotlib/backends/backend_macosx.py:235: UserWarning: Python is not installed as a framework. The MacOSX backend may not work correctly if Python is not installed as a framework. Please see the Python documentation for more information on installing Python as a framework on Mac OS X
/sw/lib/python2.7/site-packages/matplotlib/backends/backend_macosx.py:235: UserWarning: Python 未作为框架安装。如果 Python 未作为框架安装,MacOSX 后端可能无法正常工作。有关在 Mac OS X 上将 Python 作为框架安装的更多信息,请参阅 Python 文档
I installed python27 using fink and it's using the default matplotlib is using macosx framework.
我使用 fink 安装了 python27,它使用默认的 matplotlib 使用 macosx 框架。
采纳答案by Ned Deily
There are two ways Python can be built and installed on Mac OS X. One is as a traditional flat Unix-y shared library. The other is known as a frameworkinstall, a file layout similar to other frameworks on OS X where all of the component directories (include, lib, bin) for the product are installed as subdirectories under the main framework directory. The Finkproject installs Pythons using the Unix shared library method. Most other distributors, including the Apple-supplied Pythons in OS X, the python.orginstallers, and the MacPortsproject, install frameworkversions of Python. One of the advantages of a framework installation is that it will work properly with various OS X API calls that require a window manager connection (generally GUI-related interfaces) because the Python interpreter is packaged as an app bundle within the framework.
在 Mac OS X 上构建和安装 Python 有两种方式。一种是作为传统的平面 Unix-y 共享库。另一种称为framework安装,一种类似于 OS X 上的其他框架的文件布局,其中产品的所有组件目录 ( include、lib、bin) 作为主框架目录下的子目录安装。该Fink项目使用 Unix 共享库方法安装 Python。大多数其他发行商,包括 OS X 中 Apple 提供的 Python、python.org安装程序和MacPorts项目,都安装frameworkPython 的版本。框架安装的优点之一是它可以与需要窗口管理器连接(通常是 GUI 相关的接口)的各种 OS X API 调用一起正常工作,因为 Python 解释器被打包为框架内的应用程序包。
If you do need the functions in matplotlib that require the GUI functions, the simplest approach may be to switch to MacPorts which also packages matplotlib (port py27-matplotlib) and its dependencies. If so, be careful not to mix packages between Finkand MacPorts. It's best to stick with one or the other unless you are really careful. Adjust your shell path accordingly; it would be safest to remove all of the Fink packages and install MacPorts versions.
如果您确实需要 matplotlib 中需要 GUI 函数的函数,最简单的方法可能是切换到 MacPorts,它也打包了 matplotlib ( port py27-matplotlib) 及其依赖项。如果是这样,请注意不要在Fink和之间混合包MacPorts。除非您非常小心,否则最好坚持其中之一。相应地调整你的 shell 路径;最安全的做法是删除所有 Fink 软件包并安装 MacPorts 版本。
回答by David M. Rogers
This shows up for me when switching to OSX 10.10 (Yosemite). I fixed it by switching to the WXAgg backend.
切换到 OSX 10.10 (Yosemite) 时,我会看到这一点。我通过切换到 WXAgg 后端修复了它。
fink install wxpython300-py27
mkdir ~/.matplotlib
echo "backend : WXAgg" >>~/.matplotlib/matplotlibrc
http://matplotlib.org/1.3.0/faq/usage_faq.html#what-is-a-backend
http://matplotlib.org/1.3.0/faq/usage_faq.html#what-is-a-backend
If the matplotlibrc file is not found there, put matplotlibrc and check the location with:
如果在那里找不到 matplotlibrc 文件,请放置 matplotlibrc 并使用以下命令检查位置:
import matplotlib as mpl
mpl.get_configdir()
回答by Jonathan
Optionally you could use the Aggbackend which requires no extra installation of anything. Just put backend : Agginto ~/.matplotlib/matplotlibrc
您可以选择使用Agg不需要额外安装任何东西的后端。只要把backend : Agg成~/.matplotlib/matplotlibrc
回答by Iron Pillow
Some users may not want to change the backend for all of their scripts. This page -- http://matplotlib.org/faq/usage_faq.html#what-is-a-backend-- tells another way:
一些用户可能不想更改所有脚本的后端。这个页面——http://matplotlib.org/faq/usage_faq.html#what-is-a-backend——讲述了另一种方式:
import matplotlib
matplotlib.use('TkAgg')
You have to do this before importing a subpackage of matplotlibor a third-party package that depends on matplotlib.
您必须在导入matplotlib依赖于matplotlib.
回答by Wendy Jan
I hit this problem using the Anaconda distribution of Python on my computer and a virtual conda environment with Python 3.4.
我在我的计算机上使用 Python 的 Anaconda 发行版和使用 Python 3.4 的虚拟 conda 环境遇到了这个问题。
After doing "pip install matplotlib", I could import matplotlit in my code fine, but it would give an error when I tried to plot images.
执行“pip install matplotlib”后,我可以在我的代码中导入 matplotlit ,但是当我尝试绘制图像时会出错。
So I went back to the Terminal and used the recommended conda command for Anaconda Python ("conda install matplotlib"), and the issue resolved itself (keep in mind this was all within an environment that I had created using the conda command tool at http://conda.pydata.org/docs/using/envs.html). My system is running Mac OSX 10.10.5.
所以我回到终端并使用推荐的 Anaconda Python conda 命令(“conda install matplotlib”),问题自行解决(请记住,这一切都在我使用http 上的 conda 命令工具创建的环境中)://conda.pydata.org/docs/using/envs.html)。我的系统运行的是 Mac OSX 10.10.5。
回答by Jonny Brooks
Check out the official FAQ on for Working with Matplotlib on OSX.It has several solutions depending on your situation. The one that worked for me is:
查看关于在 OSX上使用 Matplotlib的官方常见问题解答。根据您的情况,它有多种解决方案。对我有用的是:
- Step 1: Open terminal
- Step 2: Execute
conda install python.appto install python.app - Step 3: Run python script with
pythonwe.g.pythonw my_script.py
- 第一步:打开终端
- 第二步:执行
conda install python.app安装python.app - 第 3 步:使用
pythonweg运行 python 脚本pythonw my_script.py
回答by Sachin
If you are facing the same problem even after adding "backend: TkAgg"in ~/.matplotlib/matplotlibrctheir might a problem with your virtual environment.
如果你甚至加入后面临同样的问题"backend: TkAgg"在~/.matplotlib/matplotlibrc他们可能会与你的虚拟环境中的问题。
So if your are on python3 use venvinstead of virtualenv.
因此,如果您使用的是 python3,请使用venv而不是 virtualenv。
To use venv with python3 use python -m venv my-virtualenvto make a virtual environment and use source my-virtualenv/bin/activateto activate the virtualenv.
要将 venv 与 python3 一起使用python -m venv my-virtualenv,请创建一个虚拟环境并用于source my-virtualenv/bin/activate激活 virtualenv。
Rest is same as virtualenv. Otherwise use
PYTHON_CONFIGURE_OPTS="--enable-framework" pyenv install x.x.xif you want to use virtualenv as your virtual environment.
Rest 与 virtualenv 相同。否则,
PYTHON_CONFIGURE_OPTS="--enable-framework" pyenv install x.x.x如果您想使用 virtualenv 作为您的虚拟环境,请使用。
回答by Max Power
Generally, I've been using
一般来说,我一直在使用
import matplotlib
matplotlib.use('Agg')
to solve this. Recently however I got this framework error running tests on a codebase I should only be making very limited changes to. In that case, following matplotlib's OSX documentation's advice on how I set up my virtual environment worked for me:
来解决这个问题。但是最近我在代码库上运行测试时遇到了这个框架错误,我应该只对其进行非常有限的更改。在这种情况下,遵循 matplotlib 的 OSX 文档关于如何设置虚拟环境的建议对我有用:
If you are on Python 3, use venv instead of virtualenv:
python -m venv my-virtualenv
source my-virtualenv/bin/activate
回答by after aver
Import the library by following code sequences works for me. With this does not need to create new folder:
通过以下代码序列导入库对我有用。有了这个不需要创建新文件夹:
import matplotlib
matplotlib.use('TkAgg')
import matplotlib.pyplot as plt
回答by Alchanic Liu
Simply aliasing a new command to launch python in ~/.bash_profile will do the trick.
只需在 ~/.bash_profile 中为启动 python 的新命令添加别名即可解决问题。
alias vpython3=/Library/Frameworks/Python.framework/Versions/3.6(replace with your own python version)/bin/python3
alias vpython3=/Library/Frameworks/Python.framework/Versions/3.6(替换为你自己的python版本)/bin/python3
then 'source ~/.bash_profile' and use vpython3 to launch python3.
然后'source ~/.bash_profile' 并使用 vpython3 启动 python3。
Explanation: Python is actually by default installed as framework on mac, but using virtualenv would link your python3 command under the created virtual environment, instead of the above framework directory ('which python3' in terminal and you'll see that). Perhaps Matplotlib has to find the bin/ include/ lib/,etc in the python framework.
说明:Python 实际上默认作为框架安装在 mac 上,但是使用 virtualenv 会在创建的虚拟环境下链接你的 python3 命令,而不是上面的框架目录(终端中的'which python3',你会看到)。也许 Matplotlib 必须在 python 框架中找到 bin/include/lib/ 等。

