グラフの作成

グラフの例

次の例は、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) にグラフを表示します。

listplot.jpg

上の例で、まず、(ア)、(キ) については通常の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非線形回帰のグラフのプロット
GeometryPlot2次元のビームラインのGeometryプロット
HistoPlotヒストグラムプロット
ListContourPlotContourプロット
ListDensityPlotDensityプロット
BeamPlotbeam ellipseプロット

いずれの関数も引き数としてオプションが指定できます。これらのオプションの多くはいずれの関数にも共通です。オプションは オプションシンボル -> 値 という形をとります。プロットのオプション表にこれらのオプションを掲げます。また、これらの他にもそれぞれの関数に固有のオプションがありますが、それらは各関数の項で説明します。

グラフ作成関数のオプション

オプションデフォルト値効果
AspectRatio実数値GoldenRatio横/縦比
ColorFunctionScalingTrue, FalseTrueIf it is False in ListContourPlot, the value of the list is directly pased to the color function.
DetachTrue, FalseFalseTrue to run tdr asynchronously
DisplayFunction出力関数TopDrawerグラフィック出力関数
Epilogグラフィックス, 原子のリスト{}グラフ出力後に描く図形
ErrorBarTickSize実数値1エラーバーの鍵の相対長
FrameTrue, FalseTrueグラフの外枠の表示
FrameClickTrue, FalseTrueto 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.
GridLinesNone, 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]
InitializeTrue, FalseTrue座標系等の初期化の有無
Legend文字列FalseLegend
PlotTrue, FalseListPlot: True, Plot: Falseデータ点のマーカ表示
PlotColor"black"グラフの線の色
PlotJoinedTrue, FalseListPlot: False, Plot: Trueデータ点を線で結ぶか否か
PlotLabel文字列""グラフ全体につけるラベル
PlotRangey軸の範囲または{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 を選ぶ
TagsTrue FalseFalseCanvas$ID にアイテム番号を記録
TickSize実数値1relative size of ticks.
Background"#ffffd0"グラフの枠内の色

プロットするデータの範囲, PlotRange

プロットするデータの範囲は通常データ自身から自動的に設定されますが、オプション PlotRange により調節可能です。シンボル Automatic は自動設定値を用いることを指示します。

PlotRange.jpg

プロットの Canvas の中での位置の指定, PlotRegion

プロットは通常指定された Canvas 画面から自動的にその位置及び大きさが決まりますが、PlotRegion により 自由な位置、大きさに変更できます。

PlotRegion.jpg

横/縦比, AspectRatio

AspectRatio.jpg

グラフの外枠及び目盛の有無, Frame

グラフの外枠につけるラベル, FrameLabel

FrameLabel.jpg

グラフ全体のラベル, PlotLabel

PlotLabel.jpg

プロットの前後での図形の書き込み、Prolog と Epilog

Prolog.jpg

データ点を線で結ぶ、PlotJoined 及びデータ点の表示、Plot

PlotJoined.jpg
PlotFalse.jpg

マーカの大きさと色、PointSize、PointColor

PointSizeColor.jpg

対数グラフ、Scale

LogScale.jpg

ListPlot、及びエラーバーの表示listplot

ListPlot[リスト1, オプション1, ...] は上述のように数値データのリスト、リスト1 からグラフを作ります。

data1 = Append[#, 0.1]& /@ data;
ListPlot[data1];
ErrorBarY.jpg
ErrorBarXY.jpg

Plot

Plot は 1変数の関数のプロットを作ります。例えば、単純に次のようにすると図のようなプロットになります。

Plot[Sin[x], {x, -Pi, Pi}];
Plot.jpg

また、

 Plot[{Sin[x], Cos[x]}, {x, -Pi, Pi}];

のように第一引き数を複数の式のリストとすると図のようなプロットになります。この場合、各式の表示色は {"black", "red", "blue", "green", "grey", "magenta", "cyan"} の順に循環的に選択されますが、オプション PlotColor -> 色リスト を指定することにより自由に設定することができます。

Plot2.jpg

Plot の構文は以下の通りです。

Plotのオプション

Plot には表のオプションに加えて、 表にある Plot に固有のオプションも指定できます。

オプションデフォルト値効果
MaxBend実数値0.04各線分の曲がり角の最大値
PlotDivision実数値250表示区間の最大分割数
PlotPoints実数値25分割点の点数の初期値
PlotColor色または色リスト{"black", "red", "blue", "green", "gray", "magenta", "cyan", "yellow"}プロット線達の表示色

ColumnPlot による棒グラフの作成

ColumnPlot1.jpg
ColumnPlot2.jpg
ColumnPlot3.jpg

ColumnPlot のオプション ColumnPlot には表 Plotのオプションに加えて、 表にある ColumnPlot に固有のオプションも指定できます。

オプションデフォルト値効果
ColumnOffset数値0.15棒と枠のすき間の比率
FillColor色または色リスト{"black", "red", "blue", "green", "gray", "magenta", "cyan", "yellow"}棒達の表示色
MeshStyleビットマップまたはそのリスト{Null, "gray25", "gray50"}積み上げの表示のための充填パターン。Nullは 100% 塗る。
OrientationHorozontal, VerticalVertical棒の方向

OpticsPlot[]

ビームラインに沿ったデータ、ビーム光学系のプロットを行う。

Usage: OpticsPlot[fun_list,options]

パラメータ

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[];                  
OpticsPlot.jpg

グラフの合成, Show

ListPlot や Plot で作成したグラフは合成してひとつのグラフとして表示することができます。次の例

 g1 = ListPlot[data, DisplayFunction -> Identity];
 g2 = Plot[Sin[x], {x, 0, 2Pi}, DisplayFunction -> Identity];
 Show[g1, g2];

は図のようなグラフを作ります。

Show.jpg

ここで、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]} ]];

