2

I'd like to plot hours of the day on the x axis, but print the date when the day rolls over from one day to the next. So the x axis might look like this:

11/02 04:00 08:00 12:00 16:00 20:00 11/03 04:00 08:00 ...

Is there a sane way to do this in gnuplot?

FWIW, my file currently looks something like this:

set xdata time
set timefmt "%Y-%m-%d|%H:%M:%S"
plot '-' using 1:2 with lines linewidth 1 linecolor rgb "#FF0000"
2013-11-02|00:00:48 123.0
2013-11-02|00:00:55 124.0
2013-11-02|00:01:06 121.0
2013-11-02:00:01:17 123.0
...
2013-11-04|23:59:41 241.0
2013-11-04|23:59:52 241.0
fearless_fool
  • 33,645
  • 23
  • 135
  • 217

2 Answers2

3

That is a tough one, made me think a bit. Here is how you can do it:

You need to set the format of the x-axis to %H:%M and then replace the 00:00 with dates, similarly like done in this answer.

The main work is then to extract the timestamps for the mid nights. I use the stats command for this (needs at least version 4.6.0), but because it doesn't support time data you must fiddle around a bit with strptime and similar:

fmt = "%Y-%m-%d|%H:%M:%S"
stats 'file.txt' using (strptime(fmt, stringcolumn(1))) nooutput
t = int(STATS_min)
t_start = t - tm_hour(t)*60*60 - tm_min(t)*60 - tm_sec(t)
num_days = 2 + (int(STATS_max) - t)/(24*60*60)

set xdata time
set timefmt fmt
set xtics 4*60*60
set for [i=1:num_days] xtics add (strftime('%m/%d', t_start+(i-1)*24*60*60) t_start+(i-1)*24*60*60)
set format x '%H:%M'
plot 'file.txt' using 1:2 with lines

I increment num_days by 2 to account for the possible automatic extension of the x-range to the next tics.

The result with your data is (with 4.6.4):

enter image description here

Community
  • 1
  • 1
Christoph
  • 47,569
  • 8
  • 87
  • 187
  • Hey! I asked is there a *sane* way to do this! :) :) In all seriousness, yours is a great and useful solution. It will take me a while to absorb everything that's going on here. Many thanks. – fearless_fool Nov 09 '13 at 23:57
  • P.S.: Bonus appreciation points for actually testing your solution using the data in the original question. I wish more people did that! – fearless_fool Nov 10 '13 at 00:17
  • Also bonus point for you for providing a representative data set :) – Christoph Nov 26 '13 at 09:11
1

Here is a version with xticlabels:

$data <<EOD
2013-11-02|00:00:48 123.0
2013-11-02|00:00:55 124.0
2013-11-02|00:01:06 121.0
2013-11-02|00:01:17 123.0
2013-11-04|23:59:41 241.0
2013-11-04|23:59:52 241.0
EOD

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

plot $data using 1:2 w l # plot to get GPVAL_X_MIN GPVAL_X_MAX

t = GPVAL_X_MIN
t_start = t - tm_hour(t)*60*60 - tm_min(t)*60 - tm_sec(t)
num_days = 1 + (GPVAL_X_MAX-GPVAL_X_MIN)/(24*60*60)

replot [i=0:6*num_days:1] '+' us (t=t_start+i*24*60*60/6, strftime('%Y-%m-%d|%H:%M:%S', t)):(NaN):xtic(strftime(int(i)%6?'%H:%M':'%m-%d',t)) t ""

enter image description here

So the format is selected with int(i)%6?'%H:%M':'%m-%d'.

This was inspired by gnuplot: set link and x2tics in interactive mode.