英文:
How to programmatically check if logscale is set in gnuplot?
问题
以下是已翻译的内容:
Attempt 1:
有一些特殊的绘图例程使用不同的模板,知道是否设置了对数刻度x或y将有助于根据需要执行不同的脚本。
例如,gnuplot变量GPVAL_X_LOG
和GPVAL_Y_LOG
在第一个绘图之后包含值10.0
,尽管对数刻度尚未设置。
您可以在控制台中键入以下命令:
unset logscale
show logscale
set logscale x
show logscale
set logscale y 2
show logscale
set logscale x 5
show logscale
将打印以下响应:
logscaling on none
logscaling on x
logscaling on x y (base 2)
logscaling on x (base 5) y (base 2)
但如何获取这些消息并提取是否设置了对数刻度呢?
$Test <<EOD
EOD
set print $Test append
show logscale
unset print
print $Test
不起作用。$Test
仍然为空。
Attempt 2:
如果x轴是对数刻度,您无法将x2轴链接到x轴。
set link x2 via x inverse x
您将收到错误消息You must clear nonlinear x or y before linking it
,该错误消息存储在gnuplot变量GPVAL_ERRMSG
中,您可以轻松评估它,然而,由于此错误,脚本将停止。所以,这也行不通。
有没有解决方案的聪明想法?
希望如果没有设置对数刻度x,GPVAL_X_LOG
将为NaN
,只有在设置它时它应包含底数。
英文:
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.
答案1
得分: 1
如果可以写入一个临时文件,我建议使用以下模型:
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" }
否则,我建议你将此作为gnuplot项目页面的功能请求提交。一个复杂因素是“对数刻度”只是版本5中引入的更一般的非线性轴支持的特殊情况。实际上,“set log x”的内部实现基本上是:
do_string("set nonlinear x via log10(x) inv 10**x")
你可以通过以下输出来查看这一点:
gnuplot> set log x
gnuplot> show nonlinear
set nonlinear x via log10(x) inverse 10**x
如果有这些信息,将有助于提供一个示例,说明这样的脚本在具有这些信息时可能会有什么不同。你是否想象一个脚本只是想知道用户以前是否输入了“set log x”,还是它想要模拟当前轴设置的预期非线性行为?
英文:
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?
答案2
得分: 0
以下是已翻译的内容:
除了@Ethan提出的解决方案之外,这里有一个适用于Windows和Linux的版本。它仅依赖gnuplot并使用系统调用,而不需要安装额外的工具(特别是对于Windows)。
... # 先前的设置
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 = ''
... # 后续脚本和绘图命令
以上内容类似于@Ethan的脚本。然而,如果在技术上可能的话,我更喜欢像这样进行简单的检查:
GPVAL_X_LOG = NaN
:x轴对数标度未设置,即线性标度GPVAL_X_LOG = 10
:x轴对数标度设置为基数10
我将提交一个功能请求。
英文:
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 scaleGPVAL_X_LOG = 10
: logscale x set to basis 10
I will file a feature request.
答案3
得分: 0
gnuplot的"fit"可以做任何事情:
fit a*x+b '+' us 0:1 via a,b
如果现在FIT_WSSR不等于零,x至少不是线性的
好的,对于y,这种方法行不通。
英文:
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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论