在 Python virtualenv 中更改 matplotlib 后端
Change matplotlib backend in Python virtualenv
我使用 Python v2.7.12 安装了 virtualenv
和 pyenv。在这个 virtualenv 中,我通过以下方式安装了 matplotlib
v1.5.1:
pip install matplotlib
没有问题。问题是一个简单的
import matplotlib.pyplot as plt
plt.scatter([], [])
plt.show()
脚本无法生成情节 window。我在 virtualenv 中看到的后端使用:
import matplotlib
print matplotlib.rcParams['backend']
是 agg
,这显然是问题的根本原因。如果我在系统范围的安装中检查后端,我会得到 Qt4Agg
(当 运行 显示情节 window 时上面的脚本就好了)。
SO里已经有好几个类似的问题了,我都试过了。
Matplotlib plt.show() isn't showing graph
尝试使用 --system-site-packages 选项创建 virtualenv。不行
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
.
Matplotlib doesn't display graph in virtualenv
按照 this answer, did nothing (the other answer involves using easy_install
, which I will not do)
中给出的安装说明进行操作
matplotlib plot window won't appear
这里给出的解决方案是"install a GUI library (one of 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']
意味着所有这些后端 在我的系统中 可用 (?)。
matplotlib does not show my drawings although I call pyplot.show()
我的 matplotlibrc
文件显示行:
backend : Qt4Agg
我不知道如何让 virtualenv 知道这个?
一些解决方案涉及创建指向 matplotlib
(here and here) 系统版本的链接,我不想这样做。我想用matplotlib
的版本安装在virtualenv
。
如果我尝试设置后端:
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
但它说它已经安装了。
如何让 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 v2.7.12 安装了 virtualenv
和 pyenv。在这个 virtualenv 中,我通过以下方式安装了 matplotlib
v1.5.1:
pip install matplotlib
没有问题。问题是一个简单的
import matplotlib.pyplot as plt
plt.scatter([], [])
plt.show()
脚本无法生成情节 window。我在 virtualenv 中看到的后端使用:
import matplotlib
print matplotlib.rcParams['backend']
是 agg
,这显然是问题的根本原因。如果我在系统范围的安装中检查后端,我会得到 Qt4Agg
(当 运行 显示情节 window 时上面的脚本就好了)。
SO里已经有好几个类似的问题了,我都试过了。
Matplotlib plt.show() isn't showing graph
尝试使用 --system-site-packages 选项创建 virtualenv。不行
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
.Matplotlib doesn't display graph in virtualenv
按照 this answer, did nothing (the other answer involves using
easy_install
, which I will not do) 中给出的安装说明进行操作
matplotlib plot window won't appear
这里给出的解决方案是"install a GUI library (one of 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']
意味着所有这些后端 在我的系统中 可用 (?)。
matplotlib does not show my drawings although I call pyplot.show()
我的
matplotlibrc
文件显示行:backend : Qt4Agg
我不知道如何让 virtualenv 知道这个?
一些解决方案涉及创建指向 matplotlib
(here and here) 系统版本的链接,我不想这样做。我想用matplotlib
的版本安装在virtualenv
。
如果我尝试设置后端:
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
但它说它已经安装了。
如何让 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
.