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

2020年度 大気科学演習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> f(x)=2*x+1
gnuplot> plot f(x)

これで,2x + 1 のグラフを描くことができる.

同じものを,

gnuplot> g(x)=a*x+b
gnuplot> a=2
gnuplot> b=1
gnuplot> plot g(x)

とやって,描くこともできる. この後,b の値を変更した場合の図を描きたくなったら

gnuplot> b=2
gnuplot> plot g(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 exp(x), exp(-x*x)

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

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

データは /work2/atmos/data/gnuplot/data.txt を使う. まずデータを確認する. gnuplot を起動しているなら

gnuplot> exit

で,gnuplot を終了し,プロンプトを $ にする.

データファイルの中身を表示して,3列のデータであることを確認する.

$ cat /work2/atmos/data/gnuplot/data.txt
-2  4  -8
-1  1  -1
 0  0   0
 1  1   1
 2  4   8
 3  9  27

gnuplot を起動

$ gnuplot

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

gnuplot> plot '/work2/atmos/data/gnuplot/data.txt' 

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

ファイルの絶対パスをいちいち書くのは面倒だという場合は,gnuplot を終了して

gnuplot> exit

ディレクトリを移動してから gnuplot を起動して図を描く

$ cd /work2/atmos/data/gnuplot
$ gnuplot
gnuplot> plot 'data.txt' 

パスを書かなければカレントディレクトリにあるファイルを使って図が描かれる.

線と点

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

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 を使う. 例えば,1列目をx軸,3列目をy軸にするときは,

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

ちなみに,3列からなるデータは,1列目を x としたら, 2列目は xの2乗,3列目は xの3乗,である.

データが x の3乗の線の上に乗っていることを確認する. データは点で表示することにして,

gnuplot> plot 'data.txt' using 1:3 pointsize 3, x**3

3列目をx軸,2列目をy軸にするなら,

gnuplot> plot 'data.txt' using 3:2 pointsize 3

これはxの2/3乗の線の上に乗る.

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: 2020/11/05, Since: 2019/11/07.
This page is generated by Makefile.rd2html.