macOS下的python matplotlib框架?

发布于 2024-10-01 10:35:48 字数 303 浏览 5 评论 0原文

我收到此错误:

/sw/lib/python2.7/site-packages/matplotlib/backends/backend_macosx.py:235: UserWarning:Python 未作为框架安装。苹果系统 如果未安装 Python,后端可能无法正常工作 框架。请参阅 Python 文档以获取更多信息 在 Mac OS X 上安装 Python 作为框架

我使用 fink 安装了 python27,它使用默认的 matplotlib 使用 macosx 框架。

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

I installed python27 using fink and it's using the default matplotlib is using macosx framework.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(11

萌梦深 2024-10-08 10:35:48

某些用户可能不想更改所有脚本的后端。此页面 - http://matplotlib.org/faq/usage_faq.html #what-is-a-backend -- 告诉另一种方式:

import matplotlib
matplotlib.use('TkAgg')

您必须在导入 matplotlib 的子包或依赖于 matplotlib< 的第三方包之前执行此操作/代码>。

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:

import matplotlib
matplotlib.use('TkAgg')

You have to do this before importing a subpackage of matplotlib or a third-party package that depends on matplotlib.

如痴如狂 2024-10-08 10:35:48

您也可以选择使用 Agg 后端,它不需要额外安装任何东西。只需将 backend : Agg 放入 ~/.matplotlib/matplotlibrc

Optionally you could use the Agg backend which requires no extra installation of anything. Just put backend : Agg into ~/.matplotlib/matplotlibrc

暖伴 2024-10-08 10:35:48

在 Mac OS X 上构建和安装 Python 的方式有两种。一种是作为传统的扁平 Unix-y 共享库。另一种称为框架安装,其文件布局类似于 OS X 上的其他框架,其中所有组件目录(includelib , bin) 产品作为子目录安装在主框架目录下。 Fink 项目使用 Unix 共享库方法安装 Python。大多数其他发行商,包括 Apple 在 OS X 中提供的 Python、python.org 安装程序和 MacPorts 项目,都安装 framework 版本Python。框架安装的优点之一是它可以与需要窗口管理器连接(通常与 GUI 相关的接口)的各种 OS X API 调用正常工作,因为 Python 解释器在框架内打包为应用程序包。

如果您确实需要 matplotlib 中需要 GUI 功能的功能,最简单的方法可能是切换到 MacPorts,它也打包了 matplotlib (port py27-matplotlib) 及其依赖项。如果是这样,请注意不要在 FinkMacPorts 之间混合使用软件包。除非您非常小心,否则最好坚持使用其中之一。相应地调整你的 shell 路径;最安全的做法是删除所有 Fink 软件包并安装 MacPorts 版本。

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 framework install, 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 Fink project installs Pythons using the Unix shared library method. Most other distributors, including the Apple-supplied Pythons in OS X, the python.org installers, and the MacPorts project, install framework versions 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.

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

那请放手 2024-10-08 10:35:48

当我切换到 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

如果在那里找不到 matplotlibrc 文件,请放入 matplotlibrc 并使用以下命令检查位置:

import matplotlib as mpl
mpl.get_configdir()

This shows up for me when switching to OSX 10.10 (Yosemite). I fixed it by switching to the WXAgg backend.

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

If the matplotlibrc file is not found there, put matplotlibrc and check the location with:

import matplotlib as mpl
mpl.get_configdir()
似梦非梦 2024-10-08 10:35:48

我在计算机上使用 Python 的 Anaconda 发行版和 Python 3.4 的虚拟 conda 环境遇到了这个问题。

执行“pip install matplotlib”后,我可以在代码中导入 matplotlit ,但当我尝试绘制图像时会出现错误。

