Plot two files in gnuplot with different timefmt

585 views Asked by At

I need to plot two data file in the same graph, with gnuplot. The first data file is like:

2015-02-10 10.1
2015-02-15 12.1
2015-02-20 16.1
2015-02-25 14.1
...

and the second data file is like

2015-02-10-10:00 12.5
2015-02-10-15:00 21.4
2015-02-15-12:30 08.3
2015-02-15-22:00 09.3
2015-02-20-08:15 12.8
2015-02-20-17:32 16.7
2015-02-25-07:20 14.0
2015-02-25-21:39 14.5
..

To parse the first data file, timefmt should be set to "%Y-%m-%d" and for the second file it would be "%Y-%m-%d-%H:%M". However, since both data files should be ploted together, how do I define timefmt properly?

1

There are 1 answers

0
Christoph On BEST ANSWER

With verrsion 4.6 and earlier you must parse the datetime strings 'manually' with strptime:

set xdata time
plot 'file1.dat' using (strptime('%Y.%m.%d', strcol(1))):2,\
     'file2.dat' using (strptime('%Y.%m.%d-%H:%M', strcol(1))):2

With gnuplot version 5 you can directly give a time format to the timecolumn function, so that you can use as much formats as you want:

set xdata time
plot 'file1.dat' using (timecolumn(1, '%Y.%m.%.d')):2,\
     'file2.dat' using (timecolumn(1, '%Y.%m.%d-%H:%M')):2

In your case the difference between the two variants is marginal, but when the date time spreads over more than one column in the data file, then the latter variant is much more comfortable, since timecolumn handles the multiple columns automatically.