在gnuplot中循环遍历数组

30
这个问题与在gnuplot中使用循环结构?DarioP答案有关。
gnuplot 4.6引入了do命令。我如何使用它来循环处理文件和颜色等数组?正确的语法是什么?
colors = "red green #0000FF"
files = "file1 file2 file3"

do for [i=1:3] {
  plot files(i).".dat" lc colors(i)
}
1个回答

48
如果您想在一个图中拥有所有的文件,您需要使用plot for[...(从4.4版本开始支持)。循环多个plot命令并使用do for(仅从4.6版本开始支持)只能在multiplot模式下工作。
以下两种解决方案都能将所有数据绘制到一个图形中,但在迭代上略有不同。
第一种解决方案使用word直接从字符串提取一个单词进行绘制。
colors = "red green #0000FF"
files = "file1 file2 file3"
plot for [i=1:words(files)] word(files, i).'.dat' lc rgb word(colors, i)
第二种解决方案是改变linetype,然后直接遍历单词列表,而不是使用索引。
colors = "red green #0000FF"
files = "file1 file2 file3"
set for [i=1:words(colors)] linetype i lc rgb word(colors, i)
plot for [file in files] file.'.dat'

我刚刚才有时间测试它。我更喜欢你的第二个解决方案,它不需要多图模式。它很有效。非常漂亮的解决方案。尽管我已经使用gnuplot十多年了,但我之前并不知道“words”和“word”。谢谢你。 - tommy.carstensen
1
@tommy.carstensen,你的 do for .. plot 方法只适用于 multiplot 模式。我的两个解决方案都使用 plot for [... 并生成单个图形。我重新表述了我的答案以澄清这一点。 - Christoph
3
谢谢您的澄清。我还了解到,对于那些想要更深入学习此内容的人,“word”和“words”在字符串变量演示中有所涉及:http://gnuplot.sourceforge.net/demo/stringvar.html - tommy.carstensen

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接