因此,我回到终端并使用 Anaconda Python 推荐的 conda 命令(“conda install matplotlib”),问题自行解决(请记住,这一切都在我使用 conda 命令工具创建的环境中 < a href="http://conda.pydata.org/docs/using/envs.html" rel="noreferrer">http://conda.pydata.org/docs/using/envs.html) 。我的系统运行的是 Mac OSX 10.10.5。

I hit this problem using the Anaconda distribution of Python on my computer and a virtual conda environment with Python 3.4.

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.

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.

左岸枫 2024-10-08 10:35:48

我正在运行 OSX 10.14,并安装了 pyenv 的 Python 3.7.1。

我无法获得其他答案中列出的任何 后端为我工作(TkAggWXAggmacosx)。

相反,有效的是较新的 Qt5Agg 后端,使用 pip install pyqt5 安装,然后创建包含 ~/.matplotlib/matplotlibrc 文件>后端:Qt5Agg(根据其他答案)。

I'm running OSX 10.14 with Python 3.7.1 installed with pyenv.

I could not get any of the backends listed in the other answers to work for me (TkAgg, WXAgg, macosx).

Instead what worked is the newer Qt5Agg backend, installed with pip install pyqt5, and then creating a ~/.matplotlib/matplotlibrc file containing backend: Qt5Agg (per the other answers).

拔了角的鹿 2024-10-08 10:35:48

通过以下代码序列导入库对我有用。这样就不需要创建新文件夹了:

import matplotlib
matplotlib.use('TkAgg')
import matplotlib.pyplot as plt

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
烂人 2024-10-08 10:35:48

如果您在 ~/.matplotlib/matplotlibrc 中添加 "backend: TkAgg" 后仍面临同样的问题,那么您的虚拟环境可能存在问题。

因此,如果您使用的是 python3,请使用 venv 而不是 virtualenv。

要将 venv 与 python3 结合使用,请使用 python -m venv my-virtualenv 创建虚拟环境并使用 source my-virtualenv/bin/activate 激活 virtualenv。

其余与 virtualenv 相同。否则使用
PYTHON_CONFIGURE_OPTS="--enable-framework" pyenv install xxx 如果您想使用 virtualenv 作为虚拟环境。

If you are facing the same problem even after adding "backend: TkAgg" in ~/.matplotlib/matplotlibrc their might a problem with your virtual environment.

So if your are on python3 use venv instead of virtualenv.

To use venv with python3 use python -m venv my-virtualenv to make a virtual environment and use source my-virtualenv/bin/activate to activate the virtualenv.

Rest is same as virtualenv. Otherwise use
PYTHON_CONFIGURE_OPTS="--enable-framework" pyenv install x.x.x if you want to use virtualenv as your virtual environment.

孤独患者 2024-10-08 10:35:48

一般来说,我一直用它

import matplotlib
matplotlib.use('Agg')

来解决这个问题。然而最近,我在代码库上运行测试时遇到了这个框架错误,我应该只对其进行非常有限的更改。在这种情况下,遵循 matplotlib 的 OSX 文档关于如何设置虚拟环境的建议对我有用:

If you are on Python 3, use venv instead of virtualenv:

python -m venv my-virtualenv
source my-virtualenv/bin/activate

来自: https ://matplotlib.org/faq/osx_framework.html

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:

If you are on Python 3, use venv instead of virtualenv:

python -m venv my-virtualenv
source my-virtualenv/bin/activate

from: https://matplotlib.org/faq/osx_framework.html

影子是时光的心 2024-10-08 10:35:48

查看在 OSX 上使用 Matplotlib 的官方常见问题解答。解决方案取决于您的情况。对我有用的是:

  • 步骤 1:打开终端
  • 步骤 2:执行 conda install python.app 来安装 python.app
  • 步骤 3:使用 pythonw 运行 python 脚本 例如pythonw my_script.py

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:

  • Step 1: Open terminal
  • Step 2: Execute conda install python.app to install python.app
  • Step 3: Run python script with pythonw e.g. pythonw my_script.py
彡翼 2024-10-08 10:35:48

只需在 ~/.bash_profile 中添加一个新命令的别名来启动 python 就可以了。

alias vpython3=/Library/Frameworks/Python.framework/Versions/3.6(替换为你自己的python版本)/bin/python3

然后 'source ~/.bash_profile' 并使用 vpython3 启动 python3。

说明:Python 实际上在 mac 上默认安装为框架,但使用 virtualenv 会将 python3 命令链接到创建的虚拟环境下,而不是上面的框架目录(终端中的“which python3”,你会看到它)。也许Matplotlib必须在python框架中找到bin/include/lib/等。

Simply aliasing a new command to launch python in ~/.bash_profile will do the trick.

alias vpython3=/Library/Frameworks/Python.framework/Versions/3.6(replace with your own python version)/bin/python3

then 'source ~/.bash_profile' and use vpython3 to launch 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文