#### 3.15.9 iteration

If many similar files or functions are to be plotted together, it may be convenient to do so by iterating over a shared plot command.

Syntax:

          plot for [<variable> = <start> : <end> {:<increment>}]
          plot for [<variable> in "string of words"]

The scope of an iteration ends at the next comma or the end of the command, whichever comes first. Iteration can not be nested.

This will plot one curve, sin(3x), because iteration ends at the comma

          plot for [i=1:3] j=i, sin(j*x)

This will plot three curves because there is no comma after the definition of j

          plot for [i=1:3] j=i sin(j*x)

Example:

          plot for [dataset in "apples bananas"] dataset."dat" title dataset

In this example iteration is used both to generate a file name and a corresponding title.

Example:

          file(n) = sprintf("dataset_%d.dat",n)
          splot for [i=1:10] file(i) title sprintf("dataset %d",i)

This example defines a string-valued function that generates file names, and plots ten such files together. The iteration variable (’i’ in this example) is treated as an integer, and may be used more than once.

Example:

          set key left
          plot for [n=1:4] x**n sprintf("%d",n)

This example plots a family of functions.

Example:

          list = "apple banana cabbage daikon eggplant"
          item(n) = word(list,n)
          plot for [i=1:words(list)] item[i].".dat" title item(i)
          list = "new stuff"
          replot

This example steps through a list and plots once per item. Because the items are retrieved dynamically, you can change the list and then replot.

Example:

          list = "apple banana cabbage daikon eggplant"
          plot for [i in list] i.".dat" title i
          list = "new stuff"
          replot

This is example does exactly the same thing as the previous example, but uses the string iterator form of the command rather than an integer iterator.