我正在尝试为基于 Julia 的 gnuplot 创建一个包装器来自动化我的绘图。我的目标是为 Julia 提供要绘制的文件名、要使用的线条样式类型以及要绘制的列。例如,如果我有文件test1
and test2
,都有 3 列和标题“time,COL1,COL2”以及自定义线条样式 1 和 2,我会这样写:
gnuplot -c gnuplot-script.gnuplot "test1 test2" "COL1 COL2" "1 2"
绘制时间与 COL1 的关系test1
和时间 vs COL2test2
分别使用用户选择的线型 1 和 2。但是,如果我想要时间 vs COL1 怎么办?with points
和时间 vs COL2with lines
?
我知道如何手动执行此操作,但是考虑到文件数量可以是任意数量,我如何自动执行此操作?我尝试了几种方法。
1.
我尝试使用do for
像这样循环:
nplots = words(ARG1)
do for [i=1:nplots] {
file = word(ARG1,i)
col = word(ARG2,i)
styl = word(ARG3,i)+0
# I have 10 custom line styles and all above 4 should be continuous line
if (styl>4) {
points_lines = "with lines"
} else {
points_lines = "with points"
}
plot file using "time":col @points_lines ls styl title col
}
这种方法创建独立的窗口而不是单个图,我想要一个图。
2.
我尝试使用这样的宏替换:
nplots = words(ARG1)
array files[nplots]
array cols[nplots]
array styles[nplots]
array points_lines[nplots]
do for [i=1:nplots] {
files[i] = word(ARG1,i)
cols[i] = word(ARG2,i)
styles[i] = word(ARG3,i)+0
if (styles[i]>4} {
points_lines[i] = "lines"
} else {
points_lines[i] = "points"
}
}
plot for[i=1:nplots] files[i] using "time":cols[i] @points_lines[i] ls styles[i] title cols[i]
但宏替换只接受标量变量,而不接受数组元素。后来,经过进一步阅读,了解了宏替换到底是如何工作的,并意识到这种方式永远行不通。
我很确定我可以使用整个绘图命令自动生成一个字符串,例如:
plot_command = "plot file1 using "time":"col" points_lines1 ls styles1, ..."
eval plot_command
但这种方法似乎需要大量工作,并且管理我想引入的所有异常根本不容易。
有没有更好的方法或者是我以编程方式创建字符串的唯一机会,然后eval
it?
提前致谢