260 likes | 776 Views
Gnuplot との連携. Gnuplot による関数の描画 Gnuplot によるデータのプロット Gnuplot による 3-D グラフの描画 Gnuplot による 3-D データのプロット. 今日のポイント. グラフ描画ソフト gnuplot を体験してみよう. Gnuplot とは …. グラフ描画専用のフリーウェア Windows 版の名称は wgnuplot インストールはフォルダごとコピーするだけ 専用のコマンドウインドウにより、おもにコマンドで操作 日本語のオンラインヘルプや Web での解説ページも充実. Gnuplot の起動と終了.
E N D
Gnuplot との連携 Gnuplotによる関数の描画 Gnuplotによるデータのプロット Gnuplotによる3-Dグラフの描画 Gnuplotによる3-Dデータのプロット 今日のポイント グラフ描画ソフト gnuplot を体験してみよう
Gnuplot とは… • グラフ描画専用のフリーウェア • Windows版の名称は wgnuplot • インストールはフォルダごとコピーするだけ • 専用のコマンドウインドウにより、おもにコマンドで操作 • 日本語のオンラインヘルプやWebでの解説ページも充実
Gnuplotの起動と終了 • gnuplotの起動は [ スタート ] → [すべてのプログラム(P)>] → [gnuplot] を<クリック> • [ スタート ] → [すべてのプログラム(P)>] → [gnuplot] を <右クリック>して、[送る(N)] より [ デスクトップ(ショートカットを作成)]を選び、 ショートカットアイコン を作成 • ショートカットアイコン を<ダブルクリック>すると、gnuplotが 起動 • 左上の [File] → [Exit]または、右上の [x] ボタンにより、終了 送る(N) File Exit X
フォントが小さい場合 • gnuplot のウィンドウを <右クリック> し、3つめの[ Choose Font... ]を選択し、フォントウィンドウを開く • フォント名(F):で [Terminal]を選択し、サイズ(S):で [14]を選んでおく • [ OK ] ボタンを押してフォントウィンドウを閉じる • 再び、gnuplot のウィンドウを <右クリック> し、一番下の [Update wgnuplot.ini] を選んで設定を保存 Choose Font… Terminal 14 Update wgnuplot.ini
練習1関数グラフを描いてみよう gnuplot> plot 2*x*x+3, x**3-75*x べき乗 プロンプト
グラフ描画のためのTips(1) • X軸ラベル、Y軸ラベルは gnuplot> set xlabel "x" gnuplot> set ylabel "y" gnuplot> replot • 凡例(Key)を変えるには gnuplot> plot 2*x*x+3 title "y=2x^2+3", \ > x**3-75*x title "y=x^3-75x" • プロットの線を「線と点」に変えるには gnuplot> plot 2*x*x+3 with linespoints, \ > x**3-75*xwith linespoints 矢印キー↑で 前のコマンド を呼び出し、 編集できる Axes メニュー [Replot]ボタン 改行して 続ける記号 Plot メニュー
グラフ描画のためのTips(2) • X軸範囲、Y軸範囲の指定は gnuplot> set xrange [-15:15] gnuplot> set yrange [-2000:2000] gnuplot> replot • 目盛線をつけるには gnuplot> set grid • 対数グラフにするには gnuplot> set logscale xy • setコマンドを解除するには gnuplot> unset grid Axes メニュー Chart メニュー → Grid on Axes メニュー →Logscale xyz Chart メニュー →Grid off
練習2 対数グラフを描いてみよう plot 1/(1+x*x) set logscale xy set xrange [0.1:100] set yrange [0.0001:1] set xlabel "x" set ylabel "y" set grid replot gnuplot> gnuplot> gnuplot> gnuplot> gnuplot> gnuplot> gnuplot> gnuplot> Lorentz関数
オプション sinc2(x) = (sin px/px)2 sinc2(x)の対数グラフ gnuplot> gnuplot> gnuplot> gnuplot> gnuplot> gnuplot> gnuplot> gnuplot> gnuplot> sinc(x) = sin(pi*x)/(pi*x) plot sinc(x)**2 set logscale xy set xrange [0.1:100] set yrange [0.0001:1] set xlabel "x" set ylabel "y" set grid replot 関数定義
オプション リサージュ図形 gnuplot> gnuplot> gnuplot> gnuplot> gnuplot> gnuplot> 媒介変数t set parametric plot cos(5*t), sin(7*t) set trange [-pi:pi] set grid unset key replot 凡例なし (No Key)
練習3 データのプロット gnuplot> plot 'Z:\nyumon2\q14.csv' with lines • 凡例を書き換える場合 gnuplot> plot 'Z:\nyumon2\q14.csv' \ > title "y=2x^2+3" with lines • さらに線を「線と点」にかえる gnuplot> plot 'Z:\nyumon2\q14.csv' \ > title "y=2x^2+3" with linespoints
オプション 複数データのプロット gnuplot> plot 'Z:\nyumon2\q14_s.csv' with lines, \ > 'Z:\nyumon2\q14_s.csv' using 1:3 with lines 1列目と3列目でプロット • 凡例を書き換える場合 gnuplot> plot 'Z:\nyumon2\q14_s.csv' \ > title "2x^2+3" with lines, \ > 'Z:\nyumon2\q14_s.csv' using 1:3 \ > title "x^3-75x" with lines
練習43-Dグラフの描画 R(x,y)=sqrt(x*x+y*y) gauss(x,y)=exp(-R(x,y)**2) splot gauss(x,y) set xrange [-2:2] set yrange [-2:2] set isosample 40,40 set xlabel "x" set ylabel "y" replot gnuplot> gnuplot> gnuplot> gnuplot> gnuplot> gnuplot> gnuplot> gnuplot> gnuplot> 関数定義 default の10 では足りない
練習5等高線の描画 gnuplot> gnuplot> gnuplot> gnuplot> gnuplot> gnuplot> gnuplot> gnuplot> gnuplot> gnuplot> gnuplot> gnuplot> R(x,y)=sqrt(x*x+y*y) gauss(x,y)=exp(-R(x,y)**2) splot gauss(x,y) set xrange [-2:2] set yrange [-2:2] set contour base unset surface set view 0,0 set cntrparam levels 10 set xlabel "x" set ylabel "y" replot
3-D/等高線グラフのためのTips • 陰線処理 gnuplot> set hidden3d • カラーマップ表示 gnuplot> set pm3d • カラーパレットの指定 gnuplot> set palette gray gnuplot> set palette color
オプション Milk drop Milkdrop(x,y) = exp( exp( -R(x,y) ) * ( exp( cos( R(x,y) )**20 )+8*sin( R(x,y) )**20+2*sin( 2*R(x,y) )**8) )
3-Dプロット用のデータ構造 • Excelとは異なり、2-Dプロット用データを縦に並べることで3-Dプロット用データとしている。 • 2-Dプロット用データの区切りには<空行>を用いる。 • 3-Dプロットデータをさらに複数重ねるときには、2行分の<空行>を境界とする。
練習63-Dデータのプロット • まず、 f (x, y)=exp(-(x2 + y2)),-2 ≦ x, y ≦ 2 のgnuplot用データを作成し、gauss2.datに保存する。 • つぎに、gnuplotにより gauss2.datの3-D プロットを描く。
プログラム例 (gauss2g.c) #include <stdio.h> #include <math.h> int main(void) { int i, j, n=20; double x, y, z; for (j = -n; j <= n; j++) { y = 0.1*j; for (i = -n; i < n; i++) { x = 0.1*i; z = exp(-(x*x+y*y)); printf("% .2f, % .2f, % .3f\n",x,y,z); } printf("\n"); } return 0; }
実行手順 Z:\nyumon2>cl gauss2g.c ... Z:\nyumon2>gauss2g > gauss2.dat このあと、gnuplotを起動し、 gnuplot> splot 'Z:\nyumon2\gauss2.dat' \ > with lines
スキルアップタイム:(gauss22g.c) 絶対値は fabs( ) • 最初に f (r) = exp(-r2) | cos(2pr) | のデータを出力したあと、2回改行し、続けて f (r) = exp(-r2) + 1 を出力するプログラムを作成し、gauss22.dat に保存 せよ。ただし、r2 = x2+y2, -2 ≦ x, y ≦ 2. • つぎに、gnuplotにより gauss22.dat の3-D プロットを描け。
ヘルプ、その他 • Helpメニュー : コマンドの詳しい使い方など • コマンド、オプションは誤解されない範囲で短く省略可 • plot → p, splot → sp, replot → rep • with → w, lines → l, title → t, using → u • xrange → xr, xlabel → xl, logscale → logs • palette → pal, hidden3d → hid など • デモは C:\Gnuplot\demo\all.dem
オプション faceg.dat の表示 gnuplot> gnuplot> gnuplot> gnuplot> gnuplot> gnuplot> gnuplot> gnuplot> gnuplot> set view 180, 0 unset surface unset xtics unset ytics unset ztics set size square set pm3d set palette gray splot 'Z:\nyumon2\faceg.dat' • 真下から見る • メッシュを非表示 • 軸も非表示 • 正方形にセット • グレイスケールで表示 • gnuplot 用にデータを並べ替えた faceg.dat を3D表示
オプション face.dat → faceg.dat の変換 face4g.c #include <stdio.h> #include <string.h> int main(void) { int i; char *p,s[520]; while (fgets(s,520,stdin)!=NULL) { p=strtok(s," "); if (p && (*p!=' ')) printf("%s\n", p); for (i=1;i<128;i++) { p=strtok(NULL," "); if (p && (*p!=' ')) printf("%s\n", p); } printf("\n"); } return 0; }
オプション face.dat → faceg.dat の変換 Z:\nyumon2>cl face4g.c Z:\nyumon2>face4g < face.dat > faceg.dat