rpsml
rpsml

Reputation: 1498

Time / date handling in gnuplot 5

I have the following data set

0   22/01   5:40.416    07/01   5:59.149
1   29/01   5:49.765    14/01   5:59.036
2   05/02   5:45.675    21/01   5:52.160
3   12/02   5:44.241    28/01   5:55.160
4   19/02   5:49.330    04/02   5:44.065
5   26/02   5:48.044    11/02   5:34.803

and I want to plot column 3 as a function of 2 (and also 5 as a function of 4 but that is not the problem). The format are day/month and minutes:seconds.xxx.

In gnuplot 4.6 it was somewhat straightforward to indicate how to read both columns by setting distinct time formats for x and y. A minimal script that worked is:

set xdata time
set timefmt x "%d/%m"
set format x "%b"

set ydata time
set timefmt y "%M:%S"
set format y "%M:%S"

plot 'data.dat' using 2:3

I recently upgraded to gnuplot 5 and the command set timefmt x "%d/%m" (and its y equivalent) now give an error. It seems that you cannot put x or y anymore, and you can only use a single timefmt for the whole script.

The release notes state that

Use of time formats to generate axis tick labels is now controlled by set {xy}tics time rather than by set {xy}data time. Thus prior calls to set xdata time or set timefmt x are unnecessary for either input or output. These older commands still work, but are deprecated.

I have two issues. First, despite the indication that "older commands still work", I get an error on set timefmt x "%d/%m". Second, and most important, I couldn't figure out how to teach gnuplot how to read my x and y columns using the new commands.

Note: I have seen this question from 2011 but I don't find it elegant and I want to keep the files with the present format.

Upvotes: 4

Views: 2114

Answers (1)

e0k
e0k

Reputation: 7161

The improvement they made with this new version actually makes it more flexible. Rather than specify a default time format and then require your columns to fit that format, you can specify a format for each particular time column. This way you don't have to be consistent, as long as you specify each column's format. You can also use a time format for more than an x or y axis.

The solution to your question is mentioned in the release notes you quoted, but it doesn't explain it well. Use the timecolumn(N,"timeformat") function, like the following:

set xtics time
set format x "%b"

set ytics time
set format y "%M:%S"

plot 'data.dat' using (timecolumn(2, "%d/%m")):(timecolumn(3, "%M:%S"))

The set timefmt lines have been replaced by this new two-argument timefunction(), which allows you to specify the format for each column in the using clause. Notice the () around the timecolumn(). This means that you could (if you wanted) do some math with the time value. According to the same 5.0 release notes:

Time coordinates are stored internally as the number of seconds relative to the standard unix epoch 1-Jan-1970. Earlier versions of gnuplot used a different epoch internally (1-Jan-2000). This change resolves inconsistencies introduced when time in seconds was generated externally. The epoch convention used by a particular gnuplot installation can be determined using the command print strftime("%F",0). Time is now stored to at least millisecond precision.

Notice the change to set xtics time. The output format for the xtics is the same as in your example, and is different from the input format given by the second argument to timecolumn(). You seem to have more flexibility than before, but the syntax has changed.

Upvotes: 4

Related Questions