次の例は、SAD/Tkinter による最も簡単なグラフィックスの例題です。
FFS;
w = Window[]; (ア)
c = Canvas[w, Height -> 400, Width -> 600]; (イ)
$DisplayFunction = CanvasDrawer; (ウ)
Canvas$Widget = c; (エ)
data = Table[{x, Sin[x] + 0.1 * GaussRandom[]},{x, -Pi, Pi, Pi/10}]; (オ)
ListPlot[data]; (カ)
TkWait[]; (キ)
この例は次の図のようにWindow w (の中の Canvas c) にグラフを表示します。
上の例で、まず、(ア)、(キ) については通常のTkinterの操作ですので、それぞれ Canvas$Widget、TkWait[] を参照してください。
(イ) は Tkinter の部品Canvas を Window w の中につくります。またそのサイズを幅 600 ピクセル、高さ 400 ピクセルに指定しています。
(ウ) は以下で用いられるグラフィックスの出力関数を指定しています ($DisplayFunctionを参照)。
(エ) はグラフィックスの出力先になる Canvas部品 c を指定しています。
(オ) はこれから表示するグラフのためのデータを作っています。データは {{x1, y1}, {x2, y2}, ...} のような形をしています。この例は Sin曲線に正規擬似乱数を乗せたものです。GaussRandom[]を参照。
(カ) は (オ) で作った data からグラフを出力します。SAD ではグラフィックスは ListPlot、Plot、FitPlot 等により生成されるオブジェクト、頭部 Graphics を持った式で表わされます。このオブジェクトは ListPlot、Plot、FitPlotやShowにより出力されます。これらの関数は生成した Graphics オブジェクトを結果として返します。
この例のように、グラフの位置、範囲等々はなにも指定しなくても自動的に調節されますが、オプションを指定することにより自由に変更することができます。
グラフィックスの出力関数として TopDrawer を選ぶ (デフォルト) 場合は、(ア)--(エ)、及び (キ) の操作は不要です。
出力関数はオプションDisplayFunctionで指定されます。 DisplayFunctionのデフォルト値は$DisplayFunctionであり、$DisplayFunctionには前もってTopDrawerが割り当てられています。
出力関数はこのほかに前の例にあるように、 CanvasDrawer が選べます。CanvasDrawer は SAD/Tkinter の部品のひとつである Canvas に出力を行います。
出力されるグラフィックスは TopDrawer、CanvasDrawer でできるだけ同じになるように調整されていますが、機能の違いにより、また実装がおくれているために不完全な部分が多々あります。必要に応じて整備していくつもりです。
TopDrawer、CanvasDrawer 以外の出力関数が指定される (例えば DisplayFunction -> Identity) と、グラフィックスはどこにも出力されません。これは ListPlot などに Graphics オブジェクトだけを生成させるために用いられます。
SAD には現在、下の表にあるグラフ作成のための関数が備わっています。
| 関数 | 作成するグラフ |
| ListPlot | 数値データのプロット |
| Plot | 関数のグラフのプロット |
| ColumnPlot | 棒グラフ |
| OpticsPlot | ビームラインに沿ったデータ、ビーム光学系のプロット |
| FitPlot | 非線形回帰のグラフのプロット |
| GeometryPlot | 2次元のビームラインのGeometryプロット |
| HistoPlot | ヒストグラムプロット |
| ListContourPlot | Contourプロット |
| ListDensityPlot | Densityプロット |
| BeamPlot | beam ellipseプロット |
いずれの関数も引き数としてオプションが指定できます。これらのオプションの多くはいずれの関数にも共通です。オプションは オプションシンボル -> 値 という形をとります。プロットのオプション表にこれらのオプションを掲げます。また、これらの他にもそれぞれの関数に固有のオプションがありますが、それらは各関数の項で説明します。
グラフ作成関数のオプション
| オプション | 値 | デフォルト値 | 効果 |
| AspectRatio | 実数値 | GoldenRatio | 横/縦比 |
| ColorFunctionScaling | True, False | True | If it is False in ListContourPlot, the value of the list is directly pased to the color function. |
| Detach | True, False | False | True to run tdr asynchronously |
| DisplayFunction | 出力関数 | TopDrawer | グラフィック出力関数 |
| Epilog | グラフィックス, 原子のリスト | {} | グラフ出力後に描く図形 |
| ErrorBarTickSize | 実数値 | 1 | エラーバーの鍵の相対長 |
| Frame | True, False | True | グラフの外枠の表示 |
| FrameClick | True, False | True | to allow click on frame to change options. |
| FrameLabel | {下,左,上,右} | {"","","",""} | 座標軸のラベル文字列 |
| FrameThickness | 実数値 | Automatic | 座標軸の太さ |
| FrameTicks | {下,左,上,右} | {Both,Both,Ticks,Ticks} | None to turn off ticks and labels, Both to turn on ticks and labels, Ticks to turn on ticks only, << For bottom tick >> False is same as Ticks, True is same as Both. << For top tick >> False is same as None, True is same as Ticks. << For left & right ticks >> False is same as None, True is same as Both. |
| GridLines | None, Automatic, Axis[z], All | {Automatic, Automatic} | Automatic to draw grid lines at major ticks. {Automatic,None} for only x, {None,Automatic} for only y. Both, Minor, and Major can be also used. Axis draws a straight line at zero if it is in the plot range. Axis[z] draws a straight line at z, if it is in the plot range. All draws all of Major, Minor, Axis. These values can be put in a list. Axis: axis[0], Both : major + minor, All: major + minor + axis[0], Automatic: major + axis[0] |
| Initialize | True, False | True | 座標系等の初期化の有無 |
| Legend | 文字列 | False | Legend |
| Plot | True, False | ListPlot: True, Plot: False | データ点のマーカ表示 |
| PlotColor | 色 | "black" | グラフの線の色 |
| PlotJoined | True, False | ListPlot: False, Plot: True | データ点を線で結ぶか否か |
| PlotLabel | 文字列 | "" | グラフ全体につけるラベル |
| PlotRange | y軸の範囲または{x範囲, y範囲} | {Automatic, Automatic} | プロットするデータの領域 |
| PlotRegion | {{xmin, xmax}, {ymin, ymax}} | {{0,1},{0,1}} | プロットの相対位置 |
| PointSize | 実数値 | 1 | マーカの大きさの相対比 |
| PointColor | 色 | "green" | マーカの内部色 |
| Prolog | グラフィックス、 原子のリスト | {} | グラフ出力前に描く図形 |
| Scale | { xscale, yscale} | {Linear, Linear} | Linear または Log を選ぶ |
| Tags | True False | False | Canvas$ID にアイテム番号を記録 |
| TickSize | 実数値 | 1 | relative size of ticks. |
| Background | 色 | "#ffffd0" | グラフの枠内の色 |
プロットするデータの範囲は通常データ自身から自動的に設定されますが、オプション PlotRange により調節可能です。シンボル Automatic は自動設定値を用いることを指示します。
ListPlot[data, PlotRange -> {{-10, 10}, {-2, 3}}]
とすると下図の様になります。プロットは通常指定された Canvas 画面から自動的にその位置及び大きさが決まりますが、PlotRegion により 自由な位置、大きさに変更できます。
ListPlot[data, PlotRegion -> {{0, 0.5}, {0.5, 1}}]
とすると下図の様になります。ListPlot[data, AspectRatio->1]とすると図の様になります。
ListPlot[data, FrameLabel -> {"Bottom", "Left"}]
とすると図の様になります。ListPlot[data, PlotLabel -> "PlotLabel"]とすると図の様になります。
ListPlot[data, Prolog -> {Rectangle[{-1, -0.5}, {1, 0.5}, FillColor -> "gray"]} ]
とすると図の様になります Rectangleは後述のグラフィックス原子のひとつで、長方形を描きます)。ListPlot[data, PlotJoined -> True]とすると図の様になります。
ListPlot[data, PlotJoined -> True, Plot -> False]とすると図の様になります。
ListPlot[data, PointSize -> 3, PointColor -> "White"]とすると図の様になります。
ListPlot[リスト1, オプション1, ...] は上述のように数値データのリスト、リスト1 からグラフを作ります。
y1}, ...} の様に各々が 3 個の数値から成り立っている場合は、3 番目の数値は y方向のエラーバーの長さを表わします。
リスト1 が {{x1, y1、
x1,
y1}, ...} の様に各々が 4 個の数値から成り立っている場合は、3 番目の数値は x方向、4番目の数値はy 方向のそれぞれのエラーバーの長さを表わします。data1 = Append[#, 0.1]& /@ data; ListPlot[data1];
Plot は 1変数の関数のプロットを作ります。例えば、単純に次のようにすると図のようなプロットになります。
Plot[Sin[x], {x, -Pi, Pi}];
また、
Plot[{Sin[x], Cos[x]}, {x, -Pi, Pi}];
のように第一引き数を複数の式のリストとすると図のようなプロットになります。この場合、各式の表示色は {"black", "red", "blue", "green", "grey", "magenta", "cyan"} の順に循環的に選択されますが、オプション PlotColor -> 色リスト を指定することにより自由に設定することができます。
Plot の構文は以下の通りです。
Plot には表のオプションに加えて、 表にある Plot に固有のオプションも指定できます。
| オプション | 値 | デフォルト値 | 効果 |
| MaxBend | 実数値 | 0.04 | 各線分の曲がり角の最大値 |
| PlotDivision | 実数値 | 250 | 表示区間の最大分割数 |
| PlotPoints | 実数値 | 25 | 分割点の点数の初期値 |
| PlotColor | 色または色リスト | {"black", "red", "blue", "green", "gray", "magenta", "cyan", "yellow"} | プロット線達の表示色 |
ColumnPlot のオプション ColumnPlot には表 Plotのオプションに加えて、 表にある ColumnPlot に固有のオプションも指定できます。
| オプション | 値 | デフォルト値 | 効果 |
| ColumnOffset | 数値 | 0.15 | 棒と枠のすき間の比率 |
| FillColor | 色または色リスト | {"black", "red", "blue", "green", "gray", "magenta", "cyan", "yellow"} | 棒達の表示色 |
| MeshStyle | ビットマップまたはそのリスト | {Null, "gray25", "gray50"} | 積み上げの表示のための充填パターン。Nullは 100% 塗る。 |
| Orientation | Horozontal, Vertical | Vertical | 棒の方向 |
ビームラインに沿ったデータ、ビーム光学系のプロットを行う。
Usage: OpticsPlot[fun_list,options]
パラメータ
| options | defaults | |
| Region | {1,LINE["LENGTH"]} | {begin, end}, begin and end can be strings. |
| Lattice | True | False to turn of drawing lattice |
| LatticeRegion | Automatic | {low,high}, the region where lattice is drawn |
| FrameHeight | Automatic | List of relative heights of each frame |
| InfoLabel | False | If True, pressing Button shows Twiss, etc. |
| Names | "*" | A pattern of component names to be plotted. |
| Tags | False | True to attach tags "C$"//(component name) to each rectangle for the lattice, and "L$"//(component name) to the component label (CanvasDrawer only). |
| Legend | False | If Automatic, Legend is composed from FrameLabel Automatically. |
| PlotDivision | Automatic | determined by the number of elements |
| options | defaults | |
| Unit | 1 | Unit of the object. "Meter", "InvMeter",etc. |
| FrameLabel | "" | Left frame label. |
| Legend | False | If Automatic, Legend is composed from FrameLabel Automatically. |
Example:
p2=OpticsPlot[
{{"BX","BY"},
{"DX",{{{10,0.001},{20,0.002}},FrameLabel->"DX meas.",
Unit->Meter}}}];
例えば、
w = Window[];
c = Canvas[w, Height -> 400, Width -> 320];
$DisplayFunction = CanvasDrawer;
Canvas$Widget = c;
pl=OpticsPlot[{{"BX"},{"BY"},{"EX"}}];
TkWait[];
ListPlot や Plot で作成したグラフは合成してひとつのグラフとして表示することができます。次の例
g1 = ListPlot[data, DisplayFunction -> Identity];
g2 = Plot[Sin[x], {x, 0, 2Pi}, DisplayFunction -> Identity];
Show[g1, g2];
は図のようなグラフを作ります。
ここで、ListPlot 及び Plot には DisplayFunction -> Identity というオプションをつけておきます。これは、各々の関数の段階ではプロットを実行せず、結果のグラフィックス・オブジェクトをそれぞれシンボル g1, g2 に割り当てるだけにする為です。そして、最後の Show ではじめてプロットを実行します。図で軸の範囲が g1, g2 の両方を包む様に設定されることに注意してください。
Canvas の中でのあるグラフの表示位置は通常はその Canvas の大きさから自動的に決められます。しかし、関数 Rectangle を使えばその位置を自由に設定でき、またひとつの Canvas に複数のグラフを表示することもできます。例えば、
g1 = ListPlot[data, DisplayFunction -> Identity];
g2 = Plot[Sin[x], {x, -Pi, Pi}, DisplayFunction -> Identity];
Show[Graphics[ {
Rectangle[{0, 0}, {1, 0.5}, g1],
Rectangle[{0, 0.5}, {1, 1}, g2]} ]];
は次図のようなグラフを作ります。
ここで Rectangle 自身は Graphics オブジェクトではなくグラフィックス原子なので、Show に渡すためには (複数の場合はリストにして) Graphics をかぶせなければなりません。
FitPlot は非線形回帰関数 Fit と ListPlot, Plot を組み合わせたものです。
-Fit を求め、その結果をプロットします。
の重みとなります。エラーバーがない場合は重みは均等になります。
-Fit を求め、その結果をプロットしたものです。 data1 = Append[#, 0.1]& /@ data;
FitPlot[data1, e Sin[f x + g], x, {e, 1}, {f, 1}, {g, 0}];
GX-GY平面の2次元のビームラインのgeometryをプロットする。
Usage: GeometryPlot[options...]
例:
w=KBMainFrame["sample",f,Title->"Geometry Plot!"];
c1=Canvas[f,Width->640,Height->400];
$DisplayFunction=CanvasDrawer;
Canvas$Widget=c1;
pl=GeometryPlot[Region->{1,50},Namaes->"*",Frame->True,HoldAspect->True];
TkWait[];
図 GeometryPlotの用例
Usage: HistoPlot[data, options, ...]
HistPlot[] plots a histogram using ColumnPlot(default) or ListPlot. Data can be a single list, or list of lists, which results in a multi-column histogram on a common axis. Besides options common for all plotting functions and ColumnPlot, it has its own options:
| Option | Value | Default | Action |
| Bins | number | Automatic | number of bins |
| BinRange | {min,max} | Automatic | Range of bins |
| PlotStyle | ColumnPlot, ListPlot, FitPlot | ColumnPlot | plot function |
| Orientation | Vertical, Horizontal | Vertical | orientation of columns |
FitParameters args for FitPlot in a list
Example:
g01=HistoPlot[x1,Bins->50,BinRange->{-0.05,0.05},DisplayFunction->Identity];
Usage: ListContourPlot[list, options, ...]
ListContourPlot[] plots a coutour plot by list which is a 2D List of Real data.
| Option | Value | Default | Action |
| Contours | Real | 10 | number of contoure |
| PlotRange | {min,max} | Automatic | depth of contours |
| MeshRange | {{x0,x1},{y0,y1}} | Automatic | Range of x and y axes |
| AspectRatio | Real | 1 | |
| ColorFunction | Function or String | Automatic | Null or None means "white" |
| ContourColorFunction | Function or String | Automatic | Null or None to hide |
Example:
cf[x_]:=If[0.09<0.11,"red",Null,Null]; (* Draw red contour line only *)
(* around x = 0.1 *)
ListContourPlot[Table[-x^2+y^2,{x,-4,4,0.5},{y,-4,4,0.5}],
MeshRange->{{-4,4},{-4,4}},ContourColorFunction->cf,
ColorFunction->Automatic, FrameLabel->{"x","y"}]
Usage: ListDensityPlot[list, options, ...]
ListDensityPlot[] plots a density plot by list which is a 2D List of Real data.
| Option | Value | Default | Action |
| PlotRange | {min,max} | Automatic | depth of density |
| MeshRange | {{x0,x1},{y0,y1}} | Automatic | Range of x and y axes |
| AspectRatio | Real | 1 | |
| ColorFunction | Function or String | Automatic | Null or None means "white" |
| Mesh | True or False | False | True to draw mesh |
| MeshColor | Function or String | Automatic | Null or None to hide |
Example:
data = Table[Sin[x]/Cos[x^2 + y^2], {x, -2, 2, 0.1}, {y, -2, 2, 0.1}];
ListDensityPlot[data,PlotRange->{-5,5}, MeshRange->{{-2,2},{-2,2}},
FrameLabel->{"x","y"}]
Usage: BeamPlot[loc, axes, options]
BeamPlot[] plots a beam ellipse at a location loc, for axes. Axes are given by a list {ax, ay}, where ax and ay are one of "X", "PX", "Y", "PY", "Z", "DP". The beam envelope should be calculated by (CODPLOT;EMIT) or BEAM commands before BeamPlot.
| options | defaults | |
| Orbit | True | Uses Twiss["DX",loc], etc. as the center of ellipse. |
| SizeFunction | "SIZE" | If "SIG", LINE["SIG"] is used. LINE["SIZE"] is the default. |
| AspectRatio | 1 | |
| DataRange | Default | If Default, PlotRange becomes square for axes = {"X", "Y"} or {"PX", "PY"} |
グラフィックス原子は SAD のグラフィックスを構成する基本的な要素です。グラフィックス原子は シンボル Graphics を被せられることにより Graphics オブジェクトとなり、Show によりプロットされるものになります。ListPlot や Plot の返すものもこのような Graphics オブジェクトに他なりません。
現在のところ、Point, Line, Text, Rectangle がこのようなグラフィックス原子として定義されています。各原子はオプション指定により様々な属性をもつことができます。このようなオプションも必要に応じて充実させるつもりです。
| オプション | 値 | デフォルト値 | 効果 |
| ErrorBarTickSize | 実数値 | 1 | エラーバーの鍵の相対長 |
| PointSize | 実数値 | 1 | マーカの大きさの相対比 |
| PointColor | 色 | "green" | マーカの内部色 ("White","Black","Red","Green","Blue","Yellow","Magenta","Cyan" |
| PointSymbol | "10","60","70","80","90" | "10" | Symbol for PLOT of TopDrawer, or Bar "6O","7O","8O","9O" are triangles in CanvasDrawer. |
| オプション | 値 | デフォルト値 | 効果 |
| Dashing | 線種 | "1" | character string or a list of numbers to represent the dashing of the line. |
| ErrorBarTickSize | 実数値 | 1 | エラーバーの鍵の相対長 |
| Plot | True, False | False | データ点のマーカ表示 |
| PlotJoined | True, False | True | データ点を線で結ぶか否か |
| PlotColor | 色 | "black" | グラフの線の色 |
| PointSize | 実数値 | 1 | マーカの大きさの相対比 |
| PointColor | 色 | "green" | マーカの内部色 |
| Thicknetss | 実数値 | 1 | 線の相対的な太さ |
| option | default | optional values |
| Plot | False | whether plot symbols at data points. If True, PointSize and PointSymbol are effective (see above). |
| PointSize | 1 | relative size of a point |
| PointSymbol | "1O" | Symbol for PLOT of TopDrawer, or Bar "6O","7O","8O","9O" are triangles in CanvasDrawer. |
| PointColor | "forest green" | point fill color. |
| PointBorderColor | Automatic | point border color. Automatic measn PointColor. |
| PointTags | Null | points tag string or list of tag strings. |
| PlotJoined | True | whether plot border line of polygon. |
| Thickness | 1 | thickness of border line |
| Dashing | "1" | character string or a list of numbers to represent the dashing of the line. |
| PlotColor | "black" | border line color. |
| LineTags | Null | border line tag string. |
| FillColor | Null | polygon fill color. Null means empty polygon. |
| Tags | False | polygon tag string. |
| オプション | 値 | デフォルト値 | 効果 |
| FillColor | 色 | Null | 内部の色、Null の時は枠だけを描く |
| MeshStyle | ビットマップ | Null | 内部を塗るパターン、Null のときは完全に塗る |
| PlotColor | 色 | "black" | 枠の色 |
| Tags | 文字列 | 付加するタグ |
| オプション | 値 | デフォルト値 | 効果 |
| PlotColor | 色 | "black" | テキストの色 |
| Tags | 文字列 | 付加するタグ(\rref{tags} 参照) | |
| TextAlign | "left", "center", "right" | "left" | テキストの基準位置 |
| TextFont | フォント | $DefaultFont | フォント |
| TextSize | 実数値 | 1 | 大きさ、標準サイズに対する相対比 |
| TextPosition | 文字列 | "DATA" to represent the position by data coordinates | |
| TextRotate | 実数値 | 0 | 回転角度 |