使用循环在Gnuplot中生成绘图

use*_*221 6 scripting gnuplot

我想使用Gnuplot生成几个图,这就是为什么我需要使用循环.数据从文件"sort'i'.dat"加载.代码如下所示,但不起作用.我对主循环有一些问题.我不知道它为什么不起作用,也许它与我的Gnuplot版本有关.谢谢.

do for [i=0:10] {
  set term png
  set output 'sort'.i.'.png'
  set title "Quick sort"
  set xlabel "Position number"              
  set ylabel "Number on position"
  unset key                               
  plot 'sort'.i.'.dat' using 1:2 with points pt 5
}
Run Code Online (Sandbox Code Playgroud)

错误是:"第1行:无效复数常量"

Chr*_*oph 5

这种do for迭代是在4.6.0版本中引入的:

以下迭代仅适用于4.6.0以来:

do for [i=0:10] { print i }
Run Code Online (Sandbox Code Playgroud)

迭代

plot for [i=0:10] i*x
Run Code Online (Sandbox Code Playgroud)

也适用于4.4

4.4的另一个选择,虽然相当丑陋,但将"外包"迭代.只有两行依赖于迭代变量,这使得这是可行的.你构造了gnuplot之外的所有绘图指令,然后eval是完整的字符串:

以bash为例:

set terminal pngcairo
set title "Quick sort"
set xlabel "Position number"              
set ylabel "Number on position"
unset key
set style data points

loopstr = 'set output ''sort%d.png''; plot ''sort%d.dat'' using 1:2 pt 5; '
eval(system('exec bash -c "for ((a=0;a<=10;a++)) do printf \"'.loopstr.'\" \$a \$a; done" '))
Run Code Online (Sandbox Code Playgroud)

有关exec bash参见gnuplot和bash进程的替换.当然,您可以使用任何其他程序进行迭代.

但这当然不能取代gnuplot内部迭代的难易程度.为什么不升级到4.6?