Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add two file files with a different x-basis in gnuplot

Tags:

gnuplot

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

enter image description here

like image 288
tvcuyck Avatar asked Jan 28 '26 18:01

tvcuyck


2 Answers

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.

like image 61
Tom Fenech Avatar answered Jan 31 '26 07:01

Tom Fenech


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

like image 20
Sergei Avatar answered Jan 31 '26 06:01

Sergei