我使用Python v2.7.12安装了一个带有pyenv的virtualenv
。在这个虚拟环境中,我通过以下方式安装了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中已经有几个类似的问题,我已经尝试了所有给出的解决方案。
Matplotlib plt.show() isn't showing graph
Tried to create the virtualenv with the --system-site-packages option. No go.
How to ensure matplotlib in a Python 3 virtualenv uses the TkAgg backend?
Installed
sudo apt install tk-dev
, then re-installed usingpip --no-cache-dir install -U --force-reinstall matplotlib
. The backend still shows asagg
.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)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 (?).
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
,但它说已经安装了。
我该如何使虚拟环境使用与我的系统相同的后端?