Generating plots in Gnuplot using loops

南楼画角 提交于 2020-01-01 10:06:09

问题


I would like to generate several plots using Gnuplot thats why I need to use loop. The data is loading from files "sort'i'.dat". The code is shown below but it doesn't work. I have got some problem with main loop. I don't know why it doesn't work, maybe it is connected with my version of Gnuplot. Thanks.

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
}

The error is: "line 1: invalid complex constant"


回答1:


This kind of do for iteration was introduced in version 4.6.0:

The following iteration works only since 4.6.0:

do for [i=0:10] { print i }

The iteration

plot for [i=0:10] i*x

works also with 4.4

One other option for 4.4, although quite ugly, would be to "outsource" the iterations. Only two of the lines depend on the iteration variable, which make this feasible. You construct all the plot instructions outside of gnuplot and then eval the complete string:

As an example using 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" '))

For the exec bash see gnuplot and bash process substitution. Of course you can use any other program to do the iteration.

But this doesn't of course replace the ease of having the gnuplot-internal iterations. Why not upgrade to 4.6?




回答2:


I had the same problem, and definitely the version of gnuplot is the issue. For example, I was working on gnuplot 1.8.10.0 and I got a message that I showed in the image 1. Then, I installed gnuplot 5.0, and it worked excellent! :).

gnuplot sample 1

gnuplot sample 2

All the best



来源:https://stackoverflow.com/questions/21278243/generating-plots-in-gnuplot-using-loops

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!