I've installed a virtualenv
with pyenv using Python v2.7.12. Inside this virtualenv, I installed matplotlib
v1.5.1 via:
pip install matplotlib
with no issues. The problem is that a simple
import matplotlib.pyplot as plt
plt.scatter([], [])
plt.show()
script fails to produce a plot window. The backend that I see in the virtualenv using:
import matplotlib
print matplotlib.rcParams['backend']
is agg
, which is apparently the root cause of the issue. If I check the backend in my system-wide installation, I get Qt4Agg
(and the above script when run shows a plot window just fine).
There are already several similar questions in SO, and I've tried the solutions given in all of them.
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 using pip --no-cache-dir install -U --force-reinstall matplotlib
. The backend still shows as agg
.
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?
Some of the solutions involve creating links to the system version of matplotlib
(here and here), which I don't want to do. I want to use the version of matplotlib
installed in the virtualenv
.
If I try to set the backend with:
import matplotlib
matplotlib.use('GTKAgg')
I get ImportError: Gtk* backend requires pygtk to be installed
(same with GTK
). But if I do sudo apt-get install python-gtk2 python-gtk2-dev
, I see that they are both installed.
Using:
import matplotlib
matplotlib.use('Qt4Agg')
(or Qt5Agg
) results in ImportError: Matplotlib qt-based backends require an external PyQt4, PyQt5, or PySide package to be installed, but it was not found.
Not sure if I should install some package?
Using:
import matplotlib
matplotlib.use('TkAgg')
results in ImportError: No module named _tkinter
, but sudo apt-get install python-tk
says that it is installed.
Using:
import matplotlib
matplotlib.use('GTKCairo')
results in ImportError: No module named gtk
. So I try sudo apt-get install libgtk-3-dev
but it says that it already installed.
How can I make the virtualenv use the same backend that my system is using?
You can consider changing your backend to TkAgg
in the Python 2 virtualenv by running the following:
sudo apt install python-tk # install Python 2 bindings for Tk
pip --no-cache-dir install -U --force-reinstall matplotlib # reinstall matplotlib
To confirm the backend is indeed TkAgg
, run
python -c 'import matplotlib as mpl; print(mpl.get_backend())'
and you should see TkAgg
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With