Search code examples
gnuplot

How to plot connected points from separated data blocks within a file?


The following data file can contain several blocks TP1, TP2, ..., TPn. How can I connect the points of each first, second, third, ... line with linespoint a diagram?

Example

first plotted line:

TP1 -> (37.74, 7030.26)
TP2 -> (26.42, 7652.64)
...

second plotted line:

TP1 -> (37.74, 7030.26)
TP2 -> (33.14, 7705.56)
...
reset 

GROUP_LABELS    = "1 2 3"
n_group_labels  = words(GROUP_LABELS)
in_group        = '( $1 == int(word(GROUP_LABELS, i)) ? 1.0 : NaN )'

$Data << EOD
# TP1
gp   HS   Age     vol_01    vol_02
2    0    37.74   7030.26   7353.44
2    0    26.42   7652.64   8566.62
2    0    30.20   6938.74   7489.41
2    0    27.68   6619.97   6224.74
2    1    18.87   7472.23   8635.48
2    0    34.56   6612.52   7347.70
2    0    33.54   7049.64   7137.09
3    1    21.47   8327.11   8745.67
1    0    22.00   7924.66   8694.33
1    0    24.96   7605.58   8072.01
3    1    12.10   8883.32   9230.87
2    0    24.72   8271.73   8465.82


# TP 2
gp   HS   Age     vol_01    vol_02
2    0    38.84   7069.54   7380.43
2    0    33.14   7705.56   8076.87
2    0    39.78   7346.33   7712.80
2    0    28.98   6963.54   7171.83
2    1    23.71   6830.13   8113.97
2    0    39.93   7438.96   7020.43
2    0    44.92   6943.02   7204.57
3    1    26.82   8245.33   8335.07
1    0    24.93   7396.14   7853.01
1    0    29.79   7421.25   7810.83
3    1    14.55   8672.95   8937.76
2    0    27.30   8668.11   8506.81



EOD

plot 0 lw 1 lc -1 notitle, \
    for [i=1:n_group_labels] $Data using ( @in_group * column(3)):(column(4)) w lp pt 7 ps 2 lc i 

Solution

  • gnuplot breaks a line in the graph if there are empty lines in the data. So, you have to get rid of the empty lines or play other "tricks".

    If I now understood correctly after your comment, I would suggest the following:

    • get rid of the empty lines by plotting your data into another datablock $Data2
    • if your blocks have all the same size you can work with every (check help every)
    • get the variable color from column 1
    • optional: add some labels for the patient, i.e. row index depending on the number of header lines using only the first block of the original data
    • add a legend for the groups using keyentry (check help keyentry)
    • if your plot 0 was just to force the y-range going from zero, you could also do set yrange[0:]

    Script:

    ### plot linespoints connected over blocks
    reset session
    
    $Data << EOD
    # TP1
    gp   HS   Age     vol_01    vol_02
    2    0    37.74   7030.26   7353.44
    2    0    26.42   7652.64   8566.62
    2    0    30.20   6938.74   7489.41
    2    0    27.68   6619.97   6224.74
    2    1    18.87   7472.23   8635.48
    2    0    34.56   6612.52   7347.70
    2    0    33.54   7049.64   7137.09
    3    1    21.47   8327.11   8745.67
    1    0    22.00   7924.66   8694.33
    1    0    24.96   7605.58   8072.01
    3    1    12.10   8883.32   9230.87
    2    0    24.72   8271.73   8465.82
    
    
    # TP 2
    gp   HS   Age     vol_01    vol_02
    2    0    38.84   7069.54   7380.43
    2    0    33.14   7705.56   8076.87
    2    0    39.78   7346.33   7712.80
    2    0    28.98   6963.54   7171.83
    2    1    23.71   6830.13   8113.97
    2    0    39.93   7438.96   7020.43
    2    0    44.92   6943.02   7204.57
    3    1    26.82   8245.33   8335.07
    1    0    24.93   7396.14   7853.01
    1    0    29.79   7421.25   7810.83
    3    1    14.55   8672.95   8937.76
    2    0    27.30   8668.11   8506.81
    EOD
    
    set table $Data2
        set datafile separator "\n" commentschar ""
        plot $Data u (strcol(1)) w table
        set datafile separator            # reset to default
        set datafile commentschar         # reset to default
    unset table
    
    stats $Data index 0 u 0 nooutput     # get length of block
    GROUP_LABELS = "1 2 3"
    N_g          = words(GROUP_LABELS)
    N_h          = 1                     # number of header lines
    N_p          = STATS_records - N_h   # number of patients in block
    set key noautotitle
    
    plot for [p=0:N_p-1] $Data2 every N_p+N_h::p+N_h u 3:4:1 w lp pt 7 ps 2 lc var, \
                         $Data index 0 u 3:4:($0+1-N_h) w labels right offset -1,0, \
         for [g=1:N_g] keyentry w lp pt 7 ps 2 lc g ti "group ".word(GROUP_LABELS,g)
    ### end of script
    

    Result:

    enter image description here