20

I'm using something simpler than the sample code on the pyplot tutorial website:

import matplotlib.pyplot as plt
plt.plot([1,2,3,4,5])
plt.show()

but when I run it, I get the error:

TypeError: Couldn't find foreign struct converter for 'cairo.Context'

I'm on Python 3.4.2, and have a fresh install of matplotlib, cairocfft>0.6, numpy>=1.6 from pip.

I tried uninstalling cairocfft and grabbing python-cairo from the Arch repositories, but now I have the error:

NotImplementedError: Surface.create_for_data: Not Implemented yet.

Is there a way to draw a basic line graph without installing many libraries? I'm not enthusiastic on installing pyqt4, as this blogpost recommends. This github issue suggests installing gi-cairo, but gi-cairo is not on the Arch repositories, nor could I find it on PyPI (my own search fail?)

I remember this being a breeze on Python2, but have migrated to Python3 now.

8
  • Work fine for me on Arch. I've got python 3.4.2 and python-cairo 1.10.0-4.
    – matsjoyce
    Commented Jan 2, 2015 at 23:05
  • Do you have python-gobject installed?
    – nwk
    Commented Jan 2, 2015 at 23:07
  • @nwk yes, python-gobject is installed.
    – JDong
    Commented Jan 2, 2015 at 23:18
  • If you want a gui interface, then you need a gui framework. Mpl supports qt, gtk, wx and tk (plus OSX, windows?, ipython notebooks/web browsers). As for drawing, any reason you need to use cairo? The Agg backends work fine if you want raster and the svg/ps/pdf backends should have most of the bases covered for vector graphics.
    – tacaswell
    Commented Jan 2, 2015 at 23:47
  • 1
    I got the same error in Python 3.4.1, but changing backend from GTK3Agg to TkAgg now it works fine.
    – skytux
    Commented Jan 3, 2015 at 4:53

5 Answers 5

34

This is in case someone is having the same problem on Ubuntu 14.04, as I did using Python 3.4.3. By using bits and hints from JDong's answer, I've solved the problem as follows. (Basically change the MatPlotLib backend to qt5agg.)

  1. Install python3-pyqt5. sudo apt-get install python3-pyqt5

  2. Find out where the matplotlibrc file is so you can edit it. This can be done using the following in Python console. import matplotlib matplotlib.matplotlib_fname()

  3. Edit the matplotlibrc file (you'll probably require sudo), find the line beginning with backend :, and change it to backend : qt5agg. If such a line doesn't exist, just create one.

The above steps have solved it for me on Ubuntu 14.04. I hope that helps.

1
  • Confirmed to work in Ubuntu 14.04 with Python 3.4 - FWIW the file appear to be installed in /usr/local/lib/python3.4/dist-packages/matplotlib/mpl-data/matplotlibrc and that should be the same for everyone (although the method suggested by @Ray works just fine). Commented May 16, 2016 at 7:22
6

This does seem to be a real issue; upon further playing I found an error message:

/usr/lib/python3.4/site-packages/matplotlib/backends/backend_gtk3agg.py:18:
UserWarning: The Gtk3Agg backend is known to not work on Python 3.x with pycairo.
Try installing cairocffi.
"The Gtk3Agg backend is known to not work on Python 3.x with pycairo."

Oddly however, I did install cairocffi. The best solution I found is to use a different backend, as tcaswell suggested in the comments. I found export to pdf very agreeable with just the line: plt.savefig("Graph.pdf").

skytux mentions that changing the backend to Tk works. https://stackoverflow.com/a/21791045/2534876 shows how to do this.

Mar 2016 update: plotly is a great alternative to matplotlib that I use now. It works with the browser to generate documents and I haven't had platform issues.

4

Upon installation Matplotlib warned me to install cairocffi, because of incompatibilities in my system. After experiencing the NotImplementedError (plots not being drawn), installing Debian's python3-cairocffi package solved the problem.

2
  • 1
    This should be voted up. Much better solution than changing the backend to Qt. Commented Mar 13, 2017 at 23:36
  • Actually, this one is the best solution.
    – Fardo
    Commented Feb 14, 2021 at 8:00
4

Installing python-gi-cairo using sudo apt-get install python-gi-cairo solved my problem

1
  • 2
    In my case (Debian 9.2), the accepted answer didn't help but this one did!
    – phinz
    Commented Nov 25, 2017 at 16:59
3

In the case of Python 2.7 it can be solved by installing the package python-pyqt5 and using pyplot like this:

import matplotlib
matplotlib.use('Qt5Agg')
import matplotlib.pyplot as plt

If you have sudo acces you can also set Qt5Agg as your default backend, see Matplotlib Backend for other ways to set your backend.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.