gnuplot里面的循环结构?

时间:2013-02-18 22:32:52

标签: gnuplot

有没有办法迭代检索多个文件中的数据并将它们绘制在gnuplot中的相同图形上。假设我有像data1.txt,data2.txt ...... data1000.txt这样的文件;每个都有相同的列数。现在我可以写一些像 -

plot "data1.txt" using 1:2 title "Flow 1", \
     "data2.txt" using 1:2 title "Flow 2", \
      .
      .
      .
     "data1000.txt"  using 1:2 title "Flow 6"

但这真的很不方便。我想知道是否有办法遍历gnuplot中的 plot 部分。

6 个答案:

答案 0 :(得分:90)

肯定有(在gnuplot 4.4 +中):

plot for [i=1:1000] 'data'.i.'.txt' using 1:2 title 'Flow '.i

变量i可以解释为变量或字符串,因此您可以执行类似

的操作
plot for [i=1:1000] 'data'.i.'.txt' using 1:($2+i) title 'Flow '.i

如果你想让线条相互偏移。

在gnuplot命令行输入help iteration以获取更多信息。

另外请务必查看@ DarioP关于do for语法的答案;这使您更接近传统的for循环。

答案 1 :(得分:76)

从gnuplot 4.6开始,看看do { ... }命令,因为它非常强大:

do for [t=0:50] {
  outfile = sprintf('animation/bessel%03.0f.png',t)
  set output outfile
  splot u*sin(v),u*cos(v),bessel(u,t/50.0) w pm3d ls 1
}

http://www.gnuplotting.org/gnuplot-4-6-do/

答案 2 :(得分:9)

我有脚本all.p

set ...
...
list=system('ls -1B *.dat')
plot for [file in list] file w l u 1:2 t file

这里最后两行是文字的,而不是启发式的。然后我跑

$ gnuplot -p all.p

*.dat更改为您拥有的文件类型,或添加文件类型。

下一步:添加到〜/ .bashrc这一行

alias p='gnuplot -p ~/./all.p'

并将您的文件all.p放入您的主目录中,然后瞧。您可以通过键入p并输入来绘制任何目录中的所有文件。

编辑我更改了命令,因为它没有用。以前它包含list(i)=word(system(ls -1B *.dat),i)

答案 3 :(得分:2)

如果要在图表中绘制离散列

,请使用以下内容
do for [indx in "2 3 7 8"] {
  column = indx + 0
  plot ifile using 1:column ;  
}

答案 4 :(得分:1)

我想使用通配符来绘制通常放在不同目录中的多个文件,同时在任何目录下工作。我找到的解决方案是在~/.bashrc

中创建以下函数
plo () {
local arg="w l"
local str="set term wxt size 900,500 title 'wild plotting'
set format y '%g'
set logs
plot"
while [ $# -gt 0 ]
        do str="$str '$1' $arg,"
        shift
done
echo "$str" | gnuplot -persist
}

并使用它,例如与plo *.dat ../../dir2/*.out类似,用于绘制当前目录中的所有.dat个文件以及恰好为级别的目录中的所有.out个文件,并称为dir2

答案 5 :(得分:0)

这是替代命令:

gnuplot -p -e 'plot for [file in system("find . -name \\*.txt -depth 1")] file using 1:2 title file with lines'