'gnuplot: how to plot sum of three data sets

I have a data in file which I would like to plot using gnuplot. In the file, there are 3 data sets separated by two blank lines so that gnuplot can differentiate between the data sets by 'index'. I can plot three data sets separately via 'index' option of 'plot' command. However, I am not sure how can I plot the data which is sum of 2nd column of all three data sets?

Note: all three data sets have same x data, i.e. 1st column



Solution 1:[1]

To do this the simplest thing would be to change your file format. Gnuplot manipulates columns pretty well. Since you are sharing the x data, you can change the file format to have four columns (assuming you are just plotting (x,y) data):

<x data> <y1 data> <y2 data> <y3 data>

and use a command like

plot 'data.dat' using 1:2 title 'data 1', \
'' u 1:3 t 'data 2', \
'' u 1:4 t 'data 3', \
'' u 1:($2+$3+$4) t 'sum of datas'

The dollar signs inside the parens in the using column specification allow you to add/subtract/perform other functions on columnar data.

This way your data file will also be smaller since you won't repeat the x data.

Solution 2:[2]

@Youjun Hu, never say that there is "no way" to do something with gnuplot. Most of the cases there is a way with gnuplot only, sometimes maybe not obvious or sometimes a bit cumbersome.

Data: SO16861334.dat

1    11
2    12
3    13
4    14


1    21
2    22
3    23
4    24


1    31
2    32
3    33
4    34

Code 1: (works with gnuplot 4.6.0, needs some adaptions for >=4.6.5)

In gnuplot 4.6.0 (version at the time of OP's question) there were no datablocks and no plot ... with table. The example below only works for 3 subdatasets, but could be adapted for other numbers. However, arbitrary large number of subdatasets will be difficult with this approach.

### calculate sum from 3 different (sub)datasets, gnuplot 4.6.0
reset

FILE = "SO16861334.dat"

stats FILE u 0 nooutput
N = int(STATS_records/STATS_blocks)   # get number of lines per subblock

set table FILE."2"
    plot FILE u 1:2
set table FILE."3"
    x1=x2=y1=y2=NaN
    myValueX(col) = (x0=x1,x1=x2,x2=column(col), r=int($0-2)/N, r<1 ? x0 : r<2 ? x1 : x2)
    myValueY(col) = (y0=y1,y1=y2,y2=column(col), r<1 ? y0 : r<2 ? y1 : y2)
    plot FILE."2" u (myValueX(1)):(myValueY(2))
unset table
set key top left
set offset graph 0.1, graph 0.1, graph 0.2, graph 0.1

plot for [i=0:2] FILE     u 1:2 index i w lp pt 7 lc i+1 ti sprintf("index %d",i), \
                 FILE."3" u 1:2 every ::2 smooth freq w lp pt 7 lc rgb "magenta" ti "sum"
### end of code

Code 2: (works with gnuplot>=5.0.0)

This code works with arbitrary number of subdatasets.

### calculate sum from 3 different (sub)datasets, gnuplot>=5.0.0
reset

FILE = "SO16861334.dat"

set table $Data2
    plot FILE u 1:2 w table
unset table
set key top left
set offset graph 0.1, graph 0.1, graph 0.2, graph 0.1
set colorsequence classic

plot for [i=0:2] FILE   u 1:2 index i w lp pt 7 lc i+1 ti sprintf("index %d",i), \
                 $Data2 u 1:2 smooth freq w lp pt 7 lc rgb "magenta" ti "sum"
### end of code

Result: (same result for Code1 with gnuplot 4.6.0 and Code2 for gnuplot 5.0.0)

enter image description here

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 andyras
Solution 2