I'm trying to create a wrapper for gnuplot based in Julia to automate my plots. My goal is to give Julia the file names to plot, the type of line style that I want to use and the column to plot. For example, if I have files test1
and test2
, both with 3 columns and headers "time,COL1,COL2" and custom line styles 1 and 2, I would write this:
gnuplot -c gnuplot-script.gnuplot "test1 test2" "COL1 COL2" "1 2"
To plot time vs COL1 of test1
and time vs COL2 of test2
using the line styles 1 and 2, respectively, chosen by the user. However, what if I want time vs COL1 with points
and time vs COL2 with lines
?
I know how to do it manually, but how could I do it automatically, considering that the number of files can be any number? I tried several ways.
1.
I tried using the do for
loop like this:
nplots = words(ARG1)
do for [i=1:nplots] {
file = word(ARG1,i)
col = word(ARG2,i)
styl = word(ARG3,i)+0
# I have 10 custom line styles and all above 4 should be continuous line
if (styl>4) {
points_lines = "with lines"
} else {
points_lines = "with points"
}
plot file using "time":col @points_lines ls styl title col
}
This approach creates independent windows and not a single plot, and I want a single plot.
2.
I tried using macro substitution like this:
nplots = words(ARG1)
array files[nplots]
array cols[nplots]
array styles[nplots]
array points_lines[nplots]
do for [i=1:nplots] {
files[i] = word(ARG1,i)
cols[i] = word(ARG2,i)
styles[i] = word(ARG3,i)+0
if (styles[i]>4} {
points_lines[i] = "lines"
} else {
points_lines[i] = "points"
}
}
plot for[i=1:nplots] files[i] using "time":cols[i] @points_lines[i] ls styles[i] title cols[i]
But macro substitution only accepts scalar variables, not array elements. Later, after further reading, learned how exactly macro substitution works and realized this way would never work.
I'm pretty sure that I could automatically generate a string with the entire plot command like:
plot_command = "plot file1 using "time":"col" points_lines1 ls styles1, ..."
eval plot_command
But this approach seems a lot of work and managing all exceptions that I want to introduce is not easy at all.
Is there any better approach or is my only chance to create the string programmatically and then eval
it?
Thanks in advance