User avatar
fred7
Posts: 17
Joined: Fri May 02, 2014 6:17 am
Location: Estonia, Europe
Contact: Website

Easiest way to generate graph from temperature data

Wed Jun 24, 2020 2:27 pm

Hello!
Does anyone know what is the easiest method to generate graph from temperature log file?
The file I have (temperature.txt):
24/06/20 03:05 21.62
24/06/20 03:06 21.63
24/06/20 03:07 22.39
....

I have tried using Gnuplot and Rrdtool. But these fail. I am using only console through SSH. I would like just to generate basic XY-graph and save it to file (graph.jpg).

JovianPyx
Posts: 132
Joined: Fri Nov 20, 2015 9:34 pm

Re: Easiest way to generate graph from temperature data

Wed Jun 24, 2020 2:42 pm

I use gnuplot to generate a png file from the data which is then available to apache2. Since the webpage auto-refreshes, the graph is "live" given some latency.

Alternatively, I have used KO18-U translation on a puTTY terminal which has two half block characters that I use as huge pixels. I wrote a C program that reads the temperature and displays it as a live updating coarse graph. That is good enough to display the temperature (in C) in 1/2 degree increments on a terminal with about 100 lines. This method displays in the terminal in which it is run and needs no webserver. The downside is the translation makes other things look weird unless you change it back to UTF-8 or whatever your default is. I color the "pixels" green except when above 70C they are yellow and red when 80C or more (which I fortunately never see).

gordon77
Posts: 5276
Joined: Sun Aug 05, 2012 3:12 pm

Re: Easiest way to generate graph from temperature data

Wed Jun 24, 2020 4:03 pm

