JDong
JDong

Reputation: 2304

Python matplotlib Cairo error

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.

Upvotes: 20

Views: 18407

Answers (5)

Aman Agrawal
Aman Agrawal

Reputation: 53

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

Upvotes: 4

Dominykas Mostauskis
Dominykas Mostauskis

Reputation: 8135

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.

Upvotes: 4

Guillem Cucurull
Guillem Cucurull

Reputation: 1691

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.

Upvotes: 3

JDong
JDong

Reputation: 2304

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.

Upvotes: 6

Ray
Ray

Reputation: 8593

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.

Upvotes: 34

Related Questions