bash で gnuplot プロットを自動化する

bash で gnuplot プロットを自動化する

誤差範囲付きの折れ線グラフとしてプロットし、異なる png ファイルに出力する必要があるファイルが 6 つあります。ファイル形式は次のとおりです。

秒 平均 最小 最大

これらのグラフを自動的にプロットするにはどうすればよいでしょうか。bash.sh というファイルを実行すると、6 つのファイルが取得され、グラフが別の.pngファイルに出力されます。タイトルと軸ラベルも必要です。

答え1

私が正しく理解していれば、あなたが望んでいるのは次のようなものです:

for FILE in *; do
    gnuplot <<- EOF
        set xlabel "Label"
        set ylabel "Label2"
        set title "Graph title"   
        set term png
        set output "${FILE}.png"
        plot "${FILE}" using 1:2:3:4 with errorbars
EOF
done

これは、ファイルがすべて現在のディレクトリにあることを前提としています。上記はグラフを生成する bash スクリプトです。個人的には、通常、gnuplot_in何らかの形式のスクリプトを使用して、各ファイルに対して上記のコマンドを含む gnuplot コマンド ファイル ( など) を作成し、 を使用してプロットしますgnuplot < gnuplot_in

Python の例を挙げると次のようになります。

#!/usr/bin/env python3
import glob
commands=open("gnuplot_in", 'w')
print("""set xlabel "Label"
set ylabel "Label2"
set term png""", file=commands)

for datafile in glob.iglob("Your_file_glob_pattern"):
    # Here, you can tweak the output png file name.
    print('set output "{output}.png"'.format( output=datafile ), file=commands )
    print('plot "{file_name}" using 1:2:3:4 with errorbars title "Graph title"'.format( file_name = datafile ), file=commands)

commands.close()

ここでYour_file_glob_pattern、 はデータファイルの名前を記述するもので、 または です*。モジュール*datの代わりに、ももちろんglob使用できます。 実際には、ファイル名のリストを生成するものなら何でもかまいません。os

答え2

一時コマンド ファイルを使用した Bash ソリューション:

echo > gnuplot.in 
for FILE in *; do
    echo "set xlabel \"Label\"" >> gnuplot.in
    echo "set ylabel \"Label2\"" >> gnuplot.in
    echo "set term png" >> gnuplot.in
    echo "set output \"${FILE}.png\" >> gnuplot.in
    echo "plot \"${FILE}\" using 1:2:3:4 with errorbars title \"Graph title\"" >> gnuplot.in
done
gnuplot gnuplot.in

答え3

これが役に立つかもしれません。

#set terminal postfile       (These commented lines would be used to )
#set output  "d1_plot.ps"    (generate a postscript file.            )
set title "Energy vs. Time for Sample Data"
set xlabel "Time"
set ylabel "Energy"
plot "d1.dat" with lines
pause -1 "Hit any key to continue"

スクリプト ファイルを次のように実行しますgnuplot filename

詳細についてはここをクリックしてください。

関連情報