先月投稿した記事「4次元よりも無限次元の方がイメージしやすいという罠」で使った図の描き方についてまとめる。
■ ■ ■
先月の記事 nlog(n): 4次元よりも無限次元の方がイメージしやすいという罠 の説明に使った図の描き方についてメモを残すことにした。すぐに忘れてしまうので「あれ? どうやるんだっけな?」状態に何度もなるからだ。
最初の図は Microsoft PowerPoint 2000 (Microsoft Office - 以前のバージョン - Office 2000 - Microsoft PowerPoint 2000),それ以降のグラフは gnuplot for Windows 32 bit Version 4.2.4 (gnuplot homepage)である。
PowerPoint 2000

拡大図
線に「コネクタ」を使うと,図形にガイドが表示されるので便利。図形を移動すると,コネクタ線は図形にくっついたまま引っ張られる。
立体感を出すために,手前の線には,最初に太い白線を描き,その上に細い黒線を描いている。
Gnuplot
グラフには f(x)=1.5*x**2-3.5*x+3 という2次関数を使うことにした。この関数は,(0, 3), (1, 1), (2, 2), (3, 6) という整数の格子点を通る。
準備
グラフの大きさを 640x400 にしたい。そのときは,ウィンドウサイズを 648x427 にする。指定するには,C:\Documents and Settings\(User)\Application Data\wgnuplot.ini を編集する。
[WGNUPLOT]
...
GraphSize=648 427
...
グラフの左下に出るマウスの座標表示を消すために「unset mouse」し,「x0」のような下つき文字を表示させるために,ターミナルの設定「set terminal windows」に「enhanced」オプションをつける。
gnuplot> unset mouse
gnuplot> set terminal windows enhanced
1次元から4次元までの図
データファイルを4つ用意する。ファイルの内容を type コマンドで確認する。
C:\> type dimension-1.txt
0 3
C:\> type dimension-2.txt
0 3
1 1
C:\> type dimension-3.txt
0 3
1 1
2 2
C:\> type dimension-4.txt
0 3
1 1
2 2
3 6
gnuplot のコマンドは次の通り。
gnuplot> set xtics ('x_1' 0)
gnuplot> plot [-1:1][0:7] 'dimension-1.txt' title '' with impulses,'dimension-1.txt' title '' with points 1 6
gnuplot> set xtics ('x_1' 0, 'x_2' 1)
gnuplot> plot [-1:2][0:7] 'dimension-2.txt' title '' with impulses,'dimension-2.txt' title '' with points 1 6
gnuplot> set xtics ('x_1' 0, 'x_2' 1, 'x_3' 2)
gnuplot> plot [-1:3][0:7] 'dimension-3.txt' title '' with impulses,'dimension-3.txt' title '' with points 1 6
gnuplot> set xtics ('x_1' 0, 'x_2' 1, 'x_3' 2, 'x_4' 3)
gnuplot> plot [-1:4][0:7] 'dimension-4.txt' title '' with impulses,'dimension-4.txt' title '' with points 1 6
gnuplot では,「with impulses」をつけてプロットすると,垂直な線でデータが表示され,「with points」をつけると,○や×などのマーカが表示される (nlog(n): gnuplot のマーカ)。しかし,これを同時には指定できない。そこで,「with impulses」で1度描いた後,「with points」で重ね描きをしている。「with points」に続く数値は,「1」がマーカの色で,「6」が形である。この辺は試行錯誤して決める。
11次元と100次元の図
11次元の図では,[0, 3] 区間を10分割して11個の点を打ちたい。しかし,データファイルのためにいちいち手で計算するのは面倒。gnuplot を使ってデータを作ることにする。
表示する X 軸の幅の中に何個のデータを描くかは「set samples 数値」で指定する。データの書き出しには「set table」を使う (gnuplotで数値をファイルに書き出す(table) - 米澤進吾 ホームページ)。ターミナルを使って「set terminal table」でも可能なようだ (gnuplot / plot3d (1))。
gnuplot> f(x) = 1.5*x**2 - 3.5*x + 3
gnuplot> set samples 11
gnuplot> set table 'dimension-11.txt'
gnuplot> plot [0:3][*:*] f(x)
gnuplot> unset table
できたデータファイル「dimension-11.txt」の内容は次の通り。
#Curve 0 of 1, 11 points
#x y type
0 3 i
0.3 2.085 i
0.6 1.44 i
0.9 1.065 i
1.2 0.96 i
1.5 1.125 i
1.8 1.56 i
2.1 2.265 i
2.4 3.24 i
2.7 4.485 i
3 6 i
データは,1行につき「x 座標」,「y 座標」,「タイプ」の順で並んでいる。「タイプ」とは,xrange, yrange 内にデータが入っているかどうかを表している。入っていれば i (in),入っていなければ o (out) となる。
描画するコマンドは次の通り。
gnuplot> set xtics ('x_1' 0, \
'x_2' 0.3, \
'x_3' 0.6, \
'x_4' 0.9, \
'x_5' 1.2, \
'x_6' 1.5, \
'x_7' 1.8, \
'x_8' 2.1, \
'x_9' 2.4, \
'x_{10}' 2.7, \
'x_{11}' 3)
gnuplot> plot [-1:4][0:7] 'dimension-11.txt' title '' with impulses,'dimension-11.txt' title '' with points 1 6
100次元の場合も同じ手順でできる。x 軸のラベル表示はなしの方向で。
gnuplot> set xtics ''
gnuplot> plot [-1:4][0:7] 'dimension-100.txt' title '' with impulses,'dimension-100.txt' title '' with points 1 6
無限次元の図
これは gnuplot のオーソドックスな使い方。設定を戻してからプロットする。
gnuplot> set samples 100
gnuplot> set xtics auto
gnuplot> plot [-1:4][0:7] f(x) title '' with lines
気をつけたいこと
上の例では,関数 f(x) を定義しているが,定義の仕方がまずいとハマる (実際ハマッた)。次に例を示す。
gnuplot> f(x) = 1.5*x**2 - 3.5*x + 3 # OK
gnuplot> f(x) = 3./2.*x**2 - 7./2.*x + 3. # OK
gnuplot> f(x) = 3/2*x**2 - 7/2*x + 3 # NG
一番下の例では,「3/2」が整数として計算されてしまうため「1.5」ではなく「1」になってしまう。「3./2.」や「3./2」や「3/2.」であれば浮動小数点数として扱われるので,意図した通りになる。
Posted by n at 2009-03-13 04:53 | Edit | Comments (0) | Trackback(0)