5. GNUPLOT による数式と数値データのグラフ化
1 目的
- GNUPLOT を使って関数のグラフを描いたり,テキストファイル内の数値データをグラフ化できる。
2 GNUPLOT の使い方の基本
2.1 wgnuplot (Windows で利用可能なウィンドウ版 GNUPLOT) の起動と終了
- 起動
- スタートボタンなどからマウスで起動
- またはコマンドプロンプトで wgnuplot (コンソール版の起動コマンドは gnuplot)
- 終了
gnuplot> exit
2.2 GNUPLOT 利用の基本的な方法
- 基本はコマンドによる描画や表示設定
- 描画コマンド: plot(3次元プロットは splot)
gnuplot> plot 1.0/2.0*x, abs(x), sin(x)
- 再描画コマンド: replot
gnuplot> replot
- plot 時の描画形式変更:
gnuplot> plot [-2*pi:2*pi] 1.0/2.0*x, abs(x), sin(x) gnuplot> plot 1.0/2.0*x, abs(x), sin(x) with impulses
- 描画設定の変更コマンド: set
gnuplot> set xrange [-5:5] gnuplot> set xtics 1 gnuplot> set xlabel 'x'
set による設定変更は以降の描画に反映される
gnuplot> plot cos(x)
2.3 作成した図の複写と貼り付け
- グラフが表示されたウィンドウのボタンでクリップボードに複写可能
- 複写先のアプリケーションで貼り付け操作
2.4 ヘルプコマンド
gnuplot> help plot
2.5 GNUPLOT 4.6 マニュアル(日本語)
3 練習1
関数 sin(x) と sin(x)/x のグラフ (gnuplot_func.pdf 参照) を GNUPLOT で描きなさい。 gnuplot_func.pdf に描かれている,二つのグラフが入った word ファイルを提出しなさい。
4 GNUPLOT を用いたデータファイルの描画(ファイルの利用)
4.1 wgnuplot の起動
- コマンドプロンプトで wgnuplot を実行
4.2 データの入ったテキストファイル(データファイル)の作成
- データファイルの保存場所を意識すること (保存場所
H:\
) - Web ページ上のデータをファイルに保存 (sample_a.txt, sample_score.txt)
4.3 数値のみからなるデータファイルのプロット
- データファイルが存在するディレクトリ(フォルダ)の確認
- カレントディレクトリの確認: pwd
- (必要があれば)カレントディレクトリの変更: cd "ディレクトリのパス名" あるいは ChDir ボタンからフォルダ選択
- データの描画: plot 'ファイル名' (カレントディレクトリとファイルの在処が一致しないと,ファイル名だけでは失敗する)
gnuplot> plot 'sample_a.txt'
- 描画スタイル: plot コマンドの with オプション
- 諸設定の変更: set コマンド
4.4 数値以外を含んだ複数列データのプロット
- コメント: #
- コマンドプロンプトとエディタ(メモ帳: コマンド名 notepad)によるファイル (sample_score.txt) 編集 (1行目の文字化けを直してから行頭に # を挿入)
- plot コマンドにおける描画列等の選択: using オプション,描画列指定,定数列 ( )
gnuplot> pwd gnuplot> plot 'sample_score.txt' using 2:3 gnuplot> plot 'sample_score.txt' using (1):2 with boxplot gnuplot> plot 'sample_score.txt' using (1):2, 'sample_score.txt' using (2):3 gnuplot> plot 'sample_score.txt' using (1):2, 'sample_score.txt' using (2):3, '' using (3):4 gnuplot> set style data boxplot gnuplot> plot 'sample_score.txt' using (1):2, '' using (2):3, '' using (3):4 gnuplot> set key off gnuplot> set yrange [0:100] gnuplot> set xtics("数学" 1, "英語" 2, "国語" 3) gnuplot> set ylabel "得点" gnuplot> plot 'sample_score.txt' using (1):2, '' using (2):3, '' using (3):4
4.5 命令の保存と読み込み: save, load コマンド
gnuplot> save 'sample_score.plt'
gnuplot> load 'sample_score.plt'
4.6 参考: グラフをファイルに出力
- グラフをウィンドウ上に描く代わりに pdf ファイル(ファイル名 sample_score.pdf)に出力
gnuplot> set terminal pdf gnuplot> set output 'sample_score.pdf' gnuplot> replot
4.7 参考: コマンド行から設定・描画を実行
- コマンドプロンプトのコマンド行から gnuplot コマンドに設定・命令ファイルを渡して描画
H:\> gnuplot -p 'sample_score.plt'
4.8 参考: GNUPLOT 付属のデモファイル
C:\Program Files\gnuplot\demo
gnuplot> load 'c:\program files\gnuplot\demo\all.dem'
5 練習2
資料ページにある gardens.txt には 3 つの場所における10日間のオゾン濃度(ozone concentration) の測定値が pphm (parts per hundred million) 単位で入っているとする。 このデータの箱ひげ図 (boxplot_gardens_gnuplot.pdf 参照)を GNUPLOT で描きなさい。 描画に用いた設定・命令を GNUPLOT の save コマンドで .plt ファイルに保存し,提出しなさい。