Froop
Froop

Reputation: 199

Gnuplot for loop with continous variables

I have a plot with many objects and labels. So I wanted to simplify the srcipt using loops. But I don't know how to adress the variables. I define the variables as followed

V1 = 10
V2 = 20
V3 = 23
...
LABEL1 = a
LABEL2 = b
...

The loop should look something like that

set for [i=1:15] label i at V(i),y_label LABEL(i)

This notation leads to errors compiling the script. Is it possiple at all to define such a loop in gnuplot? If so how can I do it? Thanks for your help!

Upvotes: 1

Views: 618

Answers (1)

Christoph
Christoph

Reputation: 48390

You can define a function which formats the label-definition as string and use a do loop to evaluate the strings:

y_label = 0
V1 = 10
V2 = 20
V3 = 23
LABEL1 = "a"
LABEL2 = "b"
LABEL3 = "c"

do for [i=1:3] {
   eval(sprintf('set label %d at V%d,y_label LABEL%d', i, i, i))
}

Alternatively, you can use two string with whitespace-separated words for the iteration:

V = "10 20 23"
LABEL = "a b c"
set for [i=1:words(V)] label i at word(V, i),y_label word(LABEL, i)

Note, that gnuplot 5.0 also has some limited support to use quote marks to hold several words together as one item:

V = "10 20 25"
LABEL = "'first label' 'second label' 'third one'"
set for [i=1:words(V)] label i at word(V, i),y_label word(LABEL, i)

enter image description here

Upvotes: 1

Related Questions