0

Suppose I have the following data:

"1,5"
"2,10"
""
"3,4"
"4,2"
""
"5,6"
"6,10"

I want to graph this using gnuplot with a line between each condition, similar to this display:

enter image description here

How might this be accomplished? I have looked into gridlines, but that does not seem to suit my need. I am also looking for a solution that will automatically draw condition / phase lines between each break in the data set.

Uwe Keim
  • 39,551
  • 56
  • 175
  • 291
Justapigeon
  • 560
  • 1
  • 8
  • 22
  • 1
    Are the "condition lines" the vertical dashed lines in the plot? If yes, you could get some ideas from here: https://stackoverflow.com/questions/4457046/how-do-i-draw-a-vertical-line-in-gnuplot – user000001 Dec 28 '18 at 06:51
  • Yes, that is what I am looking for, thank you! – Justapigeon Dec 28 '18 at 06:58

1 Answers1

0

As mentioned in the comments and explained in the linked question and its answers, you can draw arbitrary lines manually via set arrow ... (check help arrow).

However, if possible I don't want to adjust the lines manually every time I change the data or if I have many different plots. But, hey, you are using gnuplot, so, make it automated! To be honest, within the time figuring out how it can be done I could have changed a "few" lines and labels manually ;-). But now, this might be helpful for others.

The script below is written in such a way that it doesn't matter whether you have zero, one or two or more empty lines between the different blocks.

Comments:

  • the function valid(1) returns 0 and 1 if column(1) contains a valid number (check help valid).
  • the vertical lines are plotted with vectors (check help vectors). The x-position is taken as average of the x-value before the label line and the x-value after the label line. The y-value LevelY is determined beforehand via stats (check help stats).
  • the labels are plotted with labels (check help labels) and positioned at the first x-value after each label line and at an y-value of LevelY with an offset.

Script:

### automatic vertical lines and labels
reset session

$Data <<EOD
Baseline
  1  10.0
  2  12.0
  3  10.5
  4  11.0   # zero empty lines follow
Treatment
  5  45.0
  6  35.0
  7  32.5
  8  31.0   # one empty line follows

Baseline
  9  14.0
 10  12.8
 11  12.0
 12  11.3   # two empty lines follow


Treatment
 13  35.0
 14  45.0
 15  45.0
 16  37.0
EOD

set offset 1,1,1,1
set border 3
set title "Student Performance" font ",14"
set xlabel "Sessions"
set xtics 1 out nomirror
set ylabel "Number of Responses"
set yrange [0:]
set ytics out nomirror
set key noautotitle
set grid x,y

stats $Data u 2 nooutput
LevelY = STATS_max    # get the max y-level

getLinePosX(col) = (v0=v1,(v1=valid(col))?(x0=x1,x1=column(1)):0, v0==0?(x0+x1)/2:NaN)
getLabel(col)    = (v0=v1,(v1=valid(col))?0:(h1=strcol(1),h0=h1),column(1))

plot x1=NaN $Data u (y0=(valid(1)?$2:NaN),$1):(y0) w lp pt 13 ps 2 lw 2 lc "red", \
     x1=v1=NaN '' u (getLinePosX(1)):(0):(0):(LevelY) w vec nohead lc "black" lw 1.5 dt 2, \
        v1=NaN '' u (getLabel(1)):(LevelY):(sprintf("%s",v0==0?h0:'')) w labels left offset 0,1.5 font ",12"
### end of script

Result:

enter image description here

theozh
  • 22,244
  • 5
  • 28
  • 72