huggies15
Posts: 9
Joined: Tue Feb 19, 2013 11:36 am

Plot output problem

Fri Apr 12, 2013 9:42 am

Hi All,
Im trying to write/run a program that logs temperature and outputs the results to a png image.
I think the program is running fine (i get the desired .log file) up until the grpah part, then i get the following error:
Traceback (most recent call last):
File "temp_record_all_in_one.py", line 81, in <module>
plot(time, temp, color='red', linewidth=2.0, linestyle="-")
File "/usr/lib/pymodules/python2.7/matplotlib/pyplot.py", line 2460, in plot
ax = gca()
File "/usr/lib/pymodules/python2.7/matplotlib/pyplot.py", line 701, in gca
ax = gcf().gca(**kwargs)
File "/usr/lib/pymodules/python2.7/matplotlib/pyplot.py", line 369, in gcf
return figure()
File "/usr/lib/pymodules/python2.7/matplotlib/pyplot.py", line 343, in figure
**kwargs)
File "/usr/lib/pymodules/python2.7/matplotlib/backends/backend_tkagg.py", line 80, in new_figure_manager
window = Tk.Tk()
File "/usr/lib/python2.7/lib-tk/Tkinter.py", line 1712, in __init__
self.tk = _tkinter.create(screenName, baseName, className, interactive, wantobjects, useTk, sync, use)
_tkinter.TclError: no display name and no $DISPLAY environment variable
Im running the program through ssh, putty, windows 7.

the code for the program is:

Code: Select all

#import all neccessary modules
import RPi.GPIO as GPIO
import time
from pylab import *

#state pin setup
LED1_GPIO_PIN = 18
LED2_GPIO_PIN = 25
BUTTON_GPIO_PIN = 17
GPIO.setmode(GPIO.BCM)
GPIO.setup(BUTTON_GPIO_PIN, GPIO.IN)
GPIO.setup(LED1_GPIO_PIN, GPIO.OUT)
GPIO.setup(LED2_GPIO_PIN, GPIO.OUT)

#turn on LED
GPIO.output(LED1_GPIO_PIN, GPIO.HIGH)

#Search for button press
while True:
	if GPIO.input(BUTTON_GPIO_PIN):
		break

while GPIO.input(BUTTON_GPIO_PIN):
	pass

#Turn down led, light up 'processing' led
GPIO.output(LED1_GPIO_PIN, GPIO.LOW)
GPIO.output(LED2_GPIO_PIN, GPIO.HIGH)

#create file with timestamp and write to it
timestamp = time.strftime("%Y-%m-%d--%H-%M-%S")
prog_start = time.time()
filename = "".join(["temperaturedata", timestamp, ".log"])
graphname = "".join(["temperaturedata", timestamp, ".png"])
datafile = open(filename, "w", 1)

#measure every 15 seconds
measurement_wait = 2
button_pressed = False

#CREATE & GATHER DATAFILE
#check for end button press and gather data
while True:
	time_1 = time.time()
	tfile = open("/sys/bus/w1/devices/28-000003ea1c1e/w1_slave")
	text = tfile.read()
	tfile.close()
	temperature_data = text.split()[-1]
	temperature = float(temperature_data[2:])
	temperature = temperature / 1000
	datafile.write(str(time.time()-prog_start) + "\t" + str(temperature) + "\n")
	time_2 = time.time()
	if (time_2 - time_1) < measurement_wait:
		no_of_sleeps = int(round((measurement_wait - (time_2 - time_1)) / 0.1))
		for i in range(no_of_sleeps):
			time.sleep(0.1)
			if GPIO.input(BUTTON_GPIO_PIN):
				button_pressed = True
				break
	if button_pressed:
		break
			
#close and save datafile
datafile.close()

#CREATE GRAPH
#create time and temp arrays
time = {}
temp = {}

#open file created above
with open(filename, 'r') as f:
	lines = f.readlines()

#state what data is from which tab
	for line in lines:
		time = line.split('\t')[0].strip()
		temp = line.split('\t')[1].strip()

#plot line graph of time (x) vs temp (y)
plot(time, temp, color='red', linewidth=2.0, linestyle="-")
title('Time taken to boil 500 ml water', verticalalignment='top', fontsize=12, weight='bold')
xticks
xlabel('Time (s)', fontsize=10)
ylim(0, 110)
yticks
ylabel('Temperature (C)', fontsize=10)

savefig(graphname)

GPIO.output(LED2_GPIO_PIN, GPIO.LOW)
I have installed rpi-gpio, and all the required pylab modules (i think).

I dont want to view the resultant graph through putty, rather save it direct to a png which i will then print/view on my windows PC.

Any help would be amazing.

Cheers,
Steve

huggies15
Posts: 9
Joined: Tue Feb 19, 2013 11:36 am

Re: Plot output problem

Fri Apr 12, 2013 12:25 pm

Quick reply to myself:
fixed this problem by telling matplot lib not to use an X display:
added

Code: Select all

import matplotlib
matplotlib.use('Agg')
before

Code: Select all

from pylab import *
now i get further along the script and incounter another error:

Code: Select all

Traceback (most recent call last):
  File "temp_record_all_in_one.py", line 83, in <module>
    plot(time,temp,color='red',linewidth=2.0,linestyle="-")
  File "/usr/lib/pymodules/python2.7/matplotlib/pyplot.py", line 2467, in plot
    ret = ax.plot(*args, **kwargs)
  File "/usr/lib/pymodules/python2.7/matplotlib/axes.py", line 3893, in plot
    for line in self._get_lines(*args, **kwargs):
  File "/usr/lib/pymodules/python2.7/matplotlib/axes.py", line 322, in _grab_next_args
    for seg in self._plot_args(remaining, kwargs):
  File "/usr/lib/pymodules/python2.7/matplotlib/axes.py", line 281, in _plot_args
    linestyle, marker, color = _process_plot_format(tup[-1])
  File "/usr/lib/pymodules/python2.7/matplotlib/axes.py", line 125, in _process_plot_format
    'Unrecognized character %c in format string' % c)
ValueError: Unrecognized character 5 in format string
Any ideas?

huggies15
Posts: 9
Joined: Tue Feb 19, 2013 11:36 am

Re: Plot output problem

Fri Apr 12, 2013 2:39 pm

ok, so i turned the seconds and degs into floats and now everything works, sort of.
It produces a graph, but there is no data in it, and it i try and print array values (to check they are populating) they are blank... ARRGHHHH

huggies15
Posts: 9
Joined: Tue Feb 19, 2013 11:36 am

Re: Plot output problem

Mon Apr 15, 2013 10:57 am

Amoung loads of minor problems with my code, teh main one was that i wasnt appending data to the array.
Solved now!

zizou0178
Posts: 3
Joined: Tue May 28, 2013 5:38 pm

Re: Plot output problem

Wed May 29, 2013 8:35 pm

Hi,

Can you please post your final code? I am doing a similar project and my graph doesn't have any datapoints on it. I notice you commented on the way that you fixed this. Can you say what changes you made in particular to the code please?

J

Return to “Python”