lyl
lyl

Reputation: 293

Gnuplot: how to prevent xticklabels from override "set xtics"

I have such a data file with three columns(named "0.dat", which has nearly 10000 rows of data, and the following is just for example):

i ii iii
1 a 1
2 b 2
3 c 6
4 d 8
5 e 10
6 f 12
7 g 14
8 h 16
9 i 18
10 j 20

The first and third column are x coordinate and y coordinate that is for plot. The second column(ii) is used for xtics label.

I want xtic and its label to appear on x-axis in an interval of 3, that is, only in the position 1 4 7 on x-axis, should there be xtic mark and xtic label "a d g".

But the following script shows that each poit in my data file creat a xtic, that is to say, "xticklabels" overrides "set xtics".

set term png  size 800,600 
set output "0.png"
set grid
set xrang [1:]
set xtics 3
plot "0.dat" using 1:3:xticlabels(2) axes x1y1 w l
set output
pause 0

how to prevent xticklabels from override "set xtics"?

Upvotes: 2

Views: 811

Answers (1)

Ethan
Ethan

Reputation: 15118

xticlabels always overwrite the auto-generated labels.

However you can include the original label as part of the xticlabel. Here is one option that prints the column 1 content as a number and the column 2 content as a string.

1) Define the format for the label. Then we use that format for every 3rd label, with a blank label for the other 2 slots: 2) Skip the first line of the file, which does not contain data 3) Use the label format for every 3rd line, with blank labels otherwise

set bmargin 3 # leave room for 2 lines of x labels
label(i1,i2) = sprintf("%d\n%s",column(i1),stringcolumn(i2))

plot '0.dat' skip 1 using 1:3:xticlabel(int($0)%3==0 ? label(1,2) : "") with lines

enter image description here

Alternative approach

Use two plots, one for the actual data with no tic marks, one for only 1/3 of the data with tic marks and labels.

set bmargin 3 # leave room for 2 lines of x labels
label(i1,i2) = sprintf("%d\n%s",column(i1),stringcolumn(i2))

set yrange [0:*]  # So that a line at y = -10 will not show

plot '0.dat' skip 1 using 1:3, \
     '0.dat' skip 1 every 3 using 1:(-10):xticlabel(label(1,2)) with lines

enter image description here

Upvotes: 3

Related Questions