Shenan
Shenan

Reputation: 545

loops in gnuplot inside the plot command

I am having trouble using a for loop in gnuplot inside the plot command. Here is a pared down gnuplot script, which focuses on the parts that aren't working:

set multiplot layout 1, 3 title "Convergence Plots"
planeIter=4

do for [ringIter=0:20:10] {
    if (ringIter == 0) {ringName="outer"} else {if (ringIter == 10) {ringName="middle"} else {ringName="inner"}}
    set title "Pressure in ".ringName." ring"
    plot for [quadIter=0:90:30] path1 every ::5 using 1:(1 + planeIter + ringIter + quadIter) notitle   # title quadName.' quadrant' with lines
#   if (quadIter == 0) {quadName="first"} else {if (quadIter == 30) {quadName="second"} else {if (quadIter == 60) {quadName="third"} else {quadName="fourth"}}}
}

My questions are:

  1. Where/how can I include the (commented out) if/else statement, so that I can replace 'notitle' in the line above with the 'title' after the # symbol?
  2. The lines plotted should be of the following columns in p: (1:5, 1:35, 1:65, 1:95), (1:15, 1:45, 1:75, 1:105) and (1:25, 1:55, 1:85, 1:115) but instead of taking the value in the second column (5, 35, 65, ...) it plots the column number (i.e. a constant). How can I get the plot command to select the values inside the columns?

I have included an image of what it looks like and what it should look like (below), which I have generated by not using for loops and variables for quadName.

gnuplot_image

The file, p, is a list of numbers, each row containing 121 columns.

Thank you for any help you can provide.

Upvotes: 1

Views: 1812

Answers (1)

Christoph
Christoph

Reputation: 48390

To use a variable to specify the selected column, you must use column(var), i.e. in your case it would be using 1:(column(1 + planeIter + ringIter + quadIter)).

To select a word for the title, you can define a function which returns the respective string, like:

quadName(x) = (x == 0 ? "first" : (x == 30 ? "second" : (x == 60 ? "third" : "fourth")))
plot for [quadIter=0:90:30] path1 using 1:(column(1 + planeIter + ringIter + quadIter)) \
    title quadName(quadIter).' quadrant' with lines

Alternatively you could also use the word function, which might be nicer for more selections:

quadName = "first second third fourth"
plot for [quadIter=0:90:30] path1 using 1:(column(1 + planeIter + ringIter + quadIter)) \
    title word(quadName, (quadIter / 30) + 1)

Upvotes: 1

Related Questions