newspaint

Documenting Problems That Were Difficult To Find The Answer To

Millisecond Time Resolution X/Y Scatter Plotting With Gnuplot

So you have timestamps with a resolution of milliseconds and you want to plot them on a X/Y scatter graph using Gnuplot.

This is possible because the %S specifier in timefmt accepts a double (i.e. a floating point value) so that the fractional portion of the time will be interpreted as a fraction of seconds.

You can print fractional seconds on the X-axis using the set format x “%H:%M:%.3S” command.

Here is a Gnuplot plot file to demonstrate this:

set terminal png size 400,320 font 'Verdana,8'
set output 'test.png'
set datafile separator ","
set xdata time
set timefmt "%Y-%m-%d %H:%M:%S"
set format x "%H:%M:%.3S"
set title 'Speed over Time'
set xlabel "Timestamp"
set ylabel "km/h"
set xrange ["2013-09-16 10:59:05.000":"2013-09-16 10:59:06.000"]
set yrange [0:12]
set xtics out rotate by -80
set key below
set pointsize 1.0

plot '-' using 1:2 title 'My Car' with points pointtype 2
        2013-09-16 10:59:05.040,5
        2013-09-16 10:59:05.240,7
        2013-09-16 10:59:05.470,10
        2013-09-16 10:59:05.780,3
        e

If you run this you should get the following image created:

Gnuplot 4.6 Millisecond Resolution Scatter Plot

Gnuplot 4.6 Millisecond Resolution Scatter Plot

The documentation for Gnuplot is available at the Gnuplot documentation home page. Note that you might need a recent version of Gnuplot to achieve millisecond-resolution plotting as it appears this was introduced into version 4.6.

4 responses to “Millisecond Time Resolution X/Y Scatter Plotting With Gnuplot

  1. Terry Ledford June 17, 2015 at 6:55 pm

    This is exactly what I am looking for, I thank you for this help.

  2. Artym May 21, 2018 at 3:37 am

    Thank you very much!

  3. Håkan Winbom September 16, 2019 at 12:00 pm

    I have used this with success previously, but now I have a file with timestamps in %s format, with a fractional part. I am trying

    set datafile separator ";"
    set timefmt "%s"
    set xdata time
    plot 'file' u 3:4

    but all values are printed on integer seconds

    is there any limation with “%s” format

    • newspaint September 30, 2019 at 10:25 pm

      This should also work with Unix seconds since the epoch (“%s”) as per the following example:

      set terminal png size 400,320 font 'Verdana,8'
      set output 'test2.png'
      set datafile separator ","
      set xdata time
      set timefmt "%s"
      set format x "%H:%M:%.3S"
      set title 'Speed over Time'
      set xlabel "Timestamp"
      set ylabel "km/h"
      set xrange ["1379329146.000":"1379329147.000"]
      set yrange [0:12]
      set xtics out rotate by -80
      set key below
      set pointsize 1.0
      
      plot '-' using 1:2 title 'My Car 2' with points pointtype 2
              1379329146.040,2
              1379329146.240,3
              1379329146.470,5
              1379329146.780,9
              e

      Double check you are using a version of Gnuplot 4.6 or later.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: