Credits | Overview | Plotting Styles | Commands | Terminals |
---|
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. An exception to this is that definitions are grouped with the following plot item even if there is an intervening comma. Note that iteration does not work for plots in parametric mode.
Example:
plot for [j=1:3] 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 example does exactly the same thing as the previous example, but uses the string iterator form of the command rather than an integer iterator.
If an iteration is to continue until all available data is consumed, use the symbol * instead of an integer <end>. This can be used to process all columns in a line, all datasets (separated by 2 blank lines) in a file, or all files matching a template.
Examples:
plot for [file in "A.dat B.dat"] for [column=2:*] file using 1:column splot for [i=0:*] 'datafile' index i using 1:2:3 with lines plot for [i=1:*] file=sprintf("File_%03d.dat",i) file using 2 title file
Caveat: You can nest iterations where one is open-ended, as in the first example above. However nesting an open-ended iteration inside another open-ended iteration is probably not useful, since both will terminate at the same time when no data is found. The program will issue a warning if this happens.