gnuplotの使い方

コンピュータ工学基礎演習 2014
gnuplotの使い方
機能創成専攻 生体工学領域 井村 誠孝
[email protected]
講義資料: http://bit.ly/bpe-ecp14
準備: 計算結果のファイルへの出力
 2通りの方法
 方法1: リダイレクトを用いる.
 プログラム中では printf で出力する.
 プログラムの実行時に,リダイレクト先のファイルを指定して,
画面への出力をファイルに保存する.
画面に出力
$ ./program1
$ ./program1 > data1.txt
 方法2: ファイル入出力関数を用いる.
ファイル data1.txt
に出力
 fopenでファイルを開く.
 fprintf でファイルに書き込む.
 fclose でファイルを閉じる.
2014/6/30
>リダイレクトを活用しよう
Exercises in Computer Programming
2 / 12
講義資料: http://bit.ly/bpe-ecp14
gnuplotの使用
 gnuplotで何ができるか
 関数の式を与えてグラフを描画する (解析解用)
 データファイルを与えてグラフを描画する (数値解用)
 gnuplotの起動
 まず端末でデータのあるディレクトリに移動
 cd ディレクトリ名
 カレントディレクトリを確認するコマンド: pwd
 端末で gnuplot
 終了は quit もしくは exit
2014/6/30
Exercises in Computer Programming
3 / 12
講義資料: http://bit.ly/bpe-ecp14
関数の式を与えてグラフを描画
 独立変数(横軸)は常に x である.
 関数 =
y x 2 − 1 を描画
 plot x**2-1
ベキ乗は **
 複数の関数 y =x 2 − 1, y =x を同時に描画
 plot x**2-1, x
2014/6/30
Exercises in Computer Programming
4 / 12
講義資料: http://bit.ly/bpe-ecp14
データファイルの形式
 ファイルには以下の形式でデータを格納しておく.
 各行が1組のデータ
 数値の間には空白
 データファイルの例
0.00100000 0.99999950
0.00200000 0.99999800
0.00300000 0.99999550
0.00400000 0.99999200
0.00500000 0.99998750
0.00600000 0.99998200
2014/6/30
-0.00100000
-0.00200000
-0.00300000
-0.00399999
-0.00499998
-0.00599996
Exercises in Computer Programming
5 / 12
講義資料: http://bit.ly/bpe-ecp14
演習ex10-1の結果をファイルに出力
 繰り返しの内部で t と y を出力しているか確認する.
 外側にあったら,内部に入れる.
printf("t=%f, y=%f¥n", t, y);
 gnuplotでの描画に適した形式に変更する.
 数字以外を出力しないようにする.
 数字の間は空白で区切る
printf("%f %f¥n", t, y);
 画面に表示されるのを確認: ./ex10-1
 ファイル “result.txt” に出力: ./ex10-1 > result.txt
2014/6/30
Exercises in Computer Programming
6 / 12
講義資料: http://bit.ly/bpe-ecp14
データファイルを読み込んでグラフを描画
 ファイル“result.txt”の内容を描画
 plot "result.txt"
 ファイル“result.txt”の内容を線で描画
 plot "result.txt" with lines
 3列以上項目を保持しているデータファイルの場
合は,using で2項目を指定
 ファイル “data1.txt” の1列目と3列目を点で描画
 plot "data1.txt" using 1:3 with points
2014/6/30
Exercises in Computer Programming
7 / 12
plot のオプション
講義資料: http://bit.ly/bpe-ecp14
 関数もしくはファイル名の後に付加
 何列目のデータを用いるか: using X:Y
 X列目のデータを横軸に,Y列目のデータが縦軸に
 点/線/etc. で描く: with STYLE
 STYLE = {points,lines,など}
 線を太くする: linewidth N
 キャプション: title "CAPTION"
 それぞれ省略形を使用可能
 上記のアンダーバーの部分(usingならu)
 オプションの指定順序が決まっているので注意
2014/6/30
Exercises in Computer Programming
8 / 12
講義資料: http://bit.ly/bpe-ecp14
その他小技集
 X軸,Y軸の範囲を指定したい
 xの範囲を-5~5に: set xrange [-5:5]
 yの範囲を0~10に: set yrange [0:10]
 やっぱり範囲を自動設定に戻したい
 x軸だけ: set autoscale x
 xy軸どちらも: set autoscale xy
 対数グラフを描画したい
 y軸だけ: set logscale y
 xy軸どちらも: set logscale xy
2014/6/30
Exercises in Computer Programming
9 / 12
講義資料: http://bit.ly/bpe-ecp14
その他小技集
 横軸 y=0 をグラフと一緒に描きたい
 plot "result.txt" w l, 0 t ""
 先程のplotを再実行したい
 replot
 コマンドは省略可能
 例: replot→rep
2014/6/30
Exercises in Computer Programming
10 / 12
講義資料: http://bit.ly/bpe-ecp14
解析解と数値解の比較
 端末での処理手順
 データを出力したフォルダに移動(できているはず)
 フォルダに result.txt があることを確認: ls
 gnuplotを起動: gnuplot
 gnuplotでの処理手順
 解析解の描画: plot x**2*exp(-x)
 解析解はこれだった:
=
y
t 2 exp(−t )
 0から2までだけ描きたい: set xrange [0:2]
 数値解の描画: plot "result.txt"
 一緒に描くこともできる:
plot x**2*exp(-x), "result.txt"
2014/6/30
Exercises in Computer Programming
11 / 12
講義資料: http://bit.ly/bpe-ecp14
スクリプトファイルの使用
 gnuplotを起動するごとに,範囲設定をしたり,
plotの長いコマンドを入力するのは面倒だ.
 コマンドを並べたファイル(スクリプトファイル)
を用意して,その内容を上から順に実行させるこ
とができる.
 スクリプトファイルを準備: graph1.plot
 実行: load "graph1.plot"
2014/6/30
Exercises in Computer Programming
12 / 12