4
votes

I am capturing data from a sensor and it is being outputted into a .dat file. I want to export this information into a plot using gnuplot and have it be completely autonomous to only display the last 7 days worth of collected data (no hard coding the dates, should adjust based on system time using the time() function)

If I hard code the date range, i.e. set xrange ["07-25-16":"08-25-16"], I receive no errors and the code/plot runs smoothly. However, if I try to plot a function of time for the past seven days using the time() function, I receive the following error:

line 0: illegal month

Here is my collected data from the sensor (output.dat):

07-21-2015 15:21 0 0 0
07-23-2016 15:21 0 0 0
07-29-2016 15:21 0 1 -1
07-29-2016 15:21 1 1 0
07-29-2016 15:21 2 1 1
07-29-2016 15:21 3 1 2
07-29-2016 15:21 3 2 1
07-29-2016 15:21 4 2 2
07-29-2016 15:21 5 2 3
07-29-2016 15:21 5 3 2
07-29-2016 15:21 5 4 1
07-29-2016 15:21 5 5 0
07-29-2016 15:22 6 5 1
07-29-2016 15:22 6 6 0
07-29-2016 15:23 1 0 1
07-29-2016 15:23 1 1 0

Here is the code I am running (commented out the xrange that works, replaced with the one I am trying to implement):

set xdata time
set timefmt "%m-%d-%Y %H:%M"
xstart=strftime("%m-%d-%Y %H:%M", time(0) - 604800)
xend=strftime("%m-%d-%Y %H:%M", time(0))
#set xrange ["07-25-16":"08-25-16"]
set xrange ["xstart" : "xend"]
set format x "%m-%d\n%H:%M"
set title "Sensor Data"
set xlabel "Date\nTime"
set ylabel "Number of Sensor Trips"
plot    "output.dat" using 1:4 skip 1 t "Exits" with linespoints, \
        "output.dat" using 1:3 skip 1 t "Entrances" with linespoints
set term png
set output "output.png"
replot
set term x11

Any help would be greatly appreciated. Thank you!

Edit 1 -- Gnuplot now not plotting correctly after string fix (removed "" from the xrange to only show: set xrange [xstart:xend]:

wrong image

Any ideas on how to fix?

2
the stftime format include hours and minutes. Did you remove that? - jonas_toth
I did not remove that, as I want to output hours and minutes on the graph as well. Is this the wrong way to think about it? - Mel
"xstart" and "xend" are not date strings. Remove the quotes around those? - bjornruffians
the difference is, that your hardcoded string is only the month(working) and ur generated is not. so maybe sort this out first. xstart and xend using as variables is true as well - jonas_toth
The xstart and xend, without the date strings "", get rid of the error about illegal month BUT the graph is now not working. I will attach above so you can see - Mel

2 Answers

5
votes

If plotting time data, you can specify the axis range using time string formatted the same way like in the data file, but you must not. You can simply use integers for this:

reset
set xdata time
set timefmt "%m-%d-%Y %H:%M"
set xrange [time(0) - 7*24*60*60:]
set format x "%m-%d\n%H:%M"
set style data linespoints

plot "output.dat" using 1:4 skip 1 t "Exits", \
     "" using 1:3 skip 1 t "Entrances"

enter image description here

0
votes

For completeness, if you want to do what you had in your script, that is a range between two given dates, use strptime :

set xrange [strptime("%m-%d-%Y","07-25-16"):strptime("%m-%d-%Y","08-25-16")]