问题描述
我必须在同一张图上绘制几条曲线。我需要使用for循环来执行此操作。我想用直线绘制第二条第一曲线,并用点绘制其他曲线。我能够绘制所有带线的曲线或所有带点的曲线,但不能在同一for循环中更改。
这是我的代码的相关部分:set style line 1 lw 1 lc rgb "green"
set style line 2 lw 1 lc rgb "purple"
set style line 3 pt 1 ps 1.0 lc rgb "red"
set style line 4 pt 2 ps 1.0 lc rgb "red"
set style line 5 pt 3 ps 1.0 lc rgb "red"
plot for [i=1:words(FILES)] myDataFile(i) u (column(1)):((word(UTAUS_ch,i))) ls i title myTitle(i)
我想在“ ls i ”的前面加上“ wl ”作为其他两条曲线的前面,而“ ls i ”是其他曲线的开头。我尝试通过将“ ls i ”替换为“ if(i ”来使用if语句,但是Gnuplot却没有希望在此位置找到一个if语句。
有人可以帮助我吗? 谢谢, 马丁
解决方法
如here所述,您可能无法在plot for
循环内切换绘图样式。
因此,您可以做两个单独的循环,一个with points
,另一个with lines
,或者您做一个循环with linespoints
,并将点和线的所有必要参数定义为函数(以保持plot命令可读)。
如here所述,linewidth 0
不是零,而是可能的最细线,通常为1个像素。要使行完全消失,您必须使用linetype -2
。
代码:
### lines and points in the same plot for-loop
reset session
LINECOLORS = "red green blue magenta cyan"
LINEWIDTHS = '1.0 4.0 0.0 0.0 0.0'
POINTTYPES = '0 0 5 7 9'
POINTSIZES = '0 0 1.0 2.0 3.0'
TITLES = 'one two three four five'
myLinecolor(i) = word(LINECOLORS,i)
myLinewidth(i) = real(word(LINEWIDTHS,i))
myPointtype(i) = int(word(POINTTYPES,i))
myPointsize(i) = real(word(POINTSIZES,i))
myLinetype(i) = myLinewidth(i) == 0 ? -2 : 1
myTitle(i) = word(TITLES,i)
set samples 31
set key out
plot for [i=1:words(TITLES)] (sin(0.25*x-i)) w lp pt myPointtype(i) ps myPointsize(i) \
lt myLinetype(i) lw myLinewidth(i) lc rgb myLinecolor(i) title myTitle(i)
### end of code
结果:
添加:
为了使plot命令尽可能简短明了,您还可以定义线条样式,并通过plot for
在ls i
命令中使用它,结果与上面相同。
...
do for [i=1:words(TITLES)] {
set style line i pt myPointtype(i) ps myPointsize(i) \
lt myLinetype(i) lw myLinewidth(i) lc rgb myLinecolor(i)
}
plot for [i=1:words(TITLES)] (sin(0.25*x-i)) w lp ls i title myTitle(i)
,
以下是使用宏的方法:
set style line 1 lw 1 lc rgb "green"
set style line 2 lw 1 lc rgb "purple"
set style line 3 pt 1 ps 1.0 lc rgb "red"
set style line 4 pt 2 ps 1.0 lc rgb "red"
set style line 5 pt 3 ps 1.0 lc rgb "red"
set samp 100
set macro
cmd = ''
do for [i=1:10] {s = i<3? 'w l' : 'ls '.i;
cmd = cmd . '"+" us 1:(sin(x-'.i.'/10.)) '.s.' title "key '.i.'",'}
plot [0:2*pi] @cmd