stafusa
stafusa

Reputation: 195

How to get (automatic) offsets in logscale plots?

The option set offsets has no effect on log plots.

For instance, both figures below have been generated with set offsets 0,2,0,0:

enter image description hereenter image description here

In order to obtain the same effect I'm currently setting fixed ranges instead of using autoscale, which is non-optimal, as the script is intended for generating plots from data in batch.

Since Gnuplot 5.2, we have '"set log" re-implemented as special case of "set nonlinear"', and set nonlinear makes use of two axis, as it 'is similar to the set link command except that only one of the two linked axes is visible', and, since the documentation also makes clear that 'The offsets only affect the x1 and y1 axes', it appears that instead of a bug this is expected behavior.

Question: Is there a simple way of obtaining a similar autoscaled offset for log plots?

I suppose one can make a dummy plot and use the data ranges for setting the axis ranges in the outputted plot, but is there a better way?

For the sake of completeness, minimum codes for generating the figures above are:

set terminal pngcairo size 250,250
set output "1.png"
set offsets 0,2,0,0
plot [0.1:1] 1/x

and

set terminal pngcairo size 250,250
set output "2.png"
set logscale
set offsets 0,2,0,0
plot [0.1:1] 1/x

Upvotes: 3

Views: 387

Answers (1)

theozh
theozh

Reputation: 25704

As you already mentioned in your question, you can create a dummy plot and add your offset to the ranges.

The point is that you can get the GPVAL_... variables only after plotting. So, currently I don't see another way to create a dummy plot unless this offset for logarithmic axes will be implemented in gnuplot itself.

The following example is an attempt to make this replot maybe a bit easier to handle using macros and evaluate(), see help macros and help evaluate. Perhaps it is a bit of a simplification if you batch process many plots. Maybe somebody will find a way to shorten this a bit.

Of course, you can also set myRange = [*:*] for autorange and myOffsets will be added to the autorange limits.

Code:

### offsets for logarithmic scale
reset session
set term pngcairo size 300,300

SetMyOffsets(n) = sprintf("[%g:%g][%g:%g]", \
                    GPVAL_X_MIN-word(myOffsets,1), \
                    GPVAL_X_MAX+word(myOffsets,2), \
                    GPVAL_Y_MIN-word(myOffsets,4), \
                    GPVAL_Y_MAX+word(myOffsets,3))

GenerateOutput = 'set output myOutput; \
                  eval(myPlot); \
                  set output; \
                  set output myOutput; \
                  myRange=SetMyOffsets(0); \
                  eval(myPlot); \
                  set output'

# first plot with linear x-axis
myRange = '[0.1:1]'
myPlot  = 'plot @myRange 1/x'
myOffsets = '0 2 0 0'           # left right top bottom
myOutput = "Linear.png"
@GenerateOutput

# second plot with log x-axis
set logscale
myRange   = '[0.1:1]'
myPlot    = 'plot @myRange 1/x'
myOffsets = '0 2 0 0'           # l r t b
myOutput = "Logarithmic.png"
@GenerateOutput

### end of code

Result:

enter image description hereenter image description here

Upvotes: 1

Related Questions