If you have two files both structured like this
1 1.2
2 1.3
3 1.4
4 1.5
5 1.6
6 1.7
you can simply add them by doing
p "<paste file1.txt file2.txt" u 1:($2+$4) w l
But if the second file is structured with less points, e.g.
1 1.2
3 1.4
5 1.6
7 1.8
this is no longer an option because both files have a different length. My first option was to just manually cut out the unnecessary points with vim, but since there are hundreds of files I would like to know if there is an easy gnuplot solution.
I want to ignore points which only appear in one of the files. Points which appear in both files should be added.
Following @TomFenech's advice I used awk combined with gnuplot's every to only plot the odd points
plot '<awk ''FNR==NR {a[FNR]=$1; cnt=FNR} {x[$1] += $2} END {for(i=1; i<=cnt; ++i) print a[i],x[a[i]]}'' test1.txt test2.txt' u 1:2 every 2::1 w l

You could combine the two files using awk:
plot '<awk ''{a[$1]+=$2} END {for (i in a) print i, a[i]}'' file1.txt file2.txt'
Using awk is potentially more flexible if you wanted to combine the files in a more complicated way.
You could use smooth frequency option to plot sum of values with same x
plot '<cat file1.txt file2.txt' using 1:2 smooth frequency
Edit:
cumulative --> frequency
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With