Search code examples
gnuplot

xtic() does not set label for each bar after I set timefmt for y data


My data looks like this

1; 11.02.20; 6322; 00:12:38
2; 12.02.20; 6184; 00:10:57
3; 13.02.20; 6237; 00:11:17
4; 14.02.20; 5029; 00:08:16

I want to plot a bar graph for each entry in the 4th column and have the entries in the second as labels. But when I set the y-axis to the right time format the labels underneath vanish.

set boxwidth 0.7
set style fill solid
set datafile separator ";"
set timefmt "%H:%M:%S"
set format y "%M:%S" time
set ydata time
set yrange["00:00:00":"00:20:00"]
plot 'woche1.txt' using 4:xtic(2) with boxes

How can I fix this. Also, is it possible to have the values in the third column above each bar?


Solution

  • Which version of gnuplot are you running? With gnuplot 5.2.x, I would do it like this:

    Code:

    ### time data on y-axis, with xtic() and labels
    reset session
    
    $Data <<EOD
    1; 11.02.20; 6322; 00:12:38
    2; 12.02.20; 6184; 00:10:57
    3; 13.02.20; 6237; 00:11:17
    4; 14.02.20; 5029; 00:08:16
    EOD
    
    set boxwidth 0.7
    set style fill solid
    set datafile separator ";"
    myTimeFmt = "%H:%M:%S"
    set format y "%M:%S" time
    set yrange[strptime(myTimeFmt,"00:00:00"):strptime(myTimeFmt,"00:20:00")]
    
    plot $Data u 0:(timecolumn(4,myTimeFmt)):xtic(2) with boxes notitle, \
         '' u 0:(timecolumn(4,myTimeFmt)):3 w labels offset 0,1 not
    
    ### end of code
    

    Result:

    enter image description here

    Addition:

    If you have more than a few boxes to plot, I would not use xtic() as the xlabels if the x-axis is actually a time axis. Simply define it as time axis. Then gnuplot will take care about the labels, i.e. reduce them if there would be too many.

    Code:

    ### time data on x-axis and y-axis
    reset session
    
    # generate some test data
    set print $Data
        today = time(0)
        do for [i=0:14] {
            day = strftime("%d.%m.%y", today + i*86400)    # dates from today on
            number = int(rand(0)*4000)+2000                # random integer between 2000 and 6000
            time = strftime("%H:%M:%S", int(rand(0)*3600)) # random time within 1 hour
            print sprintf("%d; %s; %d; %s",i,day,number,time)
        }
    set print
    
    set boxwidth 86400*0.7   # box width 0.7 of a day
    set style fill solid
    set datafile separator ";"
    myTimeFmtX = "%d.%m.%y"
    set format x "%d.%m." time
    myTimeFmtY = "%H:%M:%S"
    set format y "%tM:%S" time
    set yrange[strptime(myTimeFmtY,"00:00:00"):strptime(myTimeFmtY,"01:00:00")]
    
    plot $Data u (timecolumn(2,myTimeFmtX)):(timecolumn(4,myTimeFmtY)) with boxes notitle, \
         '' u (timecolumn(2,myTimeFmtX)):(timecolumn(4,myTimeFmtY)):3 w labels font ",8" offset 0,1 not
    ### end of code
    

    Result:

    enter image description here