Jas
Jas

Reputation: 15103

How to pass column number argument to gnuplot

I'm running this script:

gnuplot -e "arg_xlabel='My X Label'" -e "arg_ylabel='My Y Label'" -e "arg_filename='my.csv'" -e "arg_columnindex=4" histogram.plt

All arguments work except for the arg_columnindex which yields:

#!/gnuplot
#    
# Histogram of compression ratio distribution
#
set terminal postscript enhanced landscape
set output "histogram.ps"
set size ratio 0.5
set key top right
set xlabel arg_xlabel
set ylabel arg_ylabel
set style fill solid 1.0 border -1
set datafile separator ","
binwidth=0.02 # Adjust according to distribution of values in data file 
set boxwidth binwidth * 20
bin(x,width)=width*floor(x/width) + binwidth/2
plot arg_filename using (bin($arg_columnindex,binwidth)):(1.0) smooth freq with boxes t ""
# EOF
gnuplot -e "arg_xlabel='My X Labnel'" -e "arg_ylabel='My Y Label'" -e "arg_filename='my.csv'" -e "arg_columnindex=4" histogram.plt                                                              (base) 

plot arg_filename using (bin($arg_columnindex,binwidth)):(1.0) smooth freq with boxes t ""
                              ^
"histogram.plt" line 16: Column number or datablock line expected

How can I pass the column index argument?

Upvotes: 0

Views: 140

Answers (2)

theozh
theozh

Reputation: 25734

Besides Ethan's hint to $arg_columnindex, what is the reason that you are specifying the option -e multiple times? The following works for me. Option -p for window persist (not necessary in your case with postscript terminal).

gnuplot -p -e "arg_xlabel='My X Label'; arg_ylabel='My Y Label'; arg_filename='SO70859921.dat'; arg_columnindex=4" SO70859921.gp

Data: SO70859921.dat

1  0  0  6
2  0  0  4
3  0  0  7
4  0  0  1
5  0  0  3
6  0  0  2
7  0  0  5

Code: SO70859921.gp

### calling the script from command line with arguments

set xlabel arg_xlabel
set ylabel arg_ylabel

plot arg_filename u 1:(column(arg_columnindex)) w lp pt 7
### end of code

Result:

enter image description here

Upvotes: 1

Ethan
Ethan

Reputation: 15093

use column(arg_columnindex) instead of $arg_columnindex.

Upvotes: 1

Related Questions