I have a file with this format
0 R1 R2 R3 R4
w1 I1 I2 I3 I4
w2 I1 I2 I3 I4
w3 I1 I2 I3 I4
with values of radius R and intensity I in many wavelengths w. I want to plot in 2D, the row 1 (the radius) in the x-axis and row 3 in the y-axis (choosing w2).
how plot per lines? not per columns
It's perhaps not the most elegant solution but you could first filter the two lines (the header containing the x-values and the line corresponding to the wavelength of interest), print them side by side, and finally plot this auxiliary data using Gnuplot.
To this end, the strategy would be to create a script called, e.g., filter.awk
in the directory from which you want execute Gnuplot with following content:
NR==1{
N = NF-1;
for(i=1;i<=N;i++) x[i] = $(i+1);
next;
}
$1==w{
printf "#%f\n", $1;
for(i=1;i<=N;i++) print x[i], $(i+1);
printf "\n";
}
This remembers the x-values from the first line and stores them in an array x
. The wave length is passed as an command line argument (see below) via the variable w
. If the first column in the data is equal to w
, the script then generates two columns: x-values in the first one, y-values (taken from the current row) in the second one.
In Gnuplot you can then use this as:
plot \
'<gawk -v w=100 -f filter.awk data.dat' w l
where data.dat
would be your input data and the value of 100
represents the wave length which is supposed to be filtered.
To generalize this, one could for example do:
cmd(w)=sprintf('<gawk -v w=%f -f filter.awk test.dat', w);
plot for [w in "100 200"] cmd(w+0) w l t sprintf('wave length of %.1f', w+0)
Here, the plotting command is generated via the cmd
function which accepts one argument denoting the wave length. The plot command then loops over a "list" of wave lengths and plots them individually with custom title. The statement w+0
is used here to explicitly convert the string value of w
to a number.
One solution is to create a new file with the right format (data in columns).
First create a function to read data in your file (see topic "Reading dataset value into a gnuplot variable (start of X series)"):
at(file, row, col) = system( sprintf("awk -v row=%d -v col=%d 'NR == row {print $col}' %s", row, col, file) )
file="myFile"
Thus construct your new file and plot it:
do for [j=1:5] { # 5 = number of your columns
x = at(file,1,j) # x axis in your example
y = at(file,3,j) # y axis
set print "newFile.txt" append
print x," ",y
set print
}
plot "newFile.txt"
Be sure to delete newFile.txt
each time you do the process.