Gnuplot 自动绘制所有数据文件

Gnuplot plotting all datafiles automatically

我有多个 *.data 文件,每个文件都有相同的格式,可以用这个简单的脚本绘制:

cat << __EOF | gnuplot -persist
set terminal pdf
set output 'out.pdf'
set datafile separator ";"
set boxwidth 0.5
set style fill solid
plot "xxx.dat" using 1:3:xtic(2) with boxes
__EOF

如何自动将所有 *.dat 文件的图表制作成一个 pdf?如果无法将所有图表绘制到一个文件中,单独的 pdf 就足够了。

示例 .dat 文件:

0;name1;150
1;name2;65
2;name3;81

要创建多个 pdf 文件,运行 这个脚本与 *.dat 文件在同一路径中对我有用 (确保 .dat 文件名):

#!/bin/bash

while IFS= read -r in; do
    out=${in/%.dat/.pdf}
    echo "Converting $in into $out"
    cat << __EOF | gnuplot -persist
    set terminal pdf
    set output "$out"
    set datafile separator ";"
    set boxwidth 0.5
    set style fill solid
    plot "$in" using 1:3:xtic(2) with boxes
__EOF
done < <(ls | grep "\.dat$")

要将所有图表附加到单个 pdf,您必须对 gnuplot 脚本内的文件进行迭代,并在循环之前设置输出文件:

这是 gnuplot 脚本 iterate.gp

set terminal pdf
set output 'out.pdf'
set datafile separator ";"
set boxwidth 0.5
set style fill solid
files = system('ls *.dat')
do for [file in files] {
    set title file[:strlen(file)-4]
    plot file using 1:3:xtic(2) with boxes
}

调用
gnuplot iterate.gp

请注意,使用此解决方案,您的数据文件中不能有空格。