12
votes

I am starting to get some insight into interactive plotting with python and matplotlib using pyGTK+. Therefore I took a look at the example given at the matplotlib website.

This is a short exerpt of the Code:

#!/usr/bin/env python
"""
Example of embedding matplotlib in an application and interacting with
a treeview to store data.  Double click on an entry to update plot
data

"""
import pygtk
pygtk.require('2.0')
import gtk
from gtk import gdk

import matplotlib
matplotlib.use('GTKAgg')  # or 'GTK'
from matplotlib.backends.backend_gtk import FigureCanvasGTK as FigureCanvas

from numpy.random import random
from matplotlib.figure import Figure

Ones I try to run this Script in the Terminal I get the following error:

Traceback (most recent call last):
  File "gtk_spreadsheet.py", line 15, in <module>
    from matplotlib.backends.backend_gtk import FigureCanvasGTK as FigureCanvas
  File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/matplotlib/backends/backend_gtk.py", line 33, in <module>
    from matplotlib.backends.backend_gdk import RendererGDK, FigureCanvasGDK
  File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/matplotlib/backends/backend_gdk.py", line 29, in <module>
    from matplotlib.backends._backend_gdk import pixbuf_get_pixels_array
ImportError: No module named _backend_gdk

I have python 2.7 and pygtk 2.24 installed.

Can anyone figure out where the error is located? I think it might be connected to some linking issues?

4
How did you install matplotlib? _backend_gdk is a c-python extension that needs to be compiled. - tacaswell
can yoo compile it seperately from matplotlib if matplotlib is already compiled? I build matplotlib from source via the terminal. - user1371788
probably, but getting it linked properly will require replicating the deep magic that setup.py does. - tacaswell
Ok thanks. I'll try rebuilding it from source to check wheater this fixes my problem. - user1371788
Do cp setup.cfg.template setup.cfg and set gtk = True and gtkagg = True. But you might need some extra header files (I installed pygtk2-devel on linux). - schlamar

4 Answers

13
votes

Note that the Debian/Ubuntu package you need is not 'pygtk2-devel' but 'python-gtk2-dev':

sudo apt-get install python-gtk2-dev

should fix this error on these platforms.

5
votes

This was a symptom of using a pip-installed matplotlib instead of an apt-installed matplotlib on my system, just now. If on Ubuntu/Debian, try:

pip uninstall matplotlib
apt install python-matplotlib

I believe what was happening is that the pip-install didn't build the C extension required for GTK output, but the apt package has the extension prebuilt. I don't have logs from the initial pip install of matplotlib, so I can't confirm that that's what happened.

3
votes

In addition to Haldean Brown's answer, note that if you really need to use pip you can force it to recompile matplotlib locally and get "the deep magic that setup.py does" with the --no-binary option:

pip uninstall matplotlib
pip install matplotlib --no-binary=matplotlib

This will solve your problem, provided that you already installed gtk2 with sudo apt-get install python-gtk2-dev

As you want to use the GTKAgg backend, Using pip may prove useful in the future to freeze matplotlib at a version where it is supported (the deprecation warning states it will be dropped in 3.0):

pip install matplotlib==2.2.2 --no-binary=matplotlib
1
votes

Ubuntu 18.04 has a broken matplotlib package. See this bug: https://bugs.launchpad.net/ubuntu/+source/matplotlib/+bug/1785458

You can either compile matplotlib yourself (e.g. with pip) or use the PPA linked from the bug report.