0

I installed ipython and matplotlib with pip (9.0.1) under python 3.6 (in Xubuntu 16.04), but no images are showing when I try to plot something.

starting ipython with ipython3 --matplotlib qt gives the following error:

ImportError: Matplotlib qt-based backends require an external PyQt4, PyQt5,
or PySide package to be installed, but it was not found.

I tried to install these with pip, but it fails:

$ pip3.6 install PySide
Collecting PySide
  Downloading PySide-1.2.4.tar.gz (9.3MB)
    100% |████████████████████████████████| 9.3MB 12.8MB/s 
    Complete output from command python setup.py egg_info:
    only these python versions are supported: [(2, 6), (2, 7), (3, 2), (3, 3), (3, 4)]

    ----------------------------------------
Command "python setup.py egg_info" failed with error code 1 in /tmp/pip-build-bzzpzy5q/PySide/
$ pip3.6 install PyQT
Collecting PyQT
  Could not find a version that satisfies the requirement PyQT (from versions: )
No matching distribution found for PyQT
$ pip3.6 install PyQT4
Collecting PyQT4
  Could not find a version that satisfies the requirement PyQT4 (from versions: )
No matching distribution found for PyQT4
$ pip3.6 install PyQT5
Collecting PyQT5
  Could not find a version that satisfies the requirement PyQT5 (from versions: )
No matching distribution found for PyQT5

If I try ipython3 --matplotlib gtk, the error is:

ImportError: Gtk* backend requires pygtk to be installed.

But:

$ pip3.6 install pygtk
Collecting pygtk
  Could not find a version that satisfies the requirement pygtk (from versions: )
No matching distribution found for pygtk

I seem to understand that something called PyGobject or PyGI replaced pygtk for python 3. And indeed, ipython3 --matplotlib gtk3 results in:

ImportError: Gtk3 backend requires pygobject to be installed.

But:

$ pip3.6 install pygobject
Collecting pygobject
  Could not find a version that satisfies the requirement pygobject (from versions: )
No matching distribution found for pygobject
$ pip3.6 install PyGObject
Collecting PyGObject
  Could not find a version that satisfies the requirement PyGObject (from versions: )
No matching distribution found for PyGObject

Finally, pip3.6 install PyGI succeeds!

But matplotlib still complains about GTK things not being installed.

What else should I try?

Karl Knechtel
  • 62,466
  • 11
  • 102
  • 153
bli
  • 7,549
  • 7
  • 48
  • 94
  • I tried to put the python3.6 tag, but I don't have enough reputation for this. If someone think it can be useful and has enough reputation, don't hesitate to substitute my 3.x tag by a 3.6 tag. – bli Jan 27 '17 at 17:18
  • 1
    Even without GTK or PyQt matplotlib should work. Try to use `ipython3 --matplotlib tk` for ipython or `import matplotlib` `matplotlib.use("TkAgg")` in a script. For solutions regarding the installation of pyqt or GTK it should strongly matter, which OS you are using, which version of pip you have and so on, so you might want to add this information to the question. – ImportanceOfBeingErnest Jan 27 '17 at 17:29
  • Trying `ipython3 --matplotlib tk` results in `ModuleNotFoundError: No module named '_tkinter'`. I'm recompiling python 3.6 after having installed tk-dev (http://stackoverflow.com/a/5459492/1878788). – bli Jan 27 '17 at 17:36
  • 1
    Just to mention it in case you didn't know: You can always go for a complete distribution, like [anaconda](https://www.continuum.io/downloads#linux), which has everything you need on board. – ImportanceOfBeingErnest Jan 27 '17 at 17:43
  • Recompiling whith tk-dev install enabled me to have the Tk backend work. You can post your suggestion as an answer. It was relevant and useful in my case. Thanks. – bli Jan 27 '17 at 17:45
  • I suggest you post a solution yourself, because I don't know which steps have helped you. Also, the real question on why pip fails in installing either GTK or PyQt is still unclear. – ImportanceOfBeingErnest Jan 27 '17 at 17:52
  • Does this answer your question? ["UserWarning: Matplotlib is currently using agg, which is a non-GUI backend, so cannot show the figure." when plotting figure with pyplot on Pycharm](https://stackoverflow.com/questions/56656777/userwarning-matplotlib-is-currently-using-agg-which-is-a-non-gui-backend-so) – Karl Knechtel Apr 29 '23 at 02:18

1 Answers1

0

Getting the Tk backend to work

As @ImportanceOfBeingErnest suggested, at least one backend should be available: the Tk based one.

This is true provided the Tk development libraries were available when python was compiled. This was not the case for me (I suppose a pre-compiled python distribution should be Tk-enabled).

When I tried to start ipython with Tk as matplotlib backend (ipython3 --matplotlib tk), I had an error message similar to the following one:

import _tkinter # If this fails your Python may not be configured for Tk
ImportError: No module named _tkinter

A comment to the following answer explains how to get the Tk development libraries in Ubuntu: apt install tk-dev.

After doing this and recompiling python 3.6, an ipython3 --matplotlib tk session started without errors and could display graphics.

Setting the default backend choice

The matplotlib documentation gives an example of configuration file, which I downloaded as ~/.config/matplotlib/matplotlibrc. In that file I set backend : TkAgg.

Other backends

The comments in the above-mentioned configuration file mention the existence of yet another GUI backend based on WX, which, like PyGTK and PyQT, doesn't seem to be installable using pip for python 3.6 as of january 2017 (at least in Linux):

$ pip3.6 install wxpython
Collecting wxpython
  Could not find a version that satisfies the requirement wxpython (from versions: )
No matching distribution found for wxpython
Community
  • 1
  • 1
bli
  • 7,549
  • 7
  • 48
  • 94