How to display tics only for the values in the dataset?

Viewed 84

I have a standard dataset, say:

1,10
2,15
4,20
8,5

Is there any simple way to display tics only for the values in the dataset?

I can manually set them:

set xtics ( 1, 2, 4, 8 );
set ytics ( 5, 10, 15, 20 );

however, this has two side effects:

  1. I have to manually find all the values;
  2. for some reason, it removes the margines (compared to autoscaling), so I have to compute them manually (set offsets graph works strangely with tics manually set).

Is there any simple way of display the same plot it would be plotted without the set *tics command, with the only difference being the tics printed?

I'm on Gnuplot 5.2.

2 Answers

You can call xtic() and ytic() from the plot command. Eg:

set datafile separator comma
set offsets graph 0.1, graph 0.1, graph 0.1, graph 0.1
plot '-' using 1:2:xtic(1):ytic(2)  pt 7  lc black  title ""
1,10
2,15
4,20
8,5
end

enter image description here

Try the following. You can create strings from your dataset and use them with a macro (see help macros). Maybe there are simpler ways to achieve the same result (I guess there might be also a way to set the x-axis as a log2 axis, then you should get your 1,2,4,8,16,32 tics automatically.)

Code:

### setting special tics semi-automatically
reset session

$Data <<EOD
1, 10
2, 15
4, 20
8,  5
EOD

set datafile separator comma

set table $Dummy
    plot myXtics=myYtics='' $Data using \
        (myXtics = myXtics.($0==0?'':',').strcol(1), \
         myYtics = myYtics.($0==0?'':',').strcol(2)) w table
unset table

set offsets 1,1,1,1
set xtics (@myXtics)
set ytics (@myYtics)
set grid xtics, ytics

plot $Data u 1:2 w p pt 7 notitle
### end of code

Result:

enter image description here

Addition:

If you use logarithmic x-axis to base 2 you will get your tics automatically, note: 1,2,4,8 are equidistant. However, set offsets seems to work only on linear axes (a bug?). See How to get (automatic) offsets in logscale plots?.

Code:

### setting special tics semi-automatically
reset session

$Data <<EOD
1, 10
2, 15
4, 20
8,  5
EOD

set datafile separator comma

set logscale x 2

set offsets 1,1,1,1  # offsets works on linear y-axis but not on log x axis
set xrange [0.75:12]
set grid xtics, ytics

plot $Data u 1:2 w p pt 7 notitle
### end of code

Result:

enter image description here

Related