eng27
eng27

Reputation: 916

How to tweak automatic-tics-number in gnuplot?

It is helpful that gnuplot automatically picks up reasonably good x/y-range, and x/y-tics number (say approx 5 as attached figure).

However, I sometimes would like to increase/decrease the number of tics.

Of course, changing number of tics is easy. The thing I would like to do is, to take the advantage of this gnuplot "automatic tics selecting", but tweaking the approx number of tics. Is there any way to deal with this?

Thank you!

Upvotes: 2

Views: 1482

Answers (2)

theozh
theozh

Reputation: 25704

OK, now I see your point. Especially in multiplots or when the graphs are getting small relative to the size of the tic labels there are too many tics.

Find below a workaround which seems to work in the demonstrated cases. If it works nicely all the time you need to test. The disadvantage is that you have to plot first to a dummy table in order to get the range which gnuplot puts into the GPVAL_... variables and then replot again.

In the graph below the first row is gnuplot auto-tic, the second row the attempt for semi-auto-approximate-tic. Maybe it is a starting point for further tweaking.

Script:

### set approximate number of tics with "nice" intervals
reset session

round(n) = gprintf("%.0e",n)
# alternatively with less approximate tics: 
# round(n) = gprintf("%.0e",n) + sgn(n)*10**gprintf("%T",n)

approxTicsX(n) = round((GPVAL_X_MAX - GPVAL_X_MIN)/n)
approxTicsY(n) = round((GPVAL_Y_MAX - GPVAL_Y_MIN)/n)

set multiplot layout 2,3 rowsfirst

    ### with gnuplot auto-tics
    set xrange [-10:10]
    plot x

    set xrange [-100:100]
    plot x**2

    set xrange [-90:90]
    plot x

    ### now with semi-auto tics
    set style line 1 lc rgb "red"
    set xrange [-10:10]
    set table $Dummy
        plot x ls 1
    unset table
    set xtics approxTicsX(5)
    set ytics approxTicsY(5)
    replot

    set xrange [-100:100]
    set table $Dummy
        plot x**2 ls 1
    unset table
    set xtics approxTicsX(5)
    set ytics approxTicsY(5)
    replot

    set xrange [-90:90]
    set table $Dummy
        plot x ls 1
    unset table
    set xtics approxTicsX(5)
    set ytics approxTicsY(5)
    replot

unset multiplot
### end of script

Result:

enter image description here

Upvotes: 4

vaettchen
vaettchen

Reputation: 7659

Not sure whether I get your "of course, changing the number of tics is easy" right and I maybe I'm giving the answer you know yourself already - but anyway:

From help set xics:

Positions of the tics are calculated automatically by default or if the autofreq option is given; otherwise they may be specified in either of two forms:

The implicit , , form specifies that a series of tics will be plotted on the axis between the values and with an increment of . If is not given, it is assumed to be infinity. The increment may be negative. If neither nor is given, is assumed to be negative infinity, is assumed to be positive infinity, and the tics will be drawn at integral multiples of .

So to start with - you can play around with the settings yourself:

set xtics -7, 1.5
plot[-7.5:7.5][] sin(x)

which gives you

enter image description here

Upvotes: 0

Related Questions