Andreas
Andreas

Reputation: 851

Elegant way to for loop several statements in gnuplot 4.6?

I am trying to have a plot statement loop over several couplets of functions plots. The order of the statements is important because it creates overdraw in the right order.

#!/usr/bin/gnuplot -persist

datfile="overdraw.dat"
num=3
skip=40

set table datfile
g(x,t)=exp(-x**2+{0,1}*2*t*x)
set samples 501
plot [-2:2][0:5] for [ii=0:num] real(g(x,ii))
unset table

xspeed=0.1
yspeed=0.3

## this works but creates overdraw in the wrong order
#plot [-2:2] \
#  for [ii=0:num] datfile index ii u ($1+xspeed*ii):($2-yspeed*ii) not w l lt ii lw 8 \
#, for [ii=0:num] datfile index ii every skip u ($1+xspeed*ii):($2-yspeed*ii) not w p lt ii pt 7 ps 4 \
#

set macro

## this works but is cumbersome
plotstring="NaN not"
do for [ii=0:num] {
  plotstring=plotstring.sprintf(", \"%s\" index %i u ($1+xspeed*%i):($2-yspeed*%i) not w l lt %i lw 8", datfile, ii, ii, ii, ii)
  plotstring=plotstring.sprintf(", \"%s\" index %i every skip u ($1+xspeed*%i):($2-yspeed*%i) not w p lt %i pt 7 ps 4", datfile, ii, ii, ii, ii)
}
plot [-2:2] @plotstring


## this doesn't work because the for loop only applies to the first statement
#plotboth='datfile index ii u ($1+xspeed*ii):($2-yspeed*ii) not w l lt ii lw 8\
#, datfile index ii every skip u ($1+xspeed*ii):($2-yspeed*ii) not w p lt ii pt 7 ps 4'
#plot [-2:2] for [ii=0:num] @plotboth


## this gives an error message
plot [-2:2] for [ii=0:num] { \
  datfile index ii u ($1+xspeed*ii):($2-yspeed*ii) not w l lt ii lw 8\
, datfile index ii every skip u ($1+xspeed*ii):($2-yspeed*ii) not w p lt ii pt 7 ps 4 \
}

As you can see, I made it work in the right order by appending to a string holding a plot statement. It would be nice, however, to be able to just put brackets around the plot statements as indicated at the end of my example.

Submitting several plot/replot statements seems not to be an option, as that creates pages in some in some terminals (e.g. postscript). I would regard multiplot as cumbersome, too. Perhaps there is an elegant syntax that I have overlooked?

Upvotes: 3

Views: 906

Answers (1)

Schorsch
Schorsch

Reputation: 7925

Instead of having two commands, one for the lines and one for the points, I'd suggest one command for a line with points, but - because there are many data-points - skipping some in the plot (as you intend with the skip variable).
Based on your dataset, I used the following code to generate your plot:

plot [-2:2] for [ii=0:num] datfile index ii u ($1+xspeed*ii):($2-yspeed*ii) \
  not w lp lt ii lw 8 pt 7 pi skip ps 4  

I used the w lp command (which is short for with linespoints) to have a line and points, and the pi skip (which is short for pointinterval skip) to skip 40 datapoints between the symbols. More information on linespoints and pointinterval can be found in the documentation.

Upvotes: 2

Related Questions