Search code examples
gnuplotscalelogarithm

How to programmatically check if logscale is set in gnuplot?


For some special plotting routines with different templates it would be helpful to know whether logscale x or y is set or not, and based on that the script might continue differently.

Attempt 1:

There are e.g. the gnuplot variables GPVAL_X_LOG and GPVAL_Y_LOG which contain the value 10.0 after the first plot, although logscale is not yet set.

In the console you can type the commands:

unset logscale
show  logscale
set   logscale x
show  logscale
set   logscale y 2
show  logscale
set   logscale x 5
show  logscale

The following responses will be printed:

logscaling on none

logscaling on  x

logscaling on  x y (base 2)

logscaling on  x (base 5) y (base 2)

But how to get hold of that message(s) and extract if logscale is set or not?

$Test <<EOD
EOD
set print $Test append
    show logscale
unset print
print $Test

Doesn't work. $Test will still be empty.

Attempt 2:

You cannot link the x2-axis to the x-axis if the x-axis is logarithmic.

set link x2 via x inverse x

You will get an error message You must clear nonlinear x or y before linking it which is stored in the gnuplot variable GPVAL_ERRMSG, which you could easily evaluate, however, with this error the script will be stopped. So, this doesn't work either.


Any smart ideas for a solution?

It would be good if GPVAL_X_LOG would be NaN, if logscale x is not set and only if it is set it should contain the base.


Solution

  • In addition to @Ethan's suggested solution, here is a version which is working for Windows and Linux. It's gnuplot-only with a system call, but without requiring installation of extra tools (especially for Windows).

    ...  # previous settings
    
    FILE = "Temp.tmp"
    save FILE
    
    temp = (cmd = GPVAL_SYSNAME[1:7] eq "Windows" ? "type" : "cat", \
            system(sprintf("%s %s", cmd, FILE)))
    
    if (exists("GPVAL_X_LOG")) {
        if (strstrt(temp,"set logscale x")) { 
            print sprintf("logscaling x ON, basis %g", GPVAL_X_LOG)
        }
        else {print "logscaling x OFF"}
    }
    else { print "logscale not yet defined."}
    temp = ''
    
    ... # later script and plotting command
    

    The above works similar to @Ethan's script. Nevertheless, if technically possible, I would prefer a simple check like this:

    • GPVAL_X_LOG = NaN: logscale x not set, i.e. linear scale
    • GPVAL_X_LOG = 10: logscale x set to basis 10

    I will file a feature request.