7

At present I'm using gnuplot to plot data against a time line. However the precision of the time line is in milliseconds but gnuplot only seems to be able to handle seconds.

I've looked at a couple of alternatives, but really I just need something like gnuplot that can cope with fractions of a second.

The programming language used for the main script is Python and whilst I've looked at matplotlib, it seems to be a lot more 'heavy duty' than gnuplot. As I won't always be the one updating the graphing side of things, I want to keep it as easy as possible.

Any suggestions?

Update

I'm using this with gnuplot:

set xdata time
set timefmt "%Y-%m-%d-%H:%M:%S"

However there is no %f to get milliseconds. For example, this works:

2011-01-01-09:00:01

but I need:

2011-01-01-09:00:01.123456
PeterM
  • 2,534
  • 6
  • 31
  • 38
  • 2
    Could you post a snippet of the data you're trying to plot and the commands you're using? gnuplot shouldn't care at all about the units -- how does it know if they're seconds or milliseconds, after all? -- so I'm not quite sure what it's doing, or not doing, that isn't what you want. – DSM Jan 25 '11 at 05:10
  • The smallest timefmt is a second because a minute has 60 seconds and needs to be handled specially. With milliseconds I don't understand what the problem is? – Tom Jan 26 '11 at 01:50
  • Ah, I understand the problem now, yes, this really is a problem and I don't think there is an elegant solution. – Tom Jan 26 '11 at 13:20
  • Indeed, you will have to create xtickmarks at an appropriate interval width yourself (that is how I get my x tick labels in the example below) – Martin Jan 26 '11 at 21:33
  • Btw, as you have 6 digits, we are talking microseconds, not milliseconds. – Evgeniy Berezovsky Dec 18 '14 at 01:19

4 Answers4

21

According to the gnuplot 4.6 manual it states, under "Time/date specifiers" (page 114 of the gnuplot 4.6 PDF):

%S - second, integer 0–60 on output, (double) on input

What this means is that when reading timestamps such as 2013-09-16 09:56:59.412 the fractional portion will be included as part of the %S specifier. Such a timestamp will be handled correctly with:

set timefmt "%Y-%m-%d %H:%M:%S"
set datafile separator ","
plot "timed_results.data" using 1:2 title 'Results' with lines

and fed with data like:

2013-09-16 09:56:53.405,10.947
2013-09-16 09:56:54.392,10.827
2013-09-16 09:56:55.400,10.589
2013-09-16 09:56:56.394,9.913
2013-09-16 09:56:58.050,11.04
PP.
  • 10,764
  • 7
  • 45
  • 59
  • 1
    This is a very good answer and directly addresses the question at hand - How to have millisecond values recognized by gnuplot ... +ve 1 here. – Xofo Nov 11 '14 at 04:09
  • This works find for reading time data, but not so well for writing it. For instance, `set format x "%H:%M:%S"`, combined with data within the same second results in all the x tics labeled with the same value. E.g, one gets "09:56:53", "09:56:53", "09:56:53" instead of "09:56:53.4", "09:56:53.5", "09:56:53.6". – TrentP Apr 13 '17 at 19:32
  • I have a gnuplot 5.2 and this solution doesnt work for me: I get error `x range is invalid ` with data in such format – Shadasviar Sep 03 '20 at 08:46
12

You can set the ticks format with

set format x '%.6f'

or (maybe, I have not tried it, as I now prefer to use Matplotlib and do not have gnuplot installed on my machines):

set timefmt "%Y-%m-%d-%H:%M:%.6S"

(note the number of digits specified along with the %S format string).

More details can be found in the excellent not so Frequently Asked Questions.

David L.
  • 2,095
  • 23
  • 23
Eric O. Lebigot
  • 91,433
  • 48
  • 218
  • 260
  • 1
    This is not working for me. I copied the data from the original post and issued your `set timefmt "%Y-%m-%d-%H:%M:%.6S`. While this format is accepted, when I run the `plot` command I get `warning: Bad time format in string` on all the data points. – Xander Dunn Jul 12 '12 at 20:29
  • 1
    If I remove the `.6` from the timefmt and then remove the decimal places from my data, it will then work. gnuplot does not actually seem to be able to handle non-integer seconds. – Xander Dunn Jul 13 '12 at 15:26
  • 3
    @thoughtadvances gnuplot 4.6 will interpret `%S` as a `double` meaning that fractional seconds are correctly interpreted. – PP. Sep 16 '13 at 10:35
1

I'm using gnuplot for the same purposes, my input looks like:

35010.59199,100,101
35010.76560,100,110
35011.05703,100,200
35011.08119,100,110
35011.08154,100,200
35011.08158,100,200
35011.08169,100,200
35011.10814,100,200
35011.16955,100,110
35011.16985,100,200
35011.17059,100,200

The first column is seconds since midnight and after the comma a nanosecond part. You can save this in a csv file and in gnuplut do:

set datafile separator ','
plot "test.csv" using 1:3 with lines
Martin
  • 1,084
  • 9
  • 15
1

I originally misunderstood your problem. I think the finer resolution to the time format is a big problem with gnuplot and one that to my knowledge is not implemented.

One possible work-around would be to use awk to convert your date into the number of seconds with something like

plot  "<awk 'your_awk_one_liner' file1.dat" with lines

and then just do a regular double by double plot and forget that it was every time at all (a bit like Martin's solution). I'm afraid I am not very good with awk and so I cannot help with this bit - these pages might help though - http://www.gnu.org/manual/gawk/html_node/Time-Functions.html and http://www.computing.net/answers/unix/script-to-convert-datetime-to-seco/3795.html. The use of awk with gnuplot is described here: http://t16web.lanl.gov/Kawano/gnuplot/datafile3-e.html.

You could then plot a second axis (and not the data) with the correct times - something like the method used here: Is there a way to plot change of day on an hourly timescale on the x axis?

I'm afraid I don't have time to try and write a complete solution - but something reasonable should be possible.

Good luck - keep us updated if you get something working - I would be interested.

Community
  • 1
  • 1
Tom
  • 5,219
  • 2
  • 29
  • 45