gnuplot 可以在一个命令中处理任意数量的文件。
只需发出您的 plot 命令,用逗号分隔每个文件和列规范(就像您在上面开始做的那样)
plot \
"folder1/data" using 1:2,
"folder1/data" using 1:3,
"folder2/data" using 1:2,
"folder2/data" using 1:3,
"folder3/data" using 1:2,
"folder3/data" using 1:3
通常每个plot 命令都会在单独的窗口中产生输出(这取决于gnuplot 的调用方式——无论是否在单独的进程中)。
当从 shell 脚本调用时,您可以生成单独的子 shell 以在单独的窗口中生成输出,或者在 C 程序中,您可以 fork 为同一目的单独的进程。
你也可以使用multiplot,见Multiplot – placing graphs next to each other « Gnuplotting
您还可以在这里找到部分问题的答案gnuplot : plotting data from multiple input files in a single graph
根据评论编辑
好的,现在我知道你想从当前目录下名为folderXX(其中XX可以是任何东西)的目录列表自动构建一个绘图文件,并且在每个文件夹中都会有一个名为的文件data 您想在第 2 列和第 3 列中分别绘制第一个,然后您可以通过循环遍历 folderXX 列表来构建一个临时绘图文件(例如 tmp.plt)(您可以将 globbing 调整为您的需要)并使用简单的重定向输出绘图命令。
例如,如下所示:
#!/bin/bash
## truncate tmp.plt and set line style
echo -e "set style data lines\nplot \\" > tmp.plt
cnt=0 ## flag for adding ',' line ending
## loop over each file
for i in folder*/data; do
if ((cnt == 0)); then ## check flag (skips first iteration)
cnt=1 ## set flag to write ending comma
else
printf ",\n" >> tmp.plt ## write comma
fi
printf "\"$i\" using 1:2,\n" >> tmp.plt ## write using 1:2
printf "\"$i\" using 1:3" >> tmp.plt ## write using 1:3 (no ending)
done
echo "" >> tmp.plt ## write final newline
(注意: echo 和 printf 交替使用以获得行继续所需的转义行为)
在包含folder1, folder2, folder3, folder4 的目录中运行时,每个目录都包含一个data 文件,例如
$ tree
.
├── folder1
│ └── data
├── folder2
│ └── data
├── folder3
│ └── data
├── folder4
│ └── data
它会生成一个tmp.plt 绘图文件,然后可以使用gnuplot -p tmp.plt 调用该绘图文件,例如
$ cat tmp.plt
set style data lines
plot \
"folder1/data" using 1:2,
"folder1/data" using 1:3,
"folder2/data" using 1:2,
"folder2/data" using 1:3,
"folder3/data" using 1:2,
"folder3/data" using 1:3,
"folder4/data" using 1:2,
"folder4/data" using 1:3
根据您的评论,这应该更接近您正在寻找的内容。