gnuplot plot difference of 2 columns

2020-07-20 04:34发布

I have two files A and B. Both files contain 2 columns, x and y.

Now, I want to plot a graph for x vs (yA - yB). Does gnuplot provide a command for the same ?

One more thing, lets say xA and xB are not same. How should I plot a graph where x-axis contains all elements which are in both, xA and xB and y-axis is the difference is the corresponding y-components ?

标签: plot gnuplot
2条回答
▲ chillily
2楼-- · 2020-07-20 05:05

I think this might be a good job for paste.

plot "<paste A B" u 1:($2-$4) w points #whatever line style you want...
                  #xA #yA-yB

For the file where xA != xB, I'm a little unclear whether you want to plot only the set of points with are common to both (the intersection of the two sets) or whether you want to plot all the points (the union of the sets). The union is easy:

plot "<paste A B" u 1:($2-$4) w points ls 1,\
     "<paste A B" u 3:($2-$4) w points ls 1

The intersection is hard using only unix commandline tools (especially if you want to preserve the order of your input)

using Python though, it's not too bad...

#joinfiles.py
import sys
f1=sys.argv[1]
f2=sys.argv[2]
xA,yA=zip(*[map(float,line.split()) for line in f1.readlines()])
xB,yB=zip(*[map(float,line.split()) for line in f2.readlines()])
f1.close()
f2.close()
for i,x in enumerate(xA):
    if(x in xB):
        sys.stdout.write('%f %f %f\n'%(x,yA[i],yB[i]))

and then from gnuplot:

plot "<python joinfiles.py A B" u 1:($2-$3) #...
查看更多
Summer. ? 凉城
3楼-- · 2020-07-20 05:25

First, preprocess the files with join in bash:

join <(sort -k1,1 file1) <(sort -k1,1 file2) > file3

Sorting the files is essential, otherwise join would not work.

Then you can use the result to draw the graph:

plot '< sort -n file3' using 1:($2-$3) with lines

Again, numeric sorting is needed here, because join uses alphanumeric sorting which makes the lines cross each other.

查看更多
登录 后发表回答