gnuplot: plotting three lines points problems

Viewed 49
set title "Time Intervals"
set ylabel 'Y axis'
set xlabel 'Time'
set output 'output.eps'
set terminal postscript eps enhanced color font 'Helvetica-Bold'
set key noinvert samplen 4 spacing 1.5 width 3 height 1
set key width 6.8 height 0.5 box top right
set xrange[0:11]
set yrange [0:1]
set xtics 1
set ytics 0.1
set xlabel font "Arial-Bold,20"
set ylabel font "Arial-Bold,20"
set xtics font "Arial-Bold,20"
set ytics font "Arial-Bold,20"
set key font "Arial-Bold,20"
set title font "Arial-Bold,20"
plot  "data" using 1:2  title  "t1" with linespoint lw 5.5 pt 6 ps 3.5 lt 1 lc rgb "blue" ,\
      "data" using 1:3  title  "t2" with linespoint lw 9.5 pt 3 ps 2.5 lt 1 lc rgb "red" ,\
      "data" using 1:4  title   "t3" with linespoint lw 9.5 pt 8 ps 2.5 lt 1 lc rgb "green"

DataFile

0    0       -99      -99
1    0       -99      -99
2    0       -99      -99
3    0       0.2      -99
4    -99     0.25     -99
5    -99     0.3      -99
6    -99     0.35     -99
7    -99     0.5      -99
8    -99      0.5       0
9    -99     -99        0
10    -99     -99        0
11    -99     -99        0

enter image description here

Please anyone help. After running this code I have face one problem. t2 should start from time=3 and it should connect with t1 when time=2. In same way when t3(ends at time 8) and t2 should connect time =9.

1 Answers

As Ethan mentioned in the comments, what is the condition for plotting and connecting? From your description I assume that you might want to plot and connect the maxima of columns 2, 3 and 4 with the corresponding linewidths, pointtypes, pointsizes, and colors.

Since you are plotting 3 different curves they will not be connected. In case they need to be connected: What should be the color and linewidth for the connections?

Although, you could plot the data as one curve with variable pt, ps, and color. However, there is no variable linewidth. Furthermore, you would have to create the 3 key entries manually (edit: see alternative solution below).

Anyway, in the simplified example below, 3 functions are defined using the ternary operator (check help ternary)

  • colMax() returns the number of the 3 columns which holds the maximum value
  • isMax() returns the value of the column (first input parameter) if the value is larger than the values of the other columns (2nd, 3rd input parameter) and NaN otherwise (i.e. not plotted).
  • color() returns the color as RGB value (scheme: 0xrrggbb) as function of column number

The connection lines are plotted first with variable color but constant linewidth (since there is no easy variable linewidth).

Script:

### conditional plot
reset session

$Data <<EOD
0    0       -99      -99
1    0       -99      -99
2    0       -99      -99
3    0       0.2      -99
4    -99     0.25     -99
5    -99     0.3      -99
6    -99     0.35     -99
7    -99     0.5      -99
8    -99      0.5       0
9    -99     -99        0
10    -99     -99        0
11    -99     -99        0
EOD

isMax(col1,col2,col3)  = (v1=column(col1), v2=column(col2), v3=column(col3), \
                          v1>v2 && v1>v3 ? column(col1) : NaN)
colMax(col1,col2,col3) = (v1=column(col1), v2=column(col2), v3=column(col3), \
                         v1>v2 && v1>v3 ? col1 : v2>v1 && v2>v3 ? col2 : col3)
color(col) = int(word("0x0000ff 0xff0000 0x00ff00",col-1))

set xrange[0:11]
set yrange[0:1]
set key spacing 2.0

plot $Data u 1:(column(colMax(2,3,4))):(color(colMax(2,3,4))) w l lw 5.5 lc rgb var notitle, \
        '' u 1:(isMax(2,3,4)) w lp lw 5.5 pt 6 ps 3.5 lc "blue"  ti "t1", \
        '' u 1:(isMax(3,2,4)) w lp lw 9.5 pt 3 ps 2.5 lc "red"   ti "t2", \
        '' u 1:(isMax(4,2,3)) w lp lw 9.5 pt 8 ps 2.5 lc "green" ti "t3"
### end of script

Result: (wxt terminal)

enter image description here

Alternative Solution: (with fixed linewidth and variable ps,pt,color)

Attention: there is/was a mix-up of order of variable pointtype and pointsize from gnuplot 5.2 to 5.4 (see https://sourceforge.net/p/gnuplot/bugs/2524/). Depending on your version you might need to adapt the order.

Script: (tested with gnuplot 5.2.8)

### conditional plot
reset session

$Data <<EOD
0    0       -99      -99
1    0       -99      -99
2    0       -99      -99
3    0       0.2      -99
4    -99     0.25     -99
5    -99     0.3      -99
6    -99     0.35     -99
7    -99     0.5      -99
8    -99      0.5       0
9    -99     -99        0
10    -99     -99        0
11    -99     -99        0
EOD

colMax(col1,col2,col3) = (v1=column(col1), v2=column(col2), v3=column(col3), \
                         v1>v2 && v1>v3 ? col1 : v2>v1 && v2>v3 ? col2 : col3)

myColors     = "0x0000ff 0xff0000 0x00ff00"
myColor(col) = int(word(myColors,col-1))
myPts        = "6 3 8"
myPt(col)    = int(word(myPts,col-1))
myPss        = "3.5 2.5 2.5"
myPs(col)    = real(word(myPss,col-1))
myTitles     = "t1 t2 t3"
myTitle(col) = word(myTitles,col-1)

set xrange[0:11]
set yrange[0:1]
set key spacing 2.0

plot $Data u 1:(c=colMax(2,3,4),column(c)):(myPs(c)):(myPt(c)):(myColor(c)) w lp \
        ps var pt var lc rgb var lw 5.5 notitle, \
     for [c=2:4] keyentry w lp ps myPs(c) pt myPt(c) lc rgb myColor(c) lw 5.5 ti myTitle(c)
### end of script

Result:

enter image description here

Related