[ 岡山大学 | 理学部 | 地球科学科 | 地球および惑星大気科学研究室 ]

2019年度 大気科学演習1

gnuplot

gnuplotはグラフ作成ツール. フリーウェアとして公開されている. 学術論文に使えるレベルのグラフを作成することができる. PNG, EPS, JPEG などの形式で画像を作成することができる.

シェルのコマンドラインと同じようにして,コマンドを打ち込んで図を描く. シェルのシェルスクリプトと同様に,コマンドをファイルに書いておいて,まとめて実行することもできる.

gnuplotを使うといろいろなことができる. gnuplotの使い方はウェブで検索したらいろいろ見つかるので,必要に応じて調べたらよい.

起動と終了

起動するには

$ gnuplot

と打つ. 正しく起動すればプロンプトが

gnuplot>

に変わる.

終了するときは

gnuplot> exit

とする. 終了したらプロンプトが

$

に戻る.

式を図にしてみる

y=sin(x)を図にしてみる.

gnuplot> plot sin(x)

新しいウインドウが開いて図が表示される(新しいウインドウが開くとき,ウインドウの位置を確定させるためにマウスをクリックする必要があるかもしれない).

2つの式を重ねて表示するなら,

gnuplot> plot sin(x), x

のように「,」でつなぐ.

あるいは,ひとつずつ描く.

gnuplot> plot sin(x)
gnuplot> replot x

3つ以上も可

gnuplot> plot sin(x), sin(x/2), sin(x/4)

あるいは

gnuplot> plot sin(x)
gnuplot> replot sin(x/2)
gnuplot> replot sin(x/4)

割り算はちょっと気をつける

gnuplot> plot (4/2)*sin(x)

これは問題ないが,次はうまくいかない.

gnuplot> plot (2/4)*sin(x)

2/4 は整数として計算され,2/4 = 0 あまり 2 より 2/4 = 0 が代入される. 数字に小数点をつけると, 2./4. = 0.5 と計算してくれる

gnuplot> plot (2./4.)*sin(x)

描画範囲

描画範囲を指定する

gnuplot> set xrange [-3:3]
gnuplot> set yrange [-2:2]
gnuplot> plot sin(x) 

あるいは,以下のようにして指定することも可能

gnuplot> plot [-6:6][-2:2] sin(x)

反転

軸を反転させるとき

gnuplot> set xrange [] reverse
gnuplot> plot exp(x)

元に戻す

gnuplot> unset xrange
gnuplot> plot sin(x)

描画範囲の指定を使っても軸を反転させることができる

gnuplot> set xrange [3:-3]
gnuplot> plot sin(x)

対数

まず普通に描いてみる

gnuplot> unset xrange
gnuplot> set xrange [-2:2]
gnuplot> set autoscale
gnuplot> plot exp(x), exp(-x*x)

対数を使う

gnuplot> set logscale y
gnuplot> plot exp(x), exp(-x*x)

元に戻す

gnuplot> unset logscale
gnuplot> plot plot exp(x), exp(-x*x)

ファイルに書き込まれたデータを図にする

ファイルに書かれたデータを図にしてみる.

まずデータを作る.いったん gnuplot を終了して,エディタを使って,以下を書き込んで,data.txt という名前で保存する.

-2  4  -8
-1  1  -1
 0  0   0
 1  1   1
 2  4   8
 3  9  27

ファイルを読み込んで図を描いてみる

gnuplot> plot 'data.txt' 

データファイルの1列目を図のx軸,2列目を図のy軸にして図が描かれる.

線と点

グラフを線で表示したい場合には

gnuplot> plot 'data.txt' with lines

グラフを点で表示したい場合には

gnuplot> plot 'data.txt' with points

点と線の両方で表示したいときには

gnuplot> plot 'data.txt' with linespoints

点の種類を変える

gnuplot> plot 'data.txt' with points pointtype 2
gnuplot> plot 'data.txt' with points pointtype 4
gnuplot> plot 'data.txt' with points pointtype 7

点の大きさを変える

gnuplot> plot 'data.txt' with points pointtype 7 pointsize 3

線の太さを変える

gnuplot> plot 'data.txt' with lines linewidth 3

いろいろな方法があるが,とりあえず簡単な方法

gnuplot> plot sin(x) linecolor rgb 'blue'

どんな色が使えるのかは,検索したらわかるでしょう.

点の色も linecolor で指定する

gnuplot> plot sin(x) with points pointtype 7 linecolor rgb 'orange'

列を指定する

列を指定して描画するときは,using を使う. 例えば,3列目をx軸,2列目をy軸にするときは,

gnuplot> plot 'data.txt' using 3:2 with linespoints

ちなみに,3列からなるデータは,x, xの2乗,xの3乗,である. using 3:2 で xの3乗を横軸,xの2乗を縦軸にとれば,データはxの2/3乗の線の上に載るはずである. 試してみると

gnuplot> plot 'data.txt' using 3:2 pointsize 3, x**(2.0/3.0)

using は0列目を指定することもできて,0列目を指定すると行番号が使われる.

gnuplot> plot 'data.txt' using 0:1

行番号は0から始まる. すなわち,1行目は0,2行目は1,...

2列目と3列目を表示するなら

gnuplot> plot 'data.txt' using 1:2 with linespoints, 'data.txt' using 1:3 with linespoints

演算

2列目の値を 5 倍して +7 する

gnuplot> plot 'data.txt' using 1:($2*5+7)

割り算は注意

gnuplot> plot 'data.txt' using 1:((2/3)*$2)
gnuplot> plot 'data.txt' using 1:((2./3.)*$2)

2列目の平方根(0.5乗)

gnuplot> plot 'data.txt' using 1:(($2)**0.5)

横は 1 列目の cosine,縦は 1 列目の sine

gnuplot> plot 'data.txt' using (cos($1)):(sin($1))

複数の列を混ぜることも

gnuplot> plot 'data.txt' using 1:2, x**2
gnuplot> plot 'data.txt' using 1:3, x**3
gnuplot> plot 'data.txt' using 1:($2+$3), x**2+x**3

軸のラベル

x軸やy軸にラベルをつける

gnuplot> set xlabel 'time (s)'
gnuplot> set ylabel 'velocity (m/s)'
gnuplot> plot 'data.txt' using 0:2 with linespoints

図の大きさの指定

正方形にするとき

gnuplot> set size square

縦を横の0.5倍にする

gnuplot> set size ratio 0.5




Last Updated: 2019/11/14, Since: 2019/11/07.
This page is generated by Makefile.rd2html.