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.

JDong
  • 2,304
  • 3
  • 24
  • 42
  • Work fine for me on Arch. I've got python 3.4.2 and python-cairo 1.10.0-4. – matsjoyce Jan 02 '15 at 23:05
  • Do you have `python-gobject` installed? – nwk Jan 02 '15 at 23:07
  • @nwk yes, `python-gobject` is installed. – JDong Jan 02 '15 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 Jan 02 '15 at 23:47
  • Hmm this is probably a bad question. I'm using cairo since it's the default. The pdf backend seems really nice and works so I'll just sidestep this problem for now. – JDong Jan 03 '15 at 01:08
  • 1
    I got the same error in Python 3.4.1, but changing backend from GTK3Agg to TkAgg now it works fine. – skytux Jan 03 '15 at 04:53
  • Found the way to do that at https://stackoverflow.com/questions/17393928/cannot-import-matplotlib-pyplot-in-python-3 – JDong Jan 03 '15 at 05:25
  • The best solution is [this one](https://stackoverflow.com/a/41279477/3029388) by @Dominykas. – Fardo Feb 14 '21 at 08:04

5 Answers5

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.

Ray
  • 7,833
  • 13
  • 57
  • 91
  • 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). – Marco Massenzio May 16 '16 at 07: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.

Community
  • 1
  • 1
JDong
  • 2,304
  • 3
  • 24
  • 42
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.

Dominykas Mostauskis
  • 7,797
  • 3
  • 48
  • 67
4

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

Aman Agrawal
  • 53
  • 1
  • 1
  • 4
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.

Guillem Cucurull
  • 1,681
  • 1
  • 22
  • 30