获取 python 3.6 的 matplotlib 后端

Getting matplotlib backends for python 3.6

我在 python 3.6(在 Xubuntu 16.04 中)下安装了 ipython 和带有 pip (9.0.1) 的 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

我似乎明白,python 3 的 PyGobject 或 PyGI 取代了 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 东西没有安装。

我还应该尝试什么?

让 Tk 后端工作

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

如果在编译 python 时 Tk 开发库可用,这是正确的。我不是这种情况(我想 pre-compiled python 分布应该是 Tk-enabled)。

当我尝试使用 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.

其他后端

above-mentioned 配置文件中的注释提到了另一个基于 WX 的 GUI 后端的存在,它与 PyGTK 和 PyQT 一样,似乎无法使用 pip for python 安装3.6 截至 2017 年 1 月(至少 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