gnuplot / datafile (3)

データファイルの数値のプロット (その3)

UNIXのコマンドを利用する.

gnuplotはデータファイルの中身を加工しな
がらプロットする
ことができますが,gnuplotだけでは限界があります.
例えば,X座標がa.dat,Y座標はb.datというように,データが複数のファ
イルに分れていたり,特定の数値だけを修正するような場合です.あら
かじめデータファイルをエディタで編集してgnuplot 用のデータを用意す
るのが本筋ですが,ここではUNIXのコマンドを利用する方法を幾つか紹介
します.

ここで取り上げているのは,以下のコマンドです.

sort

sortコマンドを使って,複数のファイルを連結して混ぜ合わせる
ことができます.

file1.dat file2.dat
1.0   0.1
2.0   0.2
5.0   0.5
8.0   0.8
5.0   1.0
10.0   1.2
12.0   1.3
% sort -n file1.dat file2.dat
1.0   0.1
2.0   0.2
4.0   0.6
5.0   0.5
5.0   1.0
8.0   0.8
10.0   1.2
12.0   1.3

2つのデータファイルを,それぞれ別のシンボルで表示した後,それら全部
を繋ぐ線を引きます.

gnuplot> plot "< cat -n file1.dat file2.dat" using 1:2  w l,
>             "file1.dat" u 1:2 w p,
>             "file2.dat" u 1:2 w p
fig/sample7.7a

paste

paste コマンドは2つ以上のファイルを横に並べて書き出します.

file1.dat file2.dat
1.0   0.1
2.0   0.2
3.0   0.5
4.0   0.8
1.0   0.6
2.0   1.0
3.0   1.2
4.0   1.3
% paste file1.dat file2.dat
1.0   0.1       1.0   0.6
2.0   0.2       2.0   1.0
3.0   0.5       3.0   1.2
4.0   0.8       4.0   1.3

file1.datのX座標とfile2.datのY座標を,(X,Y)として表示するには,次の
ようにします.

gnuplot> plot "< paste file1.dat file2.dat" using 2:4  w lp

このコマンドを使って,2つのファイルに入っているデータの差や比を表示し
ます. paste コマンドの出力では,file1.datのY座標が2カラム目,
file2.datのY座標は4カラム目となりますので,2つの数値の差は
$2-$4,比は $2/$4 と表されます.

gnuplot> plot "< paste file1.dat file2.dat" using 1:($2/$4) w points
from Ken. Thanks !

sed

ストリームエディタsedを使と,大概のテキスト処理ができます.
ここではsedの機能を全て網羅することはとてもできませんので,簡単な例
だけを紹介します.データファイルは,上の例のfile1.datを用います.

X座標の1.0を1.2にずらす.

gnuplot> plot '<sed "s/^   1.0/   1.2/g" file1.dat'

全部の点を表示するが,(2.0,0.2)の点だけ通らない線を描く.

gnuplot> plot 'file1.dat'                   u 1:2 with points,
>             '<sed "/^   2.0/d" file1.dat' u 1:2 with lines
fig/sample7.7b

3行目のデータだけを削除する.

gnuplot> plot '<sed "3d" file1.dat' u 1:2 with points

awk

awk も非常に協力な言語で,データファイルの各カラムに対して
種々の操作を行えます.gnuplotで利用する方法は多々ありますが,要は
awkコマンドの出力がgnuplotで表示できるような,XとYの2カラムになって
いれば良いわけです.

Yデータの累積値を棒グラフで表示する.Yの値を順次加えていきます.最後
の値が,Yの和になります.

gnuplot> plot "<awk '{x=x+$2; print $1,x}' file1.dat" with boxes
fig/sample7.7c

Y座標が0.2より小さいときは,ゼロにする.

gnuplot> plot  "<awk '{print $1,($2<0.1) ? 0.0 : $2}' file1.dat" with lines

Xデータが[1:3]の間だけ,Y値を5倍する.

gnuplot> plot  "<awk '{print $1,($1<=3 && $1>=1) ? $2*5 : $2}' file1.dat" with lines
up