python - 在 Python virtualenv 中更改 matplotlib 后端

标签 python matplotlib plot virtualenv backend

我用pyenv 安装了一个virtualenv使用 Python v2.7.12。在这个 virtualenv 中,我通过以下方式安装了 matplotlib v1.5.1:

pip install matplotlib

没有问题。问题是一个简单的

import matplotlib.pyplot as plt
plt.scatter([], [])
plt.show()

脚本无法生成绘图窗口。我在 virtualenv 中看到的后端使用:

import matplotlib
print matplotlib.rcParams['backend']

agg,这显然是问题的根本原因。如果我在我的系统范围安装中检查后端,我会得到 Qt4Agg(上面的脚本在运行时显示一个绘图窗口就好了)。

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

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

    试图用 --system-site-packages 创建 virtualenv选项。不行。

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

    已安装 sudo apt install tk-dev,然后使用 pip --no-cache-dir install -U --force-reinstall matplotlib 重新安装。后端仍然显示为 agg

  3. Matplotlib doesn't display graph in virtualenv

    按照 this answer 中给出的安装说明进行操作, 什么都不做(另一个答案涉及使用 easy_installI will not do )

  4. matplotlib plot window won't appear

    这里给出的解决方案是“安装一个GUI库(Tkinter、GTK、QT4、PySide、Wx之一)”。我不知道该怎么做。此外,如果我使用:

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

    我得到:

    [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']
    

    意味着所有这些后端在我的系统中可用(?)。

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

    我的 matplotlibrc 文件显示了这一行:

    backend      : Qt4Agg
    

    我不知道如何让 virtualenv 知道这一点?

一些解决方案涉及创建指向系统版本 matplotlib(herehere)的链接,我不想这样做。我想使用 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 的后端需要安装外部 PyQt4、PyQt5 或 PySide 包,但未找到。不确定我是否应该安装一些软件包?

使用:

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 但它说它已经安装。

如何让 virtualenv 使用与我的系统相同的后端?

最佳答案

您可以考虑通过运行以下命令将后端更改为 Python 2 virtualenv 中的 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

关于python - 在 Python virtualenv 中更改 matplotlib 后端,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38981915/

相关文章:

python - 如何将csv转换成dict格式

python - 在 matplotlib 中绘制 (x, y) 坐标列表

r - 如何使用 R 在一个面板中绘制贝叶斯先验分布和后验分布?

MATLAB:数据点无法连接?

python - 从包的 __init__.py 中屏蔽 python 子模块

python - 截断 python 生成器

python - 将 Tornado 应用程序部署到 AWS Lambda 时出错

matplotlib - 在 matplotlib 中注释数据范围

python - Matplotlib:每个时间序列子图绘制多行

python - Seaborn:在直方图上叠加箱线图或均值误差条