adrian5750
Posts: 27
Joined: Mon Mar 06, 2017 10:10 pm

Python 3.5 & matplotlib - won't plot

Wed Mar 28, 2018 9:29 am

HI Folks
You know that feeling when something _used_ to work - and now it doesn't....?

I had a little Python program running quite nicely, reading temperature from a thermocouple and plotting in in real-time using matplotlib (application is monitoring the firing schedule on a glass-fusing kiln, temperature vs time)

The hardware was lashed together (literally) - and at some time something went wrong and the RPi stopped playing.

Long story short - new RPi (3b), new installation of o/s, trying to get back to a point where it all works again.

Running Python 3.5.3 and (apparently) matplotlib-2.2.0
PRETTY_NAME="Raspbian GNU/Linux 9 (stretch)"
NAME="Raspbian GNU/Linux"


My program runs (it produces text printout in the shell), but doesn't generate the desired graph (as it used to before the hiatus).
My notes in the program say that it was tested under Python 3.4.2, which I guess is what was installed when I created the system in the first place.

Error message is as follows :-

Warning (from warnings module):
File "/usr/local/lib/python3.5/dist-packages/matplotlib-2.2.0+617.g216441741-py3.5-linux-armv7l.egg/matplotlib/__init__.py", line 960
warnings.warn(message)
UserWarning: could not find rc file; returning defaults

This is probably significant.......<g> - but I don't know enough to fix the problem.

In desperation <g>, I'd looked at dumping matplotlib and using plotly instead - but (unless I'm reading it wrong) plotly no longer supports real-time web-based graphs on their free online service - so that turned out to be a bit of a dead end.

So - any ideas about where to go from here, please?
I'm reasonably clued up on computers generally, but new-ish to the RPi and not at all familiar with the Rasbian environment.

Thanks
Adrian

B.Goode
Posts: 6434
Joined: Mon Sep 01, 2014 4:03 pm
Location: UK

Re: Python 3.5 & matplotlib - won't plot

Wed Mar 28, 2018 9:55 am

Taking a side-step from your own previously-working code...

There is Raspberry Pi Foundation resource describing simple use of matplotlib here: https://www.raspberrypi.org/learning/vi ... th-python/

Perhaps doing the first example from that would prove whether your python+matplotlib installation is functional? After that you could compare the scripts to see if you can find where your code is now failing.

adrian5750
Posts: 27
Joined: Mon Mar 06, 2017 10:10 pm

Re: Python 3.5 & matplotlib - won't plot

Wed Mar 28, 2018 5:10 pm

HI
Thanks for the suggestion. Unfortunately, running the matlibplot test program

=====
import matplotlib.pyplot as plt

y = [i for i in range(20,100,3)]
x = [i for i in range(len(y))]

plt.scatter(x,y)
plt.show()
=====

Produces exactly the same error message.

====
Warning (from warnings module):
File "/usr/local/lib/python3.5/dist-packages/matplotlib-2.2.0+617.g216441741-py3.5-linux-armv7l.egg/matplotlib/__init__.py", line 960
warnings.warn(message)
UserWarning: could not find rc file; returning defaults
====

So that suggests that there's something wrong with the matlibplot / python setup?

Thanks
Adrian

B.Goode
Posts: 6434
Joined: Mon Sep 01, 2014 4:03 pm
Location: UK

Re: Python 3.5 & matplotlib - won't plot

Wed Mar 28, 2018 6:34 pm

So that suggests that there's something wrong with the matlibplot / python setup?
Yes. And I can't replicate it on a fresh install, so it may be something local to your setup.

Code: Select all

sudo apt install python3-matplotlib
installed v2.0.0, which is apparently behind the version you have?

Running the test routine produced a plot without error.

I regret I don't know enough about python library packaging to be able to make any suggestion about how to fix this.

adrian5750
Posts: 27
Joined: Mon Mar 06, 2017 10:10 pm

Re: Python 3.5 & matplotlib - won't plot

Wed Mar 28, 2018 7:45 pm

Thanks.
The simple test program runs here under python2, but not under python 3.5.3 - where it produces the error message
===
Warning (from warnings module):
File "/usr/local/lib/python3.5/dist-packages/matplotlib-2.2.0+617.g216441741-py3.5-linux-armv7l.egg/matplotlib/__init__.py", line 960
warnings.warn(message)
UserWarning: could not find rc file; returning defaults
===
Not sure why that should be?
Any ideas, anybody?
Thanks
Adrian

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

Re: Python 3.5 & matplotlib - won't plot

Thu Mar 29, 2018 7:10 pm

You are just getting a warning, not an error. Where is your matplotlibrc file? It should be $HOME/.config/matplotlib/matplotlibrc You can check (using python3 interpreter) with

Code: Select all

>>>import matplotlib
>>> matplotlib.matplotlib_fname()
Hopefully this file will have a line
backend : agg
which is the default backend. However I have seen comments that Raspbian needs the backend set to tkagg
Tkinter is the standard python GUI library, already installed in Raspbian, and tkagg will access it.

adrian5750
Posts: 27
Joined: Mon Mar 06, 2017 10:10 pm

Re: Python 3.5 & matplotlib - won't plot

Thu Mar 29, 2018 7:51 pm

HI
Thanks for the reply.

>>> import matplotlib
>>> matplotlib.matplotlib_fname()
'/usr/local/lib/python3.5/dist-packages/matplotlib-2.2.0+617.g216441741-py3.5-linux-armv7l.egg/matplotlib/mpl-data/matplotlibrc'

When I look in that folder, there is no file named matplotlibrc (which I guess is what the original error message was telling me)

Under
/home/pi/.config/matplotlib
there are no files - just an empty folder.

Searching for matplotlibrc - there's three of them in the following folders :-

/home/pi/Adafruit_Python_MAX31855/examples/matplotlib/lib/matplotlib/mpl-data/matplotlibrc
/home/pi/Adafruit_Python_MAX31855/examples/matplotlib/examples/pyplots/matplotlibrc
/home/pi/Adafruit_Python_MAX31855/examples/matplotlib/doc/matplotlibrc

OK - looking depper into this - I grabbed one of the matplotlibrc files, changed the
backend : agg
to
backend : tkagg
and saved it into the
/home/pi/.config/matplotlib folder.

And, lo and behold, the simple matplotlib test program now works under Python 3.5.3.
I guess that's a step in the right direction <g> - thanks

My 'proper' temperature plotting program (that used to work) still doesn't generate a graph.... so there must be something wrong there - but at least I now know that my Python 3.5.3 is capable of driving matplotlib to create a graph.

Now just need to find out what's broken with my program.....<g>

Thanks
Adrian

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

Re: Python 3.5 & matplotlib - won't plot

Thu Mar 29, 2018 8:15 pm

You would have to restart the python interpreter after changing the rc file to pick up those changes.
Your program should certainly work if you include the line

Code: Select all

matplotlib.use('tkagg')
right after the

Code: Select all

import matplotlib
statement

adrian5750
Posts: 27
Joined: Mon Mar 06, 2017 10:10 pm

Re: Python 3.5 & matplotlib - won't plot

Thu Mar 29, 2018 8:24 pm

HI
Thanks for that...

In case anybody else passes this way - it seems that running matplotlib to produce a real-time display requires the addition of

plt.pause(0.0001)
after the
plt.draw()

as described here
https://stackoverflow.com/questions/197 ... -in-a-loop

Adding this to my original temperature-logging program causes it to all burst into life!
Happy bunny!!

Not sure why the original program worked - but happy that I've got it working again....

Thanks!
Adrian

Return to “Python”

Who is online

Users browsing this forum: No registered users and 6 guests