try this... (based on https://learn.sparkfun.com/tutorials/gr ... -real-time )

data in file called temp_data.txt

Code: Select all

import datetime as dt
import matplotlib.pyplot as plt
import matplotlib.animation as animation

# Create figure for plotting
fig = plt.figure()
ax = fig.add_subplot(1, 1, 1)
xs = []
ys = []

graph_data = open('temp_data.txt','r').read()
lines = graph_data.split('\n')
xs = []
ys = []
for line in lines:
    if len(line) > 1:
        d, t, y = line.split(' ')
        xs.append(d + " " + t)
        ys.append(float(y))

# This function is called periodically from FuncAnimation
def animate(i, xs, ys):

    # Limit x and y lists to 20 items
    xs = xs[-20:]
    ys = ys[-20:]

    # Draw x and y lists
    ax.clear()
    ax.plot(xs, ys)

    # Format plot
    plt.xticks(rotation=45, ha='right')
    plt.subplots_adjust(bottom=0.30)
    plt.title('Temperature over Time')
    plt.ylabel('Temperature (deg C)')

# Set up plot to call animate() function periodically
ani = animation.FuncAnimation(fig, animate, fargs=(xs, ys), interval=1000)
plt.show()

User avatar
neilgl
Posts: 2546
Joined: Sun Jan 26, 2014 8:36 pm
Location: Near Aston Martin factory

Re: Easiest way to generate graph from temperature data

Wed Jun 24, 2020 4:34 pm

That is odd, I used rrdtool to plot graphs of temperature for many years, all OK. What is failing when you use rrdtool?

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

Re: Easiest way to generate graph from temperature data

Wed Jun 24, 2020 5:29 pm

neilgl wrote:
Wed Jun 24, 2020 4:34 pm
That is odd, I used rrdtool to plot graphs of temperature for many years, all OK. What is failing when you use rrdtool?


Discussed in a parallel universe, here - viewtopic.php?f=91&t=278239&p=1684888#p1684888

User avatar
bensimmo
Posts: 4988
Joined: Sun Dec 28, 2014 3:02 pm
Location: East Yorkshire

Re: Easiest way to generate graph from temperature data

Wed Jun 24, 2020 7:56 pm

stick it in excel/sheet/spreadsheet on the pi.
select all the data, hit graph ?

EDIT: I 'll leave it up, but just reread and you're using console. my bad.

User avatar
scruss
Posts: 3500
Joined: Sat Jun 09, 2012 12:25 pm
Location: Toronto, ON
Contact: Website

Re: Easiest way to generate graph from temperature data

Wed Jun 24, 2020 9:55 pm

fred7 wrote:
Wed Jun 24, 2020 2:27 pm
I have tried using Gnuplot and Rrdtool. But these fail.
How is gnuplot failing? Something like this should work, or at least produce some output:

Code: Select all

set terminal jpeg size 720,405
set output "graph.jpg"
set xdata time
set timefmt "%y/%m/%d %H:%M"
plot "temperature.txt" using 1:3
‘Remember the Golden Rule of Selling: “Do not resort to violence.”’ — McGlashan.
Pronouns: he/him

User avatar
fred7
Posts: 17
Joined: Fri May 02, 2014 6:17 am
Location: Estonia, Europe
Contact: Website

Re: Easiest way to generate graph from temperature data

Wed Jun 24, 2020 10:08 pm

Update.
I tried using matplotlib and numpy. Again no luck.
Used this python script, and launched it using command ("sudo python numpy.py")

import numpy as np
import matplotlib.pyplot as plt
data = np.loadtxt('data.txt')


x = data[:, 2]
y = data[:, 3]
plt.plot(x, y,'r--')
plt.savefig('graph.png')
Error: ImportError: No module named matplotlib.pyplot

User avatar
neilgl
Posts: 2546
Joined: Sun Jan 26, 2014 8:36 pm
Location: Near Aston Martin factory

Re: Easiest way to generate graph from temperature data

Wed Jun 24, 2020 11:20 pm

What is failing when you use rrdtool?

User avatar
scruss
Posts: 3500
Joined: Sat Jun 09, 2012 12:25 pm
Location: Toronto, ON
Contact: Website

Re: Easiest way to generate graph from temperature data

Wed Jun 24, 2020 11:23 pm

Code: Select all

sudo apt install python3-matplotlib python3-numpy
then do

Code: Select all

python3 numpy.py
(no sudo required - you don't need sudo)
But maybe don't call it numpy.py as then Python might get confused with the numpy package.
Last edited by scruss on Thu Jun 25, 2020 1:26 pm, edited 1 time in total.
‘Remember the Golden Rule of Selling: “Do not resort to violence.”’ — McGlashan.
Pronouns: he/him

User avatar
bensimmo
Posts: 4988
Joined: Sun Dec 28, 2014 3:02 pm
Location: East Yorkshire

Re: Easiest way to generate graph from temperature data

Thu Jun 25, 2020 7:26 am

you would need to use python3 to run Python3 code under RPiOS

User avatar
scruss
Posts: 3500
Joined: Sat Jun 09, 2012 12:25 pm
Location: Toronto, ON
Contact: Website

Re: Easiest way to generate graph from temperature data

Thu Jun 25, 2020 1:26 pm

yep, typo on my part
‘Remember the Golden Rule of Selling: “Do not resort to violence.”’ — McGlashan.
Pronouns: he/him

jbudd
Posts: 1500
Joined: Mon Dec 16, 2013 10:23 am

Re: Easiest way to generate graph from temperature data

Thu Jun 25, 2020 5:03 pm

Here is a Node-Red flow to:
- Read your data from /home/pi/data.txt,
- Convert the date and time to a javascript date object,
- Display a graph as a .png file on a web page at <Pi ip address>:1880/ui
You don't need a web server on the Pi, it's built in to Node-Red
The flow uses node-red-dashboard and node-red-contrib-moment which you have to install via hamburger menu | Manage palette.

Code: Select all

[{"id":"6eca75c8.edf524","type":"ui_chart","z":"764a32e2.a1ee5c","name":"","group":"490e9b00.e9d99c","order":1,"width":0,"height":0,"label":"Temperatures","chartType":"line","legend":"false","xformat":"auto","interpolate":"linear","nodata":"","dot":false,"ymin":"","ymax":"","removeOlder":1,"removeOlderPoints":"","removeOlderUnit":"3600","cutout":0,"useOneColor":false,"colors":["#1f77b4","#aec7e8","#ff7f0e","#2ca02c","#98df8a","#d62728","#ff9896","#9467bd","#c5b0d5"],"useOldStyle":false,"outputs":1,"x":760,"y":180,"wires":[[]]},{"id":"490e9b00.e9d99c","type":"ui_group","z":"","name":"Original","tab":"5b74fd34.5b4414","disp":true,"width":"7","collapse":false},{"id":"5b74fd34.5b4414","type":"ui_tab","z":"","name":"Foo","icon":"dashboard","order":4,"disabled":false,"hidden":false}]
Untitled 1.jpg
Untitled 1.jpg (34.04 KiB) Viewed 314 times
With just the three data points you shared, it displays this graph.
index.jpg
index.jpg (18.13 KiB) Viewed 314 times

User avatar
scruss
Posts: 3500
Joined: Sat Jun 09, 2012 12:25 pm
Location: Toronto, ON
Contact: Website

Re: Easiest way to generate graph from temperature data

Thu Jun 25, 2020 6:26 pm

OP is using console through SSH, so no hamburgers there.
‘Remember the Golden Rule of Selling: “Do not resort to violence.”’ — McGlashan.
Pronouns: he/him

jbudd
Posts: 1500
Joined: Mon Dec 16, 2013 10:23 am

Re: Easiest way to generate graph from temperature data

Thu Jun 25, 2020 6:44 pm

scruss wrote:
Thu Jun 25, 2020 6:26 pm
OP is using console through SSH, so no hamburgers there.
Yes, I saw that. My suggestion requires another computer with browser on the same network to access the Pi. I use a laptop.

Return to “Beginners”