5

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.

  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?

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?

Community
  • 1
  • 1
Gabriel
  • 32,750
  • 58
  • 187
  • 337
  • Have you tried the obvious workaround of setting a backend manually? [Like this](http://stackoverflow.com/a/20249447/5067311). – Andras Deak Aug 16 '16 at 22:52
  • @AndrasDeak please see updated answer. – Gabriel Aug 16 '16 at 23:37
  • Well, that's pretty odd, since [at least tkinter should be installed, probably](http://stackoverflow.com/a/20075485/5067311). Unfortunately I don't know anything about virtualenvs, so I can't help beyond this point. – Andras Deak Aug 16 '16 at 23:50
  • Tkintir *is* installed. But the virtualenv somehow does not see it. Thanks anyway. – Gabriel Aug 16 '16 at 23:51
  • Yeah, that I understand. But python should be installed within the virtualenv itself, so I'd expect at least tkinter to work within the virtualenv:) Even if for some reason it fails to see all the other modules installed with apt. – Andras Deak Aug 17 '16 at 00:04
  • 2
    @Gabriel I am the author of the answer to the second question you posted. I just tried my own answer for Python 2 and I can confirm it doesn't work as you said (it does work for Python 3 though). Let me get back to you if I can figure out what is happening. – edwinksl Aug 17 '16 at 04:31
  • 2
    @Gabriel I think I figured it out and I posted an answer that worked for me. Let me know if it works for you. – edwinksl Aug 17 '16 at 04:50

1 Answers1

3

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.

edwinksl
  • 6,569
  • 3
  • 28
  • 34
  • I need some help in doing this in a conda environment. I want to change to 'GTKAgg' backend, and in my conda env pygtk is installed, but I still got the `Gtk* backend requires pygtk to be installed` error. Re-installing matplotlib doesn't help. – Jason May 24 '17 at 13:57
  • @Jason You might want to ask a new question since this answer may not apply to conda environments. – edwinksl May 24 '17 at 23:15