raspidiot
Posts: 8
Joined: Mon Dec 04, 2017 2:37 pm

[SOLVED] Python3 and Matplotlib - no luck

Mon Dec 04, 2017 2:54 pm

Dear all,

I ma new to Raspberry and i successfully set up a model B with Raspbian 4 weeks ago (downloaded raspbian and put it on an SD card) The System boots up fine and i can use it - So far so good.

Now i try to start a python3 program that i developed on an Ubuntu PC. I was glad to see that Python3 is already installed. But no luck with Matplotlib and pyplot. I was annoyed that al was set up so fine and here i already tried a lot, googling for a lot of single issues: Installing Pip3, installing Matplotlib (ok is normal) come over the Memory error when doing so, installing "cairo" without knowing what to do.

So in the meantime i think i did something basic wrong. I would ask you to test, if this simple demo from https://matplotlib.org/users/pyplot_tutorial.html works for you? And if yes - what did i make wrong?

Code: Select all

import matplotlib.pyplot as plt
plt.plot([1,2,3,4])
plt.ylabel('some numbers')
plt.show()
Error: Could'n find foreign struct converter for 'cairo.Context'
Last edited by raspidiot on Mon Dec 11, 2017 7:27 am, edited 1 time in total.

robbes
Posts: 136
Joined: Sun Jan 20, 2013 7:11 pm
Location: Canada - off the west coast

Re: Python3 and Matplotlib - no luck

Mon Dec 04, 2017 7:47 pm

Is your matplotlib installation configured to use the cairo backend? This will be stated in your matplotlibrc file.
I think the location is normally in ~/.config/matplotlib/matplotlibrc. If not you can find it with

Code: Select all

>>> import matplotlib
>>> matplotlib.matplotlib_fname()
Look for a line that looks like (in my case)

Code: Select all

backend      : Qt5Agg
Yours should contain some reference to cairo.

robbes
Posts: 136
Joined: Sun Jan 20, 2013 7:11 pm
Location: Canada - off the west coast

Re: Python3 and Matplotlib - no luck

Tue Dec 05, 2017 7:02 am

I decided to see if I had problems using the cairo backend of matplotlib on python3 in jessie; it seems OK after two packages are installed:

Code: Select all

sudo apt install python3-cairo 

Code: Select all

sudo apt install libcairo2
There are a few references on stackexchange referring to a need for

Code: Select all

sudo apt install python3-gi
but I didn't find that to be necessary. Maybe I had installed it previously, or maybe it is not necessary.

Code: Select all

import matplotlib.pyplot as plt

Code: Select all

import matplotlib

Code: Select all

matplotlib.rcParams['backend'] = 'cairo'
#over-ride my .matplotlibrc

Code: Select all

matplotlib.get_backend()
#just confirming that cairo is being used

Code: Select all

plt.plot([1, 2, 3], [2, 4, 6])

Code: Select all

plt.show()
gives me a display..
If you are still having trouble, I suggest you try the default backend.

Code: Select all

matplotlib.rcdefaults()
will restore all the standard settings.

raspidiot
Posts: 8
Joined: Mon Dec 04, 2017 2:37 pm

Re: Python3 and Matplotlib - no luck

Tue Dec 05, 2017 8:02 am

robbes wrote:
Mon Dec 04, 2017 7:47 pm
Is your matplotlib installation configured to use the cairo backend? This will be stated in your matplotlibrc file.
I think the location is normally in ~/.config/matplotlib/matplotlibrc. If not you can find it with

Code: Select all

>>> import matplotlib
>>> matplotlib.matplotlib_fname()
Look for a line that looks like (in my case)

Code: Select all

backend      : Qt5Agg
Yours should contain some reference to cairo.
I was not aware that i have to configure anything before using matplotlib. I was also not keen to use cairo (what ever it is) - i was leaded there by the error messages :-) Anyway - i will try and report.

raspidiot
Posts: 8
Joined: Mon Dec 04, 2017 2:37 pm

Re: Python3 and Matplotlib - no luck

Tue Dec 05, 2017 8:04 am

robbes wrote:
Tue Dec 05, 2017 7:02 am
...
If you are still having trouble, I suggest you try the default backend.

Code: Select all

matplotlib.rcdefaults()
will restore all the standard settings.
Ok i will try also this after the tests from the post before. Thanks!

raspidiot
Posts: 8
Joined: Mon Dec 04, 2017 2:37 pm

Re: Python3 and Matplotlib - no luck

Tue Dec 05, 2017 9:38 am

robbes wrote:
Mon Dec 04, 2017 7:47 pm
Is your matplotlib installation configured to use the cairo backend?
ok i did what you recommanded and it shows up that my config is in

/usr/local/lib/python3.5/dist-packages/matplotlib/mpl-data/matplotlibrc

the line is

backend : gtk3agg

so seems not fitting. Will try now the next advice..

raspidiot
Posts: 8
Joined: Mon Dec 04, 2017 2:37 pm

Re: Python3 and Matplotlib - no luck

Tue Dec 05, 2017 9:52 am

robbes wrote:
Tue Dec 05, 2017 7:02 am
I decided to see if I had problems using the cairo backend of matplotlib on python3 in jessie; it seems OK after two packages are installed:

Code: Select all

sudo apt install python3-cairo 

Code: Select all

sudo apt install libcairo2
There are a few references on stackexchange referring to a need for

Code: Select all

sudo apt install python3-gi
but I didn't find that to be necessary. Maybe I had installed it previously, or maybe it is not necessary.
Ok i installed the first, 2nd and 3rd have already be installed.

Then i did what you did - i get a white figure1 and this errors:

Code: Select all

