loop over array in gnuplot

26,348

If you want to have all files in a single plot, you need to use plot for[... (supported since version 4.4). Looping over several plot commands with do for (supported only since version 4.6) works only in multiplot mode.

The following two solutions both plot all data in one graph, but differ a bit in the iterations.

The first solution uses word to extract a word from a string directly when plotting.

colors = "red green #0000FF"
files = "file1 file2 file3"
plot for [i=1:words(files)] word(files, i).'.dat' lc rgb word(colors, i)

The second solution changes the linetype and then iterates directly over the word list instead of using an index.

colors = "red green #0000FF"
files = "file1 file2 file3"
set for [i=1:words(colors)] linetype i lc rgb word(colors, i)
plot for [file in files] file.'.dat'
Share:
26,348
tommy.carstensen
Author by

tommy.carstensen

Working in human genetics via structural bioinformatics / biophysics via wet lab biochemistry. Writing a bit of Python code since 2005 (version 2.4). Grateful for receiving help on SO and trying to pay back, when I have the time and ability.

Updated on July 23, 2020

Comments

  • tommy.carstensen
    tommy.carstensen almost 4 years

    This question is related to Loop structure inside gnuplot? and the answer there by DarioP.

    gnuplot 4.6 introduced the do command. How can I use this to loop over an array of for example files and colors? What is the correct syntax?

    colors = "red green #0000FF"
    files = "file1 file2 file3"
    
    do for [i=1:3] {
      plot files(i).".dat" lc colors(i)
    }