Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Change matplotlib backend in Python virtualenv

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?

like image 397
Gabriel Avatar asked Oct 19 '22 03:10

Gabriel


1 Answers

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.

like image 100
edwinksl Avatar answered Oct 21 '22 00:10

edwinksl