在Python虚拟环境中更改matplotlib后端

4

我使用Python v2.7.12安装了一个带有pyenvvirtualenv。在这个虚拟环境中,我通过以下方式安装了matplotlib v1.5.1:

pip install matplotlib

没有问题。问题在于一个简单的
import matplotlib.pyplot as plt
plt.scatter([], [])
plt.show()

脚本无法生成绘图窗口。我在虚拟环境中看到的后端是:

import matplotlib
print matplotlib.rcParams['backend']

问题的根源似乎是agg。如果我在我的系统范围内安装的后端中检查,我会得到Qt4Agg(当运行上面的脚本时,窗口可以正常显示绘图)。

在SO中已经有几个类似的问题,我已经尝试了所有给出的解决方案。

  1. Matplotlib plt.show() isn't showing graph

    Tried to create the virtualenv with the --system-site-packages option. No go.

  2. How to ensure matplotlib in a Python 3 virtualenv uses the TkAgg backend?

    Installed sudo apt install tk-dev, then re-installed using pip --no-cache-dir install -U --force-reinstall matplotlib. The backend still shows as agg.

  3. Matplotlib doesn't display graph in virtualenv

    Followed install instructions given in this answer, did nothing (the other answer involves using easy_install, which I will not do)

  4. matplotlib plot window won't appear

    The solution given here is to "install a GUI library (one of Tkinter, GTK, QT4, PySide, Wx)". I don't know how to do this. Furthermore, if I use:

    import matplotlib.rcsetup as rcsetup
    print(rcsetup.all_backends)
    

    I get:

    [u'GTK', u'GTKAgg', u'GTKCairo', u'MacOSX', u'Qt4Agg', u'Qt5Agg', u'TkAgg', u'WX', u'WXAgg', u'CocoaAgg', u'GTK3Cairo', u'GTK3Agg', u'WebAgg', u'nbAgg', u'agg', u'cairo', u'emf', u'gdk', u'pdf', u'pgf', u'ps', u'svg', u'template']
    

    meaning that all those backends are available in my system (?).

  5. matplotlib does not show my drawings although I call pyplot.show()

    My matplotlibrc file shows the line:

    backend      : Qt4Agg
    

    I don't know how to make the virtualenv aware of this?

其中一些解决方案涉及创建到系统版本的matplotlib的链接 (这里这里),但我不想这样做。我想使用安装在virtualenv中的matplotlib版本。

如果我尝试使用以下方式设置后端:

import matplotlib
matplotlib.use('GTKAgg')

我遇到了一个问题:ImportError: Gtk* backend requires pygtk to be installed (同样适用于GTK)。但是,如果我运行sudo apt-get install python-gtk2 python-gtk2-dev,我会发现它们都已经安装好了。

使用的环境:

import matplotlib
matplotlib.use('Qt4Agg')

(或者Qt5Agg)导致ImportError: Matplotlib qt-based backends require an external PyQt4, PyQt5, or PySide package to be installed, but it was not found.不确定是否需要安装一些包?

使用:

import matplotlib
matplotlib.use('TkAgg')

出现ImportError: No module named _tkinter错误,但是sudo apt-get install python-tk命令显示已经安装。

使用的环境为:

import matplotlib
matplotlib.use('GTKCairo')

导致ImportError: No module named gtk的结果。所以我尝试sudo apt-get install libgtk-3-dev,但它说已经安装了。

我该如何使虚拟环境使用与我的系统相同的后端?


@AndrasDeak 请查看更新的答案。 - Gabriel
这很奇怪,因为至少应该安装tkinter(可能会有其他依赖项)。不幸的是,我对虚拟环境一无所知,因此在此之后无法提供更多帮助。 - Andras Deak -- Слава Україні
Tkinter 已经安装。但是虚拟环境似乎无法识别到它。感谢您的帮助。 - Gabriel
2
@Gabriel,你好。我是你发布的第二个问题答案的作者。我刚刚尝试了一下我的Python 2答案,我可以确认它不像你说的那样有效(但对于Python 3确实有效)。如果我能弄清楚发生了什么事情,我会再联系你的。 - edwinksl
2
@Gabriel 我想我找到了解决方法,并且我发布了一个对我有效的答案。如果它对你有用,请告诉我。 - edwinksl
显示剩余3条评论
1个回答

3

您可以尝试运行以下命令,在Python 2的虚拟环境中将后端更改为TkAgg:

sudo apt install python-tk  # install Python 2 bindings for Tk
pip --no-cache-dir install -U --force-reinstall matplotlib  # reinstall matplotlib   

为了确认后端确实是TkAgg,运行以下命令

python -c 'import matplotlib as mpl; print(mpl.get_backend())'

您应该看到TkAgg


我需要在conda环境中做这件事的帮助。我想要改变到“GTKAgg”后端,在我的conda环境中已经安装了pygtk,但我仍然得到了“Gtk* backend requires pygtk to be installed”错误。重新安装matplotlib也没有帮助。 - Jason
@Jason,你可能需要提出一个新问题,因为这个答案可能不适用于conda环境。 - edwinksl

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接