3

I know that with xmgrace if I have a multiple column file I can plot a certain column as a function of another (say, 3 vs 1) with the command

xmgrace -block file.dat -bxy 1:3

If I want to plot 2vs1 and 3vs1 in the same window, I will simply use the command

xmgrace -block file.dat -bxy 1:2 -block file.dat -bxy 1:3

But if the files contains a large number of columns and I want to plot all of them as a function of the first (2vs1, 3vs1, 4vs1 etc.) in the same window it becomes impractical to use this command.

Is there a simple command to plot all the columns in a file as a function of the first (2vs1, 3vs1 etc.)?

I have tried

for i in {2..n}; do xmgrace -block file.dat -bxy 1:$i; done

But this way the plots will appear in different windows...

valerio
  • 677
  • 4
  • 12
  • 25

2 Answers2

4

The nxy option is designed to do this.

xmgrace -nxy file.dat

With this option, xmgrace reads the first column of file.dat as x-data and every other column as y(i)-data, and plots y(i) vs x on the same graph (using different colors by default).

h k
  • 320
  • 2
  • 13
1

Short answer:

This will create individual plot files for each data series vs column 1:

for i in {2..7}; do `gracebat -nosafe -hdevice PNG -printfile $i.png -block a.txt -bxy 1:$1`; done

Long answer:

If you wanted to plot all columns (from number 2 up to and including e.g. column 7) vs column 1 on the same axes you could do:

for i in {2..7}; do echo -n " -block file.dat -bxy 1:$i"; done | xargs xmgrace

But since your question is asking for individual plots we can do this:

for i in {2..7}; do echo -n " -block file.dat -bxy 1:$i" | xargs xmgrace; done

or this, which give the same result but is simpler:

for i in {2..7}; do echo -n `xmgrace -block file.dat -bxy 1:$i`; done

However, this is not very practical because the individual plots appear one by one and each has to be closed for the next one to appear.

It would be better to get xmgrace to run in batch mode and to save an image of each plot for you. You can do this with an xmgrace batch file and by using the gracebat executable.

for i in {2..7}; do `gracebat -nosafe -batch save.bfile -block a.txt -bxy 1:$1`; done

This calls grace in batch mode with the same data as before but runs the batch commands saved in save.bfile, which contains:

PRINT TO "out.ps"
PRINT

The obvious problem with this is that each new iteration overwrites the out.ps postscript file because the name of the output file is hardcoded in our batch file! So you only ever see the last one in the file. Luckily we can use sed to modify the batch file each time before we call gracebat!

for i in {2..7}; do `sed -e "s/outName/${i}/g" save.bfile > new.bfile`; `gracebat -nosafe -batch new.bfile -block a.txt -bxy 1:$1`; done

Where save.bfile now contains

PRINT TO "outName.ps"
PRINT

and the string outName gets replaced by $i.

You should now have a load of .ps files with your plots inside. Play around with the batch file in order to do more things with it. See this page for more commands that you can include, including how to change the output file format.

Alternatively you can skip using batch files altogether:

for i in {2..7}; do `gracebat -nosafe -hdevice PNG -printfile $i.png -block a.txt -bxy 1:$1`; done

If you have a parameter file you can add the -param command to set styles too.

feedMe
  • 3,431
  • 2
  • 36
  • 61
  • I needed all of them on the same axis. I was not clear enough. Thank you very much, I can learn a lot from the rest of the answer :) – valerio Aug 21 '16 at 10:47