Martin
Martin

Reputation: 381

How to put labels between tics in gnuplot?

I'm trying to have gnuplot put tics but not labels at 0, 1, 2, 3, 4, etc, and then put some text labels but not tics at 0.5, 1.5, 2.5, etc, but can't seem to figure it out. Is that even possible? Any help with that? Thansk!

Upvotes: 3

Views: 4791

Answers (3)

theozh
theozh

Reputation: 25734

Here are 3 variations, slightly different from Christoph's and Endlisnis' solutions.

The OP didn't provide example data, but I assume you have your text labels in a data file and you don't want to specify the labels manually. Hence, you can use the option xticlabels (check help xticlabels).

  • For the first two variations, you either shift the data or the tics.

  • The third variation is certainly what Endlisnis actually meant, but set xtic offset .5, 0 will shift the tic label by half a character unit. Endlisnis certainly meant to use first coordinates, i.e. set xtic offset first 0.5, 0. Note, the datapoint will be at the beginning of the 1st, 2nd, ... ranges, which might be desired or not.

Data: SO21353384.dat

 1   5   1st
 2   8   2nd
 3   1   3rd
 4   6   4th

Script: (works with gnuplot>=4.4.0, March 2010)

### place labels between tics and grid
reset

FILE = "SO21353384.dat"

set yrange [0:10]
set grid y
set key noautotitle

set multiplot layout 3,1

    set title "shift tics"
    set xrange[0.5:4.5]
    set for [i=0:4] xtics (i+0.5 1) scale 0,1
    set grid mx
    plot FILE u 1:2:xtic(3) w lp pt 7

    set title "shift data"
    set xrange [0:4]
    set boxwidth 0.8
    set style fill solid 0.3
    set for [i=0:4] xtics (i 1) scale 0,1
    set grid mx
    plot FILE u ($1-0.5):2:xtic(3) w boxes

    set title "shift tic labels"
    set xrange[1:5]
    set xtics 1 offset first 0.5, 0 scale 1,0
    set grid x
    plot FILE u 1:2:xtic(3) w lp pt 7

unset multiplot
### end of script

Result:

enter image description here

Upvotes: 2

Endlisnis
Endlisnis

Reputation: 519

Look up "offset" here: http://gnuplot.sourceforge.net/docs_4.2/node295.html
You can tell gnuplot to move your tick labels in any direction you want.
You probably want:

set xtics offset .5, 0 ... 

Upvotes: 2

Christoph
Christoph

Reputation: 48390

Yes, that is possible, but not out of the box. Here is how you can achieve that:

  1. Put the major tics where you want to have the labels, i.e. at 0.5, 1.5 etc.: set xtics ("1st" 0.5, "2nd" 1.5, "3rd" 2.5, "4th" 3.5)
  2. Add one minor tic. Usually this is possible with set mxtics 2, but if you have manually defined xtics, then you must add the minor tics also manually: set for [i=0:4] xtics add (i 1)
  3. Scale the major tics to 0 and the minor tics to the size of the major tics: set xtics scale 0,1

So the following minimal script

set xtics ("1st" 0.5, "2nd" 1.5, "3rd" 2.5, "4th" 3.5)
set for [i=0:4] xtics add (i 1)
set xtics scale 0,1
set xrange [0:4]
plot x

gives (with 4.6.3)

enter image description here

Upvotes: 7

Related Questions