Gnuplot の使い方 (2次元表示のみ)


目次

起動と終了
グラフの描画
グラフのカスタマイズ
複数のグラフを表示
ポストスクリプトに出力
関数のフィッティング

起動と終了

% gnuplot
と入力すると起動し、gnuplot のプロンプト

gnuplot> 
が現れる。
終了するには、以下のどれかを入力する。
gnuplot> exit
gnuplot> quit
gnuplot> q


グラフの描画

基本的に、
gnuplot> plot  function  with  linestyle  [,  function  with  linestyle  ]
で図を作成する。例えば、sin(x) 関数を線画で表示する場合は、
gnuplot> plot sin(x) with lines
とする。2つ以上の関数を重ねて表示したい場合は、カンマで区切り、
gnuplot> plot sin(x) with lines, -2.0*x/3.0+1 with points
2つ以上のグラフを表示するにはこちら


linestyle としては、
points点 (デフォルト)
lines
linespoints線と点
dots小さな点
boxes棒グラフ
等がある。


function に、データファイルを指定することができる。 例えば、以下のようなファイルを用意する。
% cat test.dat
# Test data
 0.31 1.2 -0.66
 0.35 2.7 -0.76
 0.45 3.7 -0.64
 0.42 4.4 -0.52

 0.50 5.2 -0.48
 0.53 6.3 -0.50
 0.63 7.6 -0.06
フォーマットは自由で、各データ列はスペースで区別される。 # で始まる行は無視される。間に空行があっても良い。 このデータの1列目を Y軸、2列目を X軸としたグラフを線画で書くと

gnuplot> plot "test.dat" using 2:1 with linespoints

となる。軸の範囲は自動的に決定される。また、空行は線でつながれない。
ファイルの指定は、"file_name" で行う。
軸の指定は using X軸:Y軸 で行う。


グラフのカスタマイズ

基本的に、
gnuplot> set option 
で指定する。ここに紹介しなかったオプションは、
gnuplot> help set
で一覧表示される。元に戻すには、option を引数なしで書く。
set コマンドでカスタマイズした後に、 plot コマンドで再描画させる必要がある。

軸範囲の指定
set xrange [Xmin:Xmax]
set yrange [Ymin:Ymax]
例1: gnuplot> set xrange [-pi:2.0*pi] 例2: gnuplot> plot [Xmin:Xmax] [Ymin:Ymax] sin(x) with linespoins 例3: gnuplot> plot [] [Ymin:Ymax] sin(x) with linespoins
set を使うと、その後の全てのプロットに対して適用されるが、 plot の後に[*:*] で指定すれば、1回限りの指定となる。

軸名の指定
set xlabel "label name on X"
set ylabel "label name on Y"
y軸が正しく表示されない場合があるが、以下に示すような ポストスクリプトに出力した場合は正しく表示されている。

グラフ名の指定
set title "name"

線の名前
plot  function  title "name"

軸の目盛
set xtics 最小値, 増分, 最大値
例: -10から、20まで 2刻み。
set xtics -10, 2, 20

もしくは、
set xtics ( 0.0, 7.0, 12.8, 20.0 )
とすると、つけたい 場所にだけ、tic が入る。 また、tic の場所に、任意の名前を入れるには、
set xtics ( "origin" 0.0, "next" 7.0, "{/Symbol p}" 20.0 )
とすれば、origin が 0.0 の位置に、というようにつけらる。 {/Symbol p} は、ギリシャ文字に変換する (ポストスクリプトに出力した場合のみ)。 数字を書式付きで表示するには、C言語と同じく
set format x "%4.2f"
tic のみで数字を入れない場合は、
set format y ""
とする。

log scale
X軸を対数表示に
set log x 
X軸の対数表示をやめる
set nolog x 

グリッドを表示
X軸にグリッドを入れる
set grid x 
グリッドを全て取る
set nogrid


複数のグラフを表示。

multiplot モードに変換する。
gnuplot> set multiplot
2つグラフを入れる場合
2つ入れるため、それぞれのグラフのサイズを小さくする。
横(X)方向は、元の 0.8倍に、縦(Y)方向は、元の 0.4 倍にする。
multiplot> set size 0.8,0.4
1つめのグラフの左下部分の位置を指定。
横(X)方向の紙面サイズの 0.1 倍の位置
縦(Y)方向の紙面サイズの 0.1 倍の位置
multiplot> set origin 0.1,0.1
1つめのグラフを表示。
multiplot> plot [-2.0*pi:2.0*pi] [-2:2] 1.5*sin(x) title "first.plot"
2つめのグラフの左下部分の位置を指定。
横(X)方向の紙面サイズの 0.1 倍の位置
縦(Y)方向の紙面サイズの 0.45 倍の位置
multiplot> set origin 0.1,0.45
2つめのグラフを表示。
multiplot> plot [-2.0*pi:2.0*pi] [-2:2] 1.5*cos(x) title "second.plot"
multiplot モードを終える。
multiplot> set nomultiplot

ポストスクリプトファイルへの出力

gnuplot> set terminal postscript style fontsize
とする。出力ファイル名は、

gnuplot> set output "file_name"
で指定する。 style には、
landscape横長
portrait縦長
epsEPS 形式
等がある。 fontsize には、20等のポイント数を入力する。
例:
gnuplot> set terminal postscript landscape 20
gnuplot> set output "test.ps"
gnuplot> plot sin(x)
ギリシャ文字を使う場合は、style に enhanced を付け加える。
gnuplot> set terminal eps enhanced 20


関数のフィッティング

任意の関数を、データに対してフィッティングさせて 係数を求めることができる。

関数の定義:
gnuplot> f(x) = a*x + b
フィッティング:
aとb を使って、test.dat というファイルの2列目と1列目のデータを使い、 X軸の範囲として、-10 から 20 の間のデータのみを使って関数 f(x) を フィットさせる
gnuplot> fit [-10:20] f(x) "test.dat" using 2:1 via a,b
確認:
gnuplot> plot "test.dat" using 2:1, f(x)