Gnuplot plot difference of 2 columns

gnuplotplot

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 ?

Best Answer

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.