I\'m doing a large number of plots using gnuplot. As the data range (for both x and y axis) is variable for each plot, I need to let gnuplot auto set both range and tics. H
To get the distance between auto-placed tics, use the following code (save as ticstep.gp
):
xr = abs(max_value - min_value)
power = 10.0 ** floor(log10(xr))
xnorm = xr / power # approximate number of decades
posns = 20.0 / xnorm;
if (posns > 40) {
tics = 0.05
} else {
if (posns > 20) {
tics = 0.1
} else {
if (posns > 10) {
tics = 0.2
} else {
if (posns > 4) {
tics = 0.5
} else {
if (posns > 2) {
tics = 1
} else {
if (posns > 0.5) {
tics = 2
} else {
tics = ceil(xnorm)
}
}
}
}
}
}
ticstep = tics * power
This ought to be equivalent to the internal gnuplot-code to determine the ticstep (see axis.c, line 677.
To get the ticstep only, you can use stats
to get the respective data values:
stats 'file.txt' using 1 noutput
max_value = STATS_max
min_value = STATS_min
load 'ticstep.gp'
print ticstep
To get the number of plotted tics you need the autoextended axis limits (unless you use set autoscale fix
). For this you can plot with the unknown
terminal to get e.g. GPVAL_Y_MAX
and GPVAL_Y_MIN
:
set terminal push # save current terminal
set terminal unknown
plot 'file.txt' using 1
set terminal pop # restore terminal
max_value = GPVAL_Y_MAX
min_value = GPVAL_Y_MIN
load 'ticstep.gp'
print sprintf('ticstep = %f', ticstep)
numtics = int((xr / ticstep) + 1)
print sprintf('numtics = %d', numtics)