如何在Gnuplot中实现类似Python zip函数的双变量迭代?
Gnuplot实现类似Python zip的双变量遍历
Gnuplot本身没有像Python for i,j in zip(l1,l2) 这样直接的双变量并行遍历语法,但可以通过将文件列表转为数组,按索引配对的方式实现需求。
问题分析
你尝试的do for [i,j in ...]写法不符合Gnuplot语法规范,这种双变量同时遍历的用法不被支持。我们可以先把两个文件列表分别存入数组,再通过索引循环来一一配对。
解决方案代码
# 获取两个文件列表,转为字符串数组 file_list = system("ls -1 file*.dat") file_t_list = system("ls -1 file_t*.dat") # 获取列表长度(需保证两个列表文件数量一致) n = words(file_list) do for [idx=1:n] { # 按索引取出配对的两个文件 i = word(file_list, idx) j = word(file_t_list, idx) set term pngcairo size 1400,800 enhanced font "Times New Roman,22" set output i.'.png' plot \ i u 1:2:3 w err ps 0 lw 2 lc rgb 'red' title i, \ j u 1:2:3 w err ps 0 lw 2 lc rgb 'green' title j }
代码说明
words(str):统计字符串中的单词数(此处每个文件名以换行分隔,作为一个"单词")word(str, idx):取出字符串中第idx个单词(Gnuplot索引从1开始)- 通过索引
idx循环,每次取出两个列表对应位置的文件,实现并行遍历
原单变量可行代码参考
你之前实现的单变量遍历代码如下:
j=0; do for [i in system("ls -1 file*.dat")] { j=j+1; set term pngcairo size 1400,800 enhanced font "Times New Roman,22"; set output ''.i.'.png' ; p i u 1:2:3 w err ps 0 lw 2 lc rgb 'red' t ''.i}
内容的提问来源于stack exchange,提问作者Elena Greg
相关产品推荐
相关产品推荐

