问题
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
回答1:
I'm not sure, but I guess you can't change the plotting style from with points
to with lines
within a plot command via @
macro (at least I haven't succeeded).
But you can use the plotting style with linespoints
together with linestyle
such that it looks either like with points
or with lines
.
It is obvious to set pointsize 0
to get lines only. However, set linewidth 0
in order to get points only doesn't work as I've just learned here: gnuplot: why is linewidth 0 not zero in width?. Instead, use linetype -2
.
At some point you have to define your 10 linestyles anyway.
Code:
### change between plotting styles 'with points' and 'with lines' programmatically
reset session
PlotCount = words(ARG1)
File(i) = word(ARG1,i)
Col(i) = word(ARG2,i)
Style(i) = int(word(ARG3,i))
set style line 1 lt -2 pt 7 lc rgb "red"
set style line 2 lt -2 pt 7 lc rgb "green"
set style line 3 lt -2 pt 7 lc rgb "blue"
set style line 4 lt -2 pt 7 lc rgb "magenta"
set style line 5 lt 1 ps 0 lc rgb "yellow"
set style line 6 lt 1 ps 0 lc rgb "cyan"
set style line 7 lt 1 ps 0 lc rgb "orange"
set style line 8 lt 1 ps 0 lc rgb "olive"
set style line 9 lt 1 ps 0 lc rgb "violet"
set style line 10 lt 1 ps 0 lc rgb "pink"
plot for [i=1:PlotCount] File(i) u "time":Col(i) w lp ls Style(i) title File(i)
pause -1
### end of code
From the gnuplot console type:
call "myScript.gp" "test1 test2" "COL1 COL2" "1 6"
or in your operating system:
gnuplot.exe -c "myScript.gp" "test1 test2" "COL1 COL2" "1 6"
来源:https://stackoverflow.com/questions/61611937/change-plot-type-with-lines-with-points-recursively