12

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?

Seanny123
  • 8,776
  • 13
  • 68
  • 124
  • 2
    How did you install matplotlib? `_backend_gdk` is a c-python extension that needs to be compiled. – tacaswell Jan 16 '13 at 05:23
  • can yoo compile it seperately from matplotlib if matplotlib is already compiled? I build matplotlib from source via the terminal. –  Jan 17 '13 at 13:43
  • probably, but getting it linked properly will require replicating the deep magic that setup.py does. – tacaswell Jan 17 '13 at 14:10
  • Ok thanks. I'll try rebuilding it from source to check wheater this fixes my problem. –  Jan 17 '13 at 15:27
  • 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 Feb 01 '13 at 14:10

5 Answers5

13

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.

Andy Hayden
  • 359,921
  • 101
  • 625
  • 535
  • This answer should be accepted. +1 for clarity, correctness and conciseness. –  Feb 16 '14 at 05:37
  • 7
    This answer is irrelevant with the problem at hand – Davide Feb 05 '15 at 18:05
  • 2
    Yes, this answer doesn't help. I have installed standard python-matplotlib packages, and I can't plot using my usual GTK backends, even though the _backend_gdk file is there in my system. – jdpipe Aug 22 '17 at 11:47
5

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.

Haldean Brown
  • 12,411
  • 5
  • 43
  • 58
  • 1
    This is just annoying, why pip doesn't work? I think having to install the package from apt prevents us to use any version other than the one supported but the distro. Still I found you answer the only working solution. – 2LayerNN300HU Mar 18 '17 at 22:35
  • On Ubuntu 18.04, this does not solve the problem. I still get the same error message. There is a bug for this here: https://bugs.launchpad.net/ubuntu/+source/matplotlib/+bug/1785458 – spirit Sep 14 '18 at 07:09
3

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
T. Reffet
  • 41
  • 3
1

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.

Forrest Voight
  • 2,249
  • 16
  • 21
0

if you are running py2.7 applications, try:

pip2 install matplotlib --no-binary=matplotlib
nairoby
  • 55
  • 4