gnuplot : setting line style in a for loop

1k views Asked by At

I have to plot several curve on a same graph. I necessarly need to use a for loop to do this. I would like to plot the 2 first curves with lines and the others with points. I am able to plot all the curves with lines or all the curves with points but not to change in the same for loop. Here is the concerned part of my code:
set style line 1 lw 1 lc rgb "green"
set style line 2 lw 1 lc rgb "purple"
set style line 3 pt 1 ps 1.0 lc rgb "red"
set style line 4 pt 2 ps 1.0 lc rgb "red"
set style line 5 pt 3 ps 1.0 lc rgb "red"
plot for [i=1:words(FILES)] myDataFile(i) u (column(1)):((word(UTAUS_ch,i))) ls i title myTitle(i)

I would like to preface "ls i" with "w l" for the 2 first curves and "ls i" for the others. I tried to use a if statement by replacing "ls i" by "if (i < 2) {w l ls i} else {ls i}" but Gnuplot does not expect to find a if statement at this location.

Can someone help me ? Thank you, Martin

2

There are 2 answers

0
theozh On BEST ANSWER

As mentioned here you probably cannot switch plotting styles within a plot for loop. So, either you do two separate loops, one with points and the other with lines or you do one loop with linespoints and define all necessary parameters for points and lines as functions (to keep the plot command readable). As mentioned here, linewidth 0 is not zero but the thinnest possible line which is typically 1 pixel. To make the line disappear completely you have to use linetype -2.

Code:

### lines and points in the same plot for-loop
reset session

LINECOLORS = "red  green blue  magenta cyan"
LINEWIDTHS = '1.0  4.0   0.0   0.0     0.0'
POINTTYPES = '0    0     5     7       9'
POINTSIZES = '0    0     1.0   2.0     3.0'
TITLES     = 'one  two   three four    five'

myLinecolor(i) = word(LINECOLORS,i)
myLinewidth(i) = real(word(LINEWIDTHS,i))
myPointtype(i) = int(word(POINTTYPES,i))
myPointsize(i) = real(word(POINTSIZES,i))
myLinetype(i) = myLinewidth(i) == 0 ? -2 : 1
myTitle(i) = word(TITLES,i)

set samples 31
set key out

plot for [i=1:words(TITLES)] (sin(0.25*x-i)) w lp pt myPointtype(i) ps myPointsize(i) \
    lt myLinetype(i) lw myLinewidth(i) lc rgb myLinecolor(i) title myTitle(i)
### end of code

Result:

enter image description here

Addition:

In order to keep the plot command as short and clear as possible you could also define line styles and use it in the plot for command via ls i, with the same result as above.

...

do for [i=1:words(TITLES)] {
    set style line i pt myPointtype(i) ps myPointsize(i) \
        lt myLinetype(i) lw myLinewidth(i) lc rgb myLinecolor(i)
}

plot for [i=1:words(TITLES)] (sin(0.25*x-i)) w lp ls i title myTitle(i)
0
Friedrich -- Слава Україні On

Here a way using a macro:

set style line 1 lw 1 lc rgb "green"
set style line 2 lw 1 lc rgb "purple"
set style line 3 pt 1 ps 1.0 lc rgb "red"
set style line 4 pt 2 ps 1.0 lc rgb "red"
set style line 5 pt 3 ps 1.0 lc rgb "red"

set samp 100
set macro
cmd = ''
do for [i=1:10] {s = i<3? 'w l' : 'ls '.i; 
                 cmd = cmd . '"+" us  1:(sin(x-'.i.'/10.)) '.s.' title "key '.i.'",'}

plot [0:2*pi] @cmd

enter image description here