python3
Python 3.5.3 (default, Jan 19 2017, 14:11:04) 
[GCC 6.3.0 20170124] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import matplotlib.pyplot as plt

** (.:3473): WARNING **: Error retrieving accessibility bus address: org.freedesktop.DBus.Error.ServiceUnknown: The name org.a11y.Bus was not provided by any .service files
>>> import matplotlib
>>> matplotlib.rcParams['backend'] = 'cairo'
>>> matplotlib.get_backend()
'cairo'
>>> plt.plot([1, 2, 3], [2, 4, 6])
[<matplotlib.lines.Line2D object at 0xb22332d0>]
>>> plt.show()
TypeError: Couldn't find foreign struct converter for 'cairo.Context'
TypeError: Couldn't find foreign struct converter for 'cairo.Context'
now testing with setting back to defaults..

raspidiot
Posts: 8
Joined: Mon Dec 04, 2017 2:37 pm

Re: Python3 and Matplotlib - no luck

Tue Dec 05, 2017 9:56 am

robbes wrote:
Tue Dec 05, 2017 7:02 am
...
If you are still having trouble, I suggest you try the default backend.

Code: Select all

matplotlib.rcdefaults()
will restore all the standard settings.
still no luck:

Code: Select all

>>> matplotlib.rcdefaults()
>>> matplotlib.get_backend()
'agg'
>>> plt.plot([1, 2, 3], [2, 4, 6])
[<matplotlib.lines.Line2D object at 0xb2259ed0>]
>>> plt.show()
TypeError: Couldn't find foreign struct converter for 'cairo.Context'
TypeError: Couldn't find foreign struct converter for 'cairo.Context'

robbes
Posts: 136
Joined: Sun Jan 20, 2013 7:11 pm
Location: Canada - off the west coast

Re: Python3 and Matplotlib - no luck

Tue Dec 05, 2017 6:20 pm

I am out of ideas. I assume you are using python3.5 and matplotlib v. 2.0.2
In your original post you mentioned installing pip3 and also a memory error. You shouldn't have had to install pip3.
Possibly try reinstalling matplotlib and the cairo dependencies.
Try all the backends and see if any of them work: apparently you can use the instruction form

Code: Select all

matplotlib.use('agg')
rather than the matplotlib.rcParams['backend'] ='agg' style I used. However, the .use() function must be placed before the import matplotlib.pyplot statement.
You may find some listings of backends contain capitals and some don't. The name specifications aren't case-sensitive, so it doesn't matter.

raspidiot
Posts: 8
Joined: Mon Dec 04, 2017 2:37 pm

Re: Python3 and Matplotlib - no luck

Wed Dec 06, 2017 9:08 am

Thanks anyway! maybe i tried a version that was "too new" and i should go back to the jessie version of raspbian.

For tools like python3 and matplotlib, that are very frequently used, i would not expect that i have to install something else than matplotlib itself - should i?

raspidiot
Posts: 8
Joined: Mon Dec 04, 2017 2:37 pm

Re: Python3 and Matplotlib - no luck

Wed Dec 06, 2017 12:00 pm

Just to let you know - i succeeded this way (and very quick, as expected):

I downloaded an older image (jessie) + installed matplotlib from the right package: sudo apt-get install python3-matplotlib

now it works fine.

robbes
Posts: 136
Joined: Sun Jan 20, 2013 7:11 pm
Location: Canada - off the west coast

Re: Python3 and Matplotlib - no luck

Wed Dec 06, 2017 5:52 pm

That's good.
I upgraded to the November release of Stretch today, and matplotlib is working with all the backends I tried; I don't think you should associate the problem you had with the OS version. It was likely to have been a package installation issue.

Anyway, try to edit the Subject Title to include [SOLVED] .

powell_CUT
Posts: 1
Joined: Sat Jun 23, 2018 8:15 am

Re: Python3 and Matplotlib - no luck

Sat Jun 23, 2018 8:22 am

raspidiot wrote:
Tue Dec 05, 2017 9:52 am
robbes wrote:
Tue Dec 05, 2017 7:02 am
I decided to see if I had problems using the cairo backend of matplotlib on python3 in jessie; it seems OK after two packages are installed:

Code: Select all

sudo apt install python3-cairo 

Code: Select all

sudo apt install libcairo2
There are a few references on stackexchange referring to a need for

Code: Select all

sudo apt install python3-gi
but I didn't find that to be necessary. Maybe I had installed it previously, or maybe it is not necessary.
Ok i installed the first, 2nd and 3rd have already be installed.

Then i did what you did - i get a white figure1 and this errors:

Code: Select all

python3
Python 3.5.3 (default, Jan 19 2017, 14:11:04) 
[GCC 6.3.0 20170124] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import matplotlib.pyplot as plt

** (.:3473): WARNING **: Error retrieving accessibility bus address: org.freedesktop.DBus.Error.ServiceUnknown: The name org.a11y.Bus was not provided by any .service files
>>> import matplotlib
>>> matplotlib.rcParams['backend'] = 'cairo'
>>> matplotlib.get_backend()
'cairo'
>>> plt.plot([1, 2, 3], [2, 4, 6])
[<matplotlib.lines.Line2D object at 0xb22332d0>]
>>> plt.show()
TypeError: Couldn't find foreign struct converter for 'cairo.Context'
TypeError: Couldn't find foreign struct converter for 'cairo.Context'
now testing with setting back to defaults..
one also need to Try this: install libffi6 and libffi-dev using apt:

sudo apt install libffi-dev libffi6

sudo pip3 install cffi

sudo pip3 install cairocffi

thats how i solved my problems

Return to “Python”