1

I followed the instructions to install matplotlib for python 3 from here: How to install matplotlib with Python3.2

But I get an ImportError, which I have not been able to resolve, when importing pyplot:

>>> import matplotlib
>>> import matplotlib.pyplot as plt
Traceback (most recent call last):
 File "<stdin>", line 1, in <module>
 File "/usr/local/lib/python3.2/dist-packages/matplotlib-1.4.x-py3.2-linux-i686.egg/matplotlib/pyplot.py", line 98, in <module>
_backend_mod, new_figure_manager, draw_if_interactive, _show = pylab_setup()
 File "/usr/local/lib/python3.2/dist-packages/matplotlib-1.4.x-py3.2-linux-i686.egg/matplotlib/backends/__init__.py", line 25, in pylab_setup
globals(),locals(),[backend_name])
 File "/usr/local/lib/python3.2/dist-packages/matplotlib-1.4.x-py3.2-linux-i686.egg/matplotlib/backends/backend_gtk3agg.py", line 1, in <module>
   import cairo
ImportError: No module named cairo 

I have tried matplotlib.use('Agg') and matplotlib.use('GTK') before import pyplot, but they don't make a difference.

Any help would be much appreciated, thanks.

Edit:
This shows that I have all the required dependencies.

jack@hostname:~/installs/matplotlib$ sudo python3 setup.py --keywords
============================================================================
Edit setup.cfg to change the build options

BUILDING MATPLOTLIB
        matplotlib: yes [1.4.x]
            python: yes [3.2.3 (default, Apr 10 2013, 05:29:11)  [GCC
                    4.6.3]]
          platform: yes [linux2]

REQUIRED DEPENDENCIES AND EXTENSIONS
             numpy: yes [version 1.6.1]
          dateutil: yes [using dateutil version 2.1]
           tornado: yes [using tornado version 3.1]
         pyparsing: yes [using pyparsing version 2.0.0]
             pycxx: yes [Official versions of PyCXX are not compatible
                    with Python 3.x.  Using local copy]
            libagg: yes [pkg-config information for 'libagg' could not
                    be found. Using local copy.]
          freetype: yes [version 14.0.8]
               png: yes [version 1.2.46]

OPTIONAL SUBPACKAGES
       sample_data: yes [installing]
          toolkits: yes [installing]
             tests: yes [using nose version 1.3.0]

OPTIONAL BACKEND EXTENSIONS
            macosx: no  [Mac OS-X only]
            qt4agg: no  [PyQt4 not found]
           gtk3agg: yes [gtk3agg backend does not work on Python 3]
         gtk3cairo: no  [Requires cairo to be installed.]
            gtkagg: no  [Requires pygtk]
             tkagg: yes [version version not identified]
             wxagg: no  [requires wxPython]
               gtk: no  [Requires pygtk]
               agg: yes [installing]
             cairo: no  [cairo not found]
         windowing: no  [Microsoft Windows only]

OPTIONAL LATEX DEPENDENCIES
            dvipng: yes [version 1.14]
       ghostscript: yes [version 9.05]
             latex: yes [version 3.1415926]
           pdftops: yes [version 0.18.4]
Community
  • 1
  • 1
qAp
  • 1,139
  • 2
  • 12
  • 26
  • 1
    How did you install matplotlib? With a package manager such as apt-get, or from setup.py? – Codie CodeMonkey Jun 30 '13 at 19:16
  • 1
    You probably haven't installed [pycairo](https://pypi.python.org/pypi/pycairo) – mata Jun 30 '13 at 19:20
  • "...they don't make a difference" because you still get *an* `ImportError` or *this particular* `ImportError`? – Brian Cain Jun 30 '13 at 19:41
  • @KenHill I cloned from Github, then did 'sudo python3 setup.py build', then 'sudo python3 setup.py install' – qAp Jun 30 '13 at 20:34
  • @mata I installed python-cairo, then rebuilt and reinstalled Is that the same? – qAp Jun 30 '13 at 20:37
  • @BrianCain It's always this particular ImportError with 'cairo'. – qAp Jun 30 '13 at 20:38
  • @jackyu - you mean you compiled pycairo yourself, or did you install the package `python-cairo` using your distro's package system? The latter probably won't work, you also need to compile it yourself. – mata Jun 30 '13 at 20:44
  • @mata How do I make pycairo visible to matplotlib installation? I have cloned pycairo from github (git clone git://git.cairographics.org/git/pycairo) and installed it via 'python3 setup.py install', but matplotlib still doesn't see it (please see Edit above). – qAp Jul 01 '13 at 16:12
  • and pycairo installed without error (can you import cairo in python)? check that it was installed to a directory on `sys.path`, and check the access permissions, installing stuff using sudo sometimes messes these up, maybe you need to chmod it to 0755... – mata Jul 01 '13 at 17:41
  • 'import cairo' throws no errors, but confusingly 'print(cairo)' points to '__init__.py' inside a directory created by the installation of 'python3-cairo'. The installation of 'pycairo' doesn't seem to create a 'pycairo' directory. – qAp Jul 02 '13 at 17:29

1 Answers1

1

Problem Cause In mac os image rendering back end of matplotlib (what-is-a-backend to render using the API of Cocoa by default). There is Qt4Agg and GTKAgg and as a back-end is not the default. Set the back end of macosx that is differ compare with other windows or linux os.

I resolve this issue following ways I assume you have installed the pip matplotlib, there is a directory in you root called ~ /. matplotlib. Create a file called matplotlibrc there and following code

~ / .matplotlib / Matplotlibrc
backend: TkAgg
J4cK
  • 30,459
  • 8
  • 42
  • 54