Search code examples
gnuplot

Wrong position of lables on x2-axis labels in gnuplot plots


I want to plot the following data(which is stored in "0-time.txt")

time_,value

23:59:58,1

23:59:59,2

00:00:00,3

00:00:01,4

00:00:02,5

00:00:03,6

00:00:04,7

00:00:05,8

00:00:06,9

00:00:07,10

00:00:08,11

00:00:09,12

by the following gnuplot script:

set term png  size 800,600 

set output "0-time.png"

set datafile separator comma

set grid

set x2tics 6

set mx2tics 3

set link x2

set autoscale xfix

set autoscale x2fix

plot "0-time.txt" skip 1 every 2 using ($0*2):(0):xticlabels(int

($0*2)%6==0 ? stringcolumn(1):''), "" using ($0):2 axes x1y1 title columnheader(2)

set output

The output plot is alright except that labels on x2-axis is wrong. Please see the attached figure. How to correct it?enter image description here


Solution

  • In Gnuplot, the data index $0 starts with zero, which is why you see 0 as the first x2 tic. But you can change the link in this way

    set link x2 via x+1 inverse x-1
    

    and then the x2 axis will be shifted by one unit with respect to x. Then you just change which x2 tics to display,

    set x2tics 1, 6
    

    which will hide 0, 6, 12, etc and show 1, 7, 13, etc instead.