具有连续变量的 Gnuplot for 循环
Gnuplot for loop with continous variables
我有一个包含许多对象和标签的图。所以我想使用循环来简化 srcipt。但我不知道如何解决这些变量。我定义变量如下
V1 = 10
V2 = 20
V3 = 23
...
LABEL1 = a
LABEL2 = b
...
循环应该看起来像这样
set for [i=1:15] label i at V(i),y_label LABEL(i)
这种表示法会导致编译脚本时出错。是否可以在 gnuplot 中定义这样的循环?如果是这样我该怎么做?
感谢您的帮助!
您可以定义一个将标签定义格式化为字符串的函数,并使用 do
循环来评估字符串:
y_label = 0
V1 = 10
V2 = 20
V3 = 23
LABEL1 = "a"
LABEL2 = "b"
LABEL3 = "c"
do for [i=1:3] {
eval(sprintf('set label %d at V%d,y_label LABEL%d', i, i, i))
}
或者,您可以使用两个以空格分隔的字符串进行迭代:
V = "10 20 23"
LABEL = "a b c"
set for [i=1:words(V)] label i at word(V, i),y_label word(LABEL, i)
请注意,gnuplot 5.0 也有一些有限的支持,可以使用引号将多个单词作为一个项目保存在一起:
V = "10 20 25"
LABEL = "'first label' 'second label' 'third one'"
set for [i=1:words(V)] label i at word(V, i),y_label word(LABEL, i)
我有一个包含许多对象和标签的图。所以我想使用循环来简化 srcipt。但我不知道如何解决这些变量。我定义变量如下
V1 = 10
V2 = 20
V3 = 23
...
LABEL1 = a
LABEL2 = b
...
循环应该看起来像这样
set for [i=1:15] label i at V(i),y_label LABEL(i)
这种表示法会导致编译脚本时出错。是否可以在 gnuplot 中定义这样的循环?如果是这样我该怎么做? 感谢您的帮助!
您可以定义一个将标签定义格式化为字符串的函数,并使用 do
循环来评估字符串:
y_label = 0
V1 = 10
V2 = 20
V3 = 23
LABEL1 = "a"
LABEL2 = "b"
LABEL3 = "c"
do for [i=1:3] {
eval(sprintf('set label %d at V%d,y_label LABEL%d', i, i, i))
}
或者,您可以使用两个以空格分隔的字符串进行迭代:
V = "10 20 23"
LABEL = "a b c"
set for [i=1:words(V)] label i at word(V, i),y_label word(LABEL, i)
请注意,gnuplot 5.0 也有一些有限的支持,可以使用引号将多个单词作为一个项目保存在一起:
V = "10 20 25"
LABEL = "'first label' 'second label' 'third one'"
set for [i=1:words(V)] label i at word(V, i),y_label word(LABEL, i)