Can I plot times when they're given as UNIX ti

2019-07-22 03:42发布

I'm using gnuplot 4.4 on CentOS 6.6.

I've found many examples online showing that the following (note the use of %.3S) will enable timestamps like "12:42:51.047" to be parsed and used as X axis values:

set xdata time
set format x "%H:%M:%.3S"
set timefmt "%H:%M:%S"

However, my input is not like HH:MM:SS.mmm but like SSSSSSSSSS.mmm, where the integral part is a UNIX timestamp.

I tried the following, but the parsing appeared to have failed since all datapoints rendered at "00:00:00":

set xdata time
set format x "%.3s"
set timefmt "%H:%M:%S"

The manual doesn't give any indication that this is possible but, then again, it doesn't say that the %H:M:%.3S was going to work, either. Is it possible to do what I want to do? If so, how?

3条回答
疯言疯语
2楼-- · 2019-07-22 03:59

Newer versions of gnuplot support Unix timestamp format %s with fractional parts. You will need to update to 4.6 or 5.x or higher.

Check src/time.c if you're not sure.

查看更多
时光不老,我们不散
3楼-- · 2019-07-22 04:00

The precision must be given only for the output. For parsing, the seconds (or the timestamp) are treated as floating numbers. But reading in a time stamp with milliseconds seems to be supported only since version 4.6.4 (tested here).

But, there is a workaround: specifying ($1) instead of 1 in the using statement makes gnuplot treat the value as normal floating point number. Note, that I do not set any timefmt, since it would be circumvented by this workaround anyway.

Then you must also know, that before version 5, gnuplot's time reference is the 1. Jan. 2000, so you must correct by the respective offset (Unix time stamp of the 1. Jan 2000 is 946684800). So, the following works with gnuplot 4.4:

set xdata time
set format x '%d.%m.%Y %H:%M:%S'
plot 'file.dat' using ($1 - 946684800):2
查看更多
聊天终结者
4楼-- · 2019-07-22 04:09

You don't have to do anything at all, except stop fighting the default settings. For this input data:

1000000000.001 1
1000000000.002 2
1000000000.003 3
1000000000.004 3
1000000000.005 2
1000000000.006 1

The following commands

gnuplot> plot "gnuplotdatatest.txt" w l
gnuplot> set yrange [0:4]
gnuplot> replot

produce the following output

enter image description here

Timestamps in SSSSSSSSSS.mmm format are simply (decimal representation of) floating-point numbers, which is how GNUplot treats input columns by default.

查看更多
登录 后发表回答