【问题标题】:gnuplot : setting line style in a for loopgnuplot:在 for 循环中设置线条样式
【发布时间】:2020-12-20 16:35:12
【问题描述】:
我必须在同一张图上绘制多条曲线。我必须使用 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)
我想在前两条曲线前加上“wl”,在其他曲线前加上“ls i” .我尝试通过将“ls i”替换为“if (i ”来使用 if 语句,但 Gnuplot 没有期望在这个位置找到一个 if 语句。
有人可以帮助我吗?
谢谢,
马丁
【问题讨论】:
标签:
for-loop
gnuplot
linestyle
【解决方案1】:
这里是使用宏的一种方式:
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
【解决方案2】:
如here 所述,您可能无法在plot for 循环内切换绘图样式。
所以,要么你做两个单独的循环,一个 with points 和另一个 with lines 或者你做一个循环 with linespoints 并将点和线的所有必要参数定义为函数(以保持绘图命令可读)。
如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
结果:
加法:
为了使绘图命令尽可能简短和清晰,您还可以定义线条样式并通过ls i 在plot for 命令中使用它,结果与上述相同。
...
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)