Python matplotlib Cairo error

JDong picture JDong · Jan 2, 2015 · Viewed 15k times · Source

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.

Answer

Ray picture Ray · Dec 3, 2015

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.