Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Generating plots in Gnuplot using loops

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"

like image 227
user1519221 Avatar asked Jan 22 '14 08:01

user1519221


People also ask

How to plot multiple data files in gnuplot?

The simplest method to plot multiple data files is to insert a for loop inside the plot command of gnuplot. Assuming you have N files named sequently, i.e.

How do I use Gnuplot in X Window?

For best results, however, you should run gnuplot from within X Window, so that you can see better previews of your plots. Entering Data All the data sets you use in gnuplot should be typed into a text file first. There should be one data point per line.

What are the advantages of gnuplot?

Another great advantage of GnuPlot is that allows the separation of script from data. GnuPlot runs a script, and generally the data is read from a data file specified by the script in the plot command. It likes columnar or tab-separated data, which suits SQL Server.

Can gnuplot produce a preview?

Producing Printed Output gnuplot is very device-neutral: when producing your plots, it could care less whether it is producing a preview on an X Window display, an ASCII-art version for a terminal, or any other output form. The plot commands will all work the same.


1 Answers

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?

like image 52
Christoph Avatar answered Oct 03 '22 00:10

Christoph