1

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.

theozh
  • 22,244
  • 5
  • 28
  • 72

3 Answers3

1

If it is acceptable to write a temp file, I suggest this as a model:

gnuplot> tempfile = system("mktemp")
gnuplot> save tempfile
gnuplot> command = sprintf("grep -c 'logscale x' %s",tempfile)
gnuplot> x_is_logscale = system(command)
gnuplot> if (x_is_logscale) { print "yes" } else { print "no" }

Otherwise I suggest you file this as a feature request on the gnuplot project page. A complicating factor is that "log scale" is only a special case of the more general nonlinear axis support introduced in version 5. In fact the internal implementation of "set log x" is essentially

do_string("set nonlinear x via log10(x) inv 10**x")

You can see this by the output from

gnuplot> set log x
gnuplot> show nonlinear
    set nonlinear x via log10(x) inverse 10**x 

It would help to have an example of what such a script might do differently if it had this information. Are you envisioning a script that simply wants to know whether the user had previously typed "set log x" or would it want to model the expected nonlinear behavior of the current axis setting?

Ethan
  • 13,715
  • 2
  • 12
  • 21
  • Thank you for this solution. Hmmm, yes, in principle this is a way to find out whether logscale is set or not (although pretty cumbersome and not platform independent). In general, when you want to convert axis coordinates to pixel coordinates you need to know whether logscale is set or not (see for example: https://stackoverflow.com/a/57685754). Well, in principle, you could set your **own** user variable everytime you set/unset logscale. I agree, `set nonlinear` with functions might make it even more complicated. But here, I was limiting myself to check whether simple logscale is set or not. – theozh Mar 05 '23 at 11:40
0

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.

theozh
  • 22,244
  • 5
  • 28
  • 72
0

gnuplot's "fit" can do anything:

fit a*x+b '+' us 0:1 via a,b

If now FIT_WSSR is different from zero, x is at least not linear

OK, for y this approach can't work. ;)

Karl
  • 2,117
  • 13
  • 26