は次図のようなグラフを作ります。

Rect.jpg

ここで Rectangle 自身は Graphics オブジェクトではなくグラフィックス原子なので、Show に渡すためには (複数の場合はリストにして) Graphics をかぶせなければなりません。

FitPlot

FitPlot は非線形回帰関数 Fit と ListPlot, Plot を組み合わせたものです。

 data1 = Append[#, 0.1]& /@ data;
 FitPlot[data1, e Sin[f x + g], x, {e, 1}, {f, 1}, {g, 0}];
FitPlot.jpg

GeometryPlot

GX-GY平面の2次元のビームラインのgeometryをプロットする。

Usage:   GeometryPlot[options...]
Region:
Drawing region "Region -> {begin, end}" style by component name or POSITION number. Default {1, LINE["LENGTH"]}
Names:
Drawing component name pattern. Default "*"
Frame:
Drawing Frame. Default False
HoldAspect:
Hold aspect ratio. Default True

例:

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.gif

GeometryPlotの用例

HistoPlot[]

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:

OptionValueDefaultAction
BinsnumberAutomaticnumber of bins
BinRange{min,max}AutomaticRange of bins
PlotStyleColumnPlot, ListPlot, FitPlotColumnPlotplot function
OrientationVertical, HorizontalVerticalorientation of columns

FitParameters args for FitPlot in a list

Example:
g01=HistoPlot[x1,Bins->50,BinRange->{-0.05,0.05},DisplayFunction->Identity];

ListContourPlot[]

Usage:  ListContourPlot[list, options, ...]

ListContourPlot[] plots a coutour plot by list which is a 2D List of Real data.

OptionValueDefaultAction
ContoursReal10number of contoure
PlotRange{min,max}Automaticdepth of contours
MeshRange{{x0,x1},{y0,y1}}AutomaticRange of x and y axes
AspectRatioReal1
ColorFunctionFunction or StringAutomaticNull or None means "white"
ContourColorFunctionFunction or StringAutomaticNull 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"}]
ListContourPlot.gif

ListDensityPlot[]

Usage:  ListDensityPlot[list, options, ...]

ListDensityPlot[] plots a density plot by list which is a 2D List of Real data.

OptionValueDefaultAction
PlotRange{min,max}Automaticdepth of density
MeshRange{{x0,x1},{y0,y1}}AutomaticRange of x and y axes
AspectRatioReal1
ColorFunctionFunction or StringAutomaticNull or None means "white"
MeshTrue or FalseFalseTrue to draw mesh
MeshColorFunction or StringAutomaticNull 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"}]
ListDensityPlot.gif

BeamPlot[]

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.

optionsdefaults
OrbitTrueUses Twiss["DX",loc], etc. as the center of ellipse.
SizeFunction"SIZE"If "SIG", LINE["SIG"] is used. LINE["SIZE"] is the default.
AspectRatio1
DataRangeDefaultIf Default, PlotRange becomes square for axes = {"X", "Y"} or {"PX", "PY"}

グラフィックス原子

グラフィックス原子は SAD のグラフィックスを構成する基本的な要素です。グラフィックス原子は シンボル Graphics を被せられることにより Graphics オブジェクトとなり、Show によりプロットされるものになります。ListPlot や Plot の返すものもこのような Graphics オブジェクトに他なりません。

現在のところ、Point, Line, Text, Rectangle がこのようなグラフィックス原子として定義されています。各原子はオプション指定により様々な属性をもつことができます。このようなオプションも必要に応じて充実させるつもりです。

Point

オプションデフォルト値効果
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.

Line

Polygon

optiondefaultoptional values
PlotFalsewhether plot symbols at data points. If True, PointSize and PointSymbol are effective (see above).
PointSize1relative 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.
PointBorderColorAutomaticpoint border color. Automatic measn PointColor.
PointTagsNullpoints tag string or list of tag strings.
PlotJoinedTruewhether plot border line of polygon.
Thickness1thickness of border line
Dashing"1"character string or a list of numbers to represent the dashing of the line.
PlotColor"black"border line color.
LineTagsNullborder line tag string.
FillColorNullpolygon fill color. Null means empty polygon.
TagsFalsepolygon tag string.

Rectangle

オプションデフォルト値効果
FillColorNull内部の色、Null の時は枠だけを描く
MeshStyleビットマップNull内部を塗るパターン、Null のときは完全に塗る
PlotColor"black"枠の色
Tags文字列付加するタグ

Text


トップ   差分 バックアップ リロード   一覧 検索 最終更新   ヘルプ   最終更新のRSS
Last-modified: 2015-08-06 (木) 20:03:02