1

I'm making a script to plot desired pairs of columns of a datafile in a window. But i cant figure out how to make gnuplot accept arguments to the using command.

my code looks like this

#!/bin/bash
filee=$1
shift 1
cols=$@

gnuplot -p << eof
#TESTS--------------------------------
print "gnuplot--starts----------------"

print "cols-var is:     $cols"
print "filee-name is:   $filee"

print "pair-vars are:"
do for [pair in "$cols"] {print pair}

print "-------------------------------"
#END TESTS----------------------------"


set term wxt 1 size 1500,900 title 'columns of $filee'
set key top left
set grid
set xrange [2:8]
set yrange [-0.02:0.02]
set format y "%g"
plot for [pair in "$cols"] '$filee' u pair  
eof

If instead of pair as the argument to u i use 1:3 or 1:2 or whatever i get the correct number of plots, but of course they are all identical. I tried a lot of things and everything seem to be working besides that u doesn't accept a variable. The output wher running this is:

$ ./cploter written.dat 1:2 1:3 1:4
gnuplot--starts----------------
cols-var is:    1:2 1:3 1:4
filee-name is:  written.dat
pair-vars are:
1:2
1:3
1:4
-------------------------------
         line 0: warning: Skipping data file with no valid points
         line 0: warning: Skipping data file with no valid points
         line 0: warning: Skipping data file with no valid points 

Some help with this issue would be appreciated:)

EDIT: as u doesn't seem to be accepting the loop through its arguments I settled for the code below. If You know a different approach to plot desired column pairs into one plot, please leave a comment or answer:)

#!/bin/bash
fil=$1; xx=$2; y1=$3; y2=$4
gnuplot -p <<- eof
set term wxt 1 size 1500,900 title 'columns of $filee'
set key top left
set format y "%g"
plot for [i=${y1}:${y2}] '$fil' u ${xx}:i w l t 'Column ${xx}:'.i
eof

I'm on gnuplot 4.6. using for [i in $range] does not work in this case for some reason. Using variables ys(i)=word("$yes",i), xs=word("$xes",i) and plot for [i=1:3] '$fil' u xs(i):ys(i) w l doesn't work either.

2 Answers 2

1

(Partial answer; there are a few other ways of working around this situation)

I get a different error running your script (gnuplot 5.0)

line 0: warning: no column with header "1:2"

but I think the source of the problem is the same. The issue is that gnuplot is looking for a single column with the name "1:2" rather than interpreting the token "1:2" as a two-column using specification.

If you only want to plot pairs of the form 1:2, you can just pass the second column indices as arguments to your script and use the plot command

plot for [pair in "$cols"] '$filee' u 1:column(int(pair))
Sign up to request clarification or add additional context in comments.

4 Comments

I think you must use using 1:(column(pair)).
@Christoph, I tried that as well and it did not work properly. It acts as if I had issued columnhead(pair) and gives an error line 0: warning: no column with header "3", for example.
You are right, column(p) checks the data type of variable p: if it is a string, it is used as column label to select a column. Otherwise it is used a column index. A value of "3" isn't casted to int. You must do this yourself with using 1:(column(int(pair))).
I kind of settled for this kind of structure, see the updated post. Thanks anyway. If you come up with something, please post it. Sadly the `[pair in "$cols"] is not working at all.
0

Using [pair in "$cols"] works fine, provided all the rest is also correct. The following example works fine:

File test.dat

1 2 3 4
5 6 7 8
9 10 11 12

and the script plot.sh:

#!/bin/bash
filee=$1
shift 1
cols=$@

gnuplot -p <<EOF
set terminal pngcairo
set output 'test.png'
plot for [col in "$cols"] '$filee' u 1:(column(int(col)))

EOF

Call this script with

./plot.sh test.dat 2 3 4

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.