Search code examples
gnuplot

Gnuplot does not calculate statistics with stats


In the following code, the stats command fails. Gnuplot assumes that there are no valid points in $Data and therefore does not calculate the number of records (STATS_records). What needs to be changed to calculate the statistics?

reset session

$Data <<EOD
AK  lA  rA  lH  rH
4   8820    3861    4954.7  2093.5
4   6294.4  2604.3  4251.9  1733.1
4   8671.5  3271.7  4912    2075.7
4   9447.3  3454.1  5054.1  2220.5
4   8070.9  2919.6  4434.8  1920.8
2   8951.2  3548.1  4838.1  1946.2
4   7809.1  2573.1  3913    1723.8
4   7985.3  3228.1  4816.9  1897
2   9239    3970.7  5011.8  2161
2   7549.9  3104.6  3854.8  1681.7
2   8706.2  3167.6  4713.1  2191.5
4   10121.5 3890.7  5599.5  2377.3
4   9153.3  3114.2  4890.2  1930
4   8327.5  3741.6  4793.4  2140.1
3   7091.6  3013.2  4053.9  2224.8
3   6878.2  3298.8  4238.6  1900
2   6940.4  2751.3  3810.6  1581.8
2   7232.9  3296.6  4342    1540.1
3   8722    3851.3  5646.5  2213
4   7723.4  3541.9  4588.6  2380.7
2   8272.8  3184.3  5262.9  1914.6
2   6798.3  2685.9  3208    1665.5
4   7685.6  3132.5  3486.7  1457.2
2   6820.9  2677.6  4388.6  2223
4   6980.2  3062.4  4375.9  1681.9
EOD

LABELS          = 'lA rA lH rH'
DATA_BLOCKS     = 'V1 V2'
GROUP_LABELS    = '2 3 4'
GROUP_COLUMNS   = 'AK'

do for [selected_label in LABELS] {
    do for [i=1:words(DATA_BLOCKS)] {
        do for [j=1:words(GROUP_LABELS)] {
            stats $Data index word(DATA_BLOCKS, i) using (((column(GROUP_COLUMNS) == j) ? 1.0 : NaN) * column(selected_label)) 
            print STATS_records
        }
    }

    pause -1
    pause -1
}

line 45: warning: No valid data points found in file
line 47: undefined variable: STATS_records

Solution

  • gnuplot is simply not finding the blocks V1 and V2 (see your stats command ... index word(DATA_BLOCKS, i)...) So, if your data would look e.g. like this, the script probably would do what you expect.

    $Data <<EOD
    # V1
    AK  lA  rA  lH  rH
    4   9447.3  3454.1  5054.1  2220.5
    4   8070.9  2919.6  4434.8  1920.8
    2   8951.2  3548.1  4838.1  1946.2
    4   7809.1  2573.1  3913    1723.8
    3   7985.3  3228.1  4816.9  1897
    2   9239    3970.7  5011.8  2161
    
    
    # V2
    AK  lA  rA  lH  rH
    2   8706.2  3167.6  4713.1  2191.5
    4   10121.5 3890.7  5599.5  2377.3
    4   9153.3  3114.2  4890.2  1930
    3   7091.6  3013.2  4053.9  2224.8
    3   6878.2  3298.8  4238.6  1900
    2   6940.4  2751.3  3810.6  1581.8
    EOD
    

    Addition:

    You can either loop by name or by index:

    do for [GROUP_LABEL in GROUP_LABELS] { ... }   # loop by name
    
    do for [j=1:words(GROUP_LABELS)] { ... }       # loop by index
    

    Since your GROUP_LABELS are actually numbers, gnuplot will convert them into numbers, so you could use either string comparsion eq or number comparison (==)

    ... strcol(GROUP_COLUMNS) eq GROUP_LABEL ...         # when loop by name
    
    ... column(GROUP_COLUMNS) == word(GROUP_LABELS,j)    # gnuplot will convert the string word(...) into a number