【问题标题】:Getting matplotlib backends for python 3.6获取 python 3.6 的 matplotlib 后端
【发布时间】:2017-01-27 17:02:40
【问题描述】:

我在 python 3.6(在 Xubuntu 16.04 中)下使用 pip (9.0.1) 安装了 ipython 和 matplotlib,但是当我尝试绘制某些东西时没有显示图像。

使用ipython3 --matplotlib qt 启动 ipython 会出现以下错误:

ImportError: Matplotlib qt-based backends require an external PyQt4, PyQt5,
or PySide package to be installed, but it was not found.

我尝试使用 pip 安装这些,但失败了:

$ pip3.6 install PySide
Collecting PySide
  Downloading PySide-1.2.4.tar.gz (9.3MB)
    100% |████████████████████████████████| 9.3MB 12.8MB/s 
    Complete output from command python setup.py egg_info:
    only these python versions are supported: [(2, 6), (2, 7), (3, 2), (3, 3), (3, 4)]

    ----------------------------------------
Command "python setup.py egg_info" failed with error code 1 in /tmp/pip-build-bzzpzy5q/PySide/
$ pip3.6 install PyQT
Collecting PyQT
  Could not find a version that satisfies the requirement PyQT (from versions: )
No matching distribution found for PyQT
$ pip3.6 install PyQT4
Collecting PyQT4
  Could not find a version that satisfies the requirement PyQT4 (from versions: )
No matching distribution found for PyQT4
$ pip3.6 install PyQT5
Collecting PyQT5
  Could not find a version that satisfies the requirement PyQT5 (from versions: )
No matching distribution found for PyQT5

如果我尝试ipython3 --matplotlib gtk,错误是:

ImportError: Gtk* backend requires pygtk to be installed.

但是:

$ pip3.6 install pygtk
Collecting pygtk
  Could not find a version that satisfies the requirement pygtk (from versions: )
No matching distribution found for pygtk

我似乎明白 PyGobject 或 PyGI 取代了 python 3 的 pygtk。事实上,ipython3 --matplotlib gtk3 导致:

ImportError: Gtk3 backend requires pygobject to be installed.

但是:

$ pip3.6 install pygobject
Collecting pygobject
  Could not find a version that satisfies the requirement pygobject (from versions: )
No matching distribution found for pygobject
$ pip3.6 install PyGObject
Collecting PyGObject
  Could not find a version that satisfies the requirement PyGObject (from versions: )
No matching distribution found for PyGObject

终于pip3.6 install PyGI成功了!

但是 matplotlib 仍然抱怨没有安装 GTK 的东西。

我还应该尝试什么?

【问题讨论】:

  • 我试图放置 python3.6 标签,但我没有足够的声誉。如果有人认为它有用并且有足够的声誉,请不要犹豫,将我的 3.x 标签替换为 3.6 标签。
  • 即使没有 GTK 或 PyQt matplotlib 也应该可以工作。尝试在 ipython 中使用 ipython3 --matplotlib tk 或在脚本中使用 import matplotlib matplotlib.use("TkAgg")。对于有关安装 pyqt 或 GTK 的解决方案,您使用的操作系统、您拥有的 pip 版本等非常重要,因此您可能希望将此信息添加到问题中。
  • 尝试ipython3 --matplotlib tk 得到ModuleNotFoundError: No module named '_tkinter'。安装 tk-dev (stackoverflow.com/a/5459492/1878788) 后,我正在重新编译 python 3.6。
  • 只是提一下,以防你不知道:你总是可以去一个完整的发行版,比如anaconda,它有你需要的一切。
  • 重新编译 tk-dev 安装使我能够让 Tk 后端工作。您可以发布您的建议作为答案。在我的情况下,它是相关且有用的。谢谢。

标签: qt python-3.x matplotlib gtk pip


【解决方案1】:

让 Tk 后端工作

正如@ImportanceOfBeingErnest 建议的那样,至少应该有一个后端可用:基于 Tk 的后端。

如果 Tk 开发库在编译 python 时可用,这是真的。这对我来说不是这样(我想预编译的 python 发行版应该启用 Tk)。

当我尝试使用 Tk 作为 matplotlib 后端 (ipython3 --matplotlib tk) 启动 ipython 时,我收到类似于以下的错误消息:

import _tkinter # If this fails your Python may not be configured for Tk
ImportError: No module named _tkinter

对以下answer 的评论解释了如何在 Ubuntu 中获取 Tk 开发库:apt install tk-dev

执行此操作并重新编译 python 3.6 后,ipython3 --matplotlib tk 会话开始时没有错误并且可以显示图形。

设置默认后端选项

matplotlib documentation 给出了一个配置文件示例,我下载为~/.config/matplotlib/matplotlibrc。在那个文件中我设置了backend : TkAgg

其他后端

上述配置文件中的 cmets 提到存在另一个基于 WX 的 GUI 后端,与 PyGTK 和 PyQT 一样,截至 2017 年 1 月,似乎无法使用 pip for python 3.6 安装(至少在 Linux 中):

$ pip3.6 install wxpython
Collecting wxpython
  Could not find a version that satisfies the requirement wxpython (from versions: )
No matching distribution found for wxpython

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-12-23
    • 2011-06-23
    • 2011-03-18
    • 2019-04-11
    相关资源
    最近更新 更多