4

I use xmgrace to plot data sets and I would like to add two data sets together.

So I have two files:

data1

1 1
2 4
3 9
...

data2

1 2
2 3
3 4
...

I start xmgrace with

xmgrace data1 data2

Then I see both files plotted. Now I would like to generate a third graph which is just data1+data2 like

1 3
2 7
3 13
...

I know that this is extremely simple, but for some reason I don't understand how that works.

I played with the feature Data->Transformations->Evaluate expression... but I don't know what to select as "Source" and as "Destination".

PS: I know many other ways to reach my goal but I also want to be able to do it with xmgrace.

thyme
  • 388
  • 5
  • 18

3 Answers3

5

This can easily be achieved using the feature Evaluate expression...

xmgrace data1 data2

Click on Data->Transformations->Evaluate expression...

Select any one (say G0.S0) of the two sets (G0.S0 and G0.S1) as the source. Do not select any existing set as the destination if you want the output to a new set. The Formula should read

y=g0.s0.y+g0.s1.y

The evaluateExpression window should look like

Selections

When you Apply, a new set (G0.S3) will be created. You can modify the formula (g1 for graph G1, etc.) to add values across different graphs as well.

h k
  • 320
  • 2
  • 13
1
xmgrace data1 data2

Go to window > commands

Type in the following commands one by one.

s2 length s0.length
s2.x = s0.x # Change s0.x to s0.x+s1.x if you want to add x too.
s2.y = s0.y+s1.y
redraw
updateall

You can also add these commands into a add.com file and save it in the same directory as the data file.

Then window > commands > Read... > (load add.com) > Replay.

Sathyam
  • 200
  • 3
  • 15
1

Sathyam's answer is great if you don't mind using the xmgrace GUI, but if you want to do everything on the commandline there are a number of ways.

Answer: Here is one way to do it:

paste -d ' ' data1 data2 | awk '{print $1,$2+$4}' | xmgrace -pipe data1 data2

Step-by-step explanation: First we horizontally stack the two files using the paste command:

paste -d ' ' data1 data2

This gives the folowing output:

1 1 1 2
2 4 2 3
3 9 3 4

Then we pipe this to awk to print the first column and to do an element-wise sum of columns 2 and 4:

paste -d ' ' data1 data2 | awk '{print $1,$2+$4}'

This gives:

1 3
2 7
3 13

Now we want to pipe this on to xmgrace and then provide the original data files as additional data sets.

paste -d ' ' data1 data2 | awk '{print $1,$2+$4}' | xmgrace -pipe data1 data2

Note that the piped data is still in the third data set "S2" because the piped data gets processed last, as xmgrace is loaded.

Here is the output (after I made it look a little prettier):

enter image description here

feedMe
  • 3,431
  • 2
  • 36
  • 61