Antonio Dell'Elce
Antonio Dell'Elce

Reputation: 383

Plotting multiple date/time formats in gnuplot

Consider the following sample input file:

yyyymmdd HHMM HHMM
20150501 0800 0140
20150502 0900 0021
20150503 1000 0021
20150504 0830 
20150505 0945
20150506 1145 0340

The first column is year/month/day, second & third is hours & minute

I need single plot using gnuplot that shows all these columns, the main problem that I have is that "set timefmt" is unique while I would need to support two different formats... any suggestion?

I am trying to use strptime in a function but getting a bit lost with gnuplot syntax.

To clarify this is an example of what I am doing:

reset

# Input:
# yyyymmdd HHMM HHMM
# 20150505 0800 0020

# Y  
set ydata    time
set format y "%H%M"
set timefmt  "%H%M"
set ylabel   "thisisy"

# 
gettime2(tcol) = strptime('%H%M',strcol(tcol))

set grid

plot '-' using 1:(gettime2(2)) with lines title 'A' #, '' using 0:3 title 'B'   with lines
20150501 0800 0140
20150502 0900 0021
20150503 1000 0021
20150504 0830 
20150505 0945
20150506 1145 0340

What I get as label in the Y-axis is something like '2.01505x10e7', I would like to have original date as is or at least as a date :)

Upvotes: 1

Views: 611

Answers (1)

Christoph
Christoph

Reputation: 48390

Here is another try: What I do is to use the first column as date on the x-axis, and the second and third columns as times on the y-axis:

reset

set ydata    time
set format y "%H:%M"

set xdata time
set format x "%d.%m.%Y"
set xtics 86400

gettime(tcol) = strptime('%H%M', strcol(tcol))
getdate(dcol) = strptime('%Y%m%d', strcol(dcol))

set style data linespoints
plot '-' using (getdate(1)):(gettime(2)) title "A",\
     '' using (getdate(1)):(valid(3) ? gettime(3) : 1/0) title "B"
20150501 0800 0140
20150502 0900 0021
20150503 1000 0021
20150504 0830 
20150505 0945
20150506 1145 0340
e
20150501 0800 0140
20150502 0900 0021
20150503 1000 0021
20150504 0830 
20150505 0945
20150506 1145 0340
e

With the valid function one can check if a given column contains a valid number. That is necessary in order to prevent errors in the strptime function if the third column is empty.

As you see, the invalid entries cause the lines to be interrupted. If you don't want this, you would need to provide default values for those entries, which you then use instead of the 1/0.

enter image description here

Upvotes: 1

Related Questions