2017年10月24日火曜日

gnuplotで.pltファイルを読み込み自動出力するサンプル

output.txtからデータ出力するサンプルです.

$> file = 'output'
$> output = 1
$> load "plotter.plt"

  • グラフの大きさをmarginで指定する
  • 軸ラベルをつける
  • グリッドを表示する
  • 凡例を移動させる
  • 線のスタイルを変更する
  • multiplotによって1つの図に2つのグラフを表示する
  • if文処理により,変数output=1にセットした時のみ.epsファイルを出力する
  • 参照ファイル名を用いて出力ファイルを作成する
等の内容を含みます.

plotter.pltの中身は以下の通りです.

# plotter.plt

set terminal wxt 0
reset
set lmargin 9
set rmargin 2
set xlabel "Time [sec]"
set ylabel "Position [m]"
set grid
set key left bottom
p [:][-2.5:2.5] file.".txt" u ($1):($4) w l lw 4 lc 2 title "Measured X", "" u ($1):($2) w l lw 3 lc 3 title "Desired X"

if(exist("output")&& output == 1)\
set terminal postscript eps color enhanced "Arial,20";\
set output file."_X.eps";\
p [:][-2.5:2.5] file.".txt" u ($1):($4) w l lw 4 lc 2 title "Measured X", "" u ($1):($2) w l lw 3 lc 3 title "Desired X";\

set terminal wxt 1
set key left bottom
p [:][-2.5:2.5] file.".txt" u ($1):($5) w l lw 4 lc 2 title "Measured Y", "" u ($1):($3) w l lw 3 lc 3 title "Desired Y"

if(exist("output")&& output == 1)\
set terminal postscript eps color enhanced "Arial,20";\
set output file."_Y.eps";\
p [:][-2.5:2.5] file.".txt" u ($1):($5) w l lw 4 lc 2 title "Measured Y", "" u ($1):($3) w l lw 3 lc 3 title "Desired Y";\

set terminal wxt 2
set multiplot layout 2,1
unset xlabel
set format x ""
set key left bottom
set tmargin screen 0.99
set bmargin screen 0.585
p [:][-2.5:2.5] file.".txt" u ($1):($4) w l lw 4 lc 2 title "Measured X", "" u ($1):($2) w l lw 3 lc 3 title "Desired X"
set format x
set xlabel "Time [sec]"
set key left bottom
set tmargin screen 0.535
set bmargin screen 0.13
p [:][-2.5:2.5] file.".txt" u ($1):($5) w l lw 4 lc 2 title "Measured Y", "" u ($1):($3) w l lw 3 lc 3 title "Desired Y"
unset multiplot

if(exist("output")&& output == 1)\
set terminal postscript eps color enhanced "Arial,20";\
set output file."_Layout.eps";\
set multiplot layout 2,1;\
unset xlabel;\
set format x "";\
set key left bottom;\
set tmargin screen 0.99;\
set bmargin screen 0.585;\
p [:][-2.5:2.5] file.".txt" u ($1):($4) w l lw 4 lc 2 title "Measured X", "" u ($1):($2) w l lw 3 lc 3 title "Desired X";\
set format x;\
set xlabel "Time [sec]";\
set key left bottom;\
set tmargin screen 0.535;\
set bmargin screen 0.13;\
p [:][-2.5:2.5] file.".txt" u ($1):($5) w l lw 4 lc 2 title "Measured Y", "" u ($1):($3) w l lw 3 lc 3 title "Desired Y";\
unset multiplot;\

undefine output
set terminal wxt 0