【发布时间】:2020-08-20 01:01:27
【问题描述】:
我正在尝试为基于 Julia 的 gnuplot 创建一个包装器,以自动化我的绘图。我的目标是为 Julia 提供要绘制的文件名、我想要使用的线型类型以及要绘制的列。例如,如果我有文件 test1 和 test2,它们都有 3 列和标题“时间、COL1、COL2”以及自定义线型 1 和 2,我会这样写:
gnuplot -c gnuplot-script.gnuplot "test1 test2" "COL1 COL2" "1 2"
分别使用用户选择的线型 1 和 2 绘制 test1 的时间与 COL1 和 test2 的时间与 COL2。但是,如果我想要时间 vs COL1 with points 和时间 vs COL2 with 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 它?
提前致谢
【问题讨论】:
-
编辑:在方法 2 中,
with在@points_lines[i]之前缺失
标签: gnuplot