グラフやプロットに凡例を加えるには,どうすればよいのですか.

Mathematica では,簡単に2次元データをプロットしたり,データを棒グラフや円グラフなどのグラフィックスで表現したりできます.このようなグラフィックスに凡例を作っておくと,データ解釈の助けとなり便利なことがよくあります.標準アドオンパッケージのGraphics`Legend`が,そのための柔軟なグラフィックスオブジェクトを提供します.


関数のプロットのための凡例

関数Plotを使っているときは,PlotLegendオプションで素早く自動的に凡例を加えることができます.


例 1.1

[Graphics:Images/index_gr_1.gif]

Plot[{Sin[x], Cos[x]}, {x, -2 Pi, 2 Pi},
    PlotStyle -> {RGBColor[0, 1, 0], RGBColor[0, 0, 1]},
    PlotLegend -> {"Sine", "Cosine"}]
[Graphics:Images/index_gr_2.gif]

Mathematica はデフォルトで凡例ボックスのサイズと位置を自動的に決定します.位置とサイズを自分で決めたいときは,それぞれLegendPositionLegendSizeを使って設定することができます.値の指定には,以下の座標系を使います.LegendPositionLegendSizeの座標系は,プロットの座標系とは関係ありません.

[Graphics:Images/index_gr_3.gif]

以下は,手動で凡例ボックスのサイズ(幅0.5x高さ0.25)と位置(0.5, 0.3)を定義する例です.


例 1.2

Plot[{Sin[x], Cos[x]}, {x, -2 Pi, 2 Pi},
    PlotStyle -> {RGBColor[0, 1, 0], RGBColor[0, 0, 1]},
    PlotLegend -> {"Sine", "Cosine"},
    LegendPosition -> {.5, .3},
    LegendSize -> {.5, .25}
]
[Graphics:Images/index_gr_4.gif]


データプロットの凡例

データプロットに凡例を加えたい場合は,Plotの場合と基本的には同じ方法でListPlot関数を使います.


例 2.1

[Graphics:Images/index_gr_5.gif]
[Graphics:Images/index_gr_6.gif]
MultipleListPlot[{datalist1, datalist2},
    PlotJoined -> {True, False},
    PlotStyle -> {RGBColor[1, 0, 0], RGBColor[0, 0, 1]},
    PlotLegend -> {"Sine", "Cosine"}]
[Graphics:Images/index_gr_7.gif]


グラフおよび他のグラフィックスの凡例

オプションPlotLegendは,BarChartPieChartなど他のオブジェクトに凡例を加えるためには使えません.このようなオブジェクトの凡例は,ShowLegend[graphics object, legend object]関数とMathematica のグラフィックスプリミティブライブラリを使って作成します.

まず,簡単な棒グラフの例です.

[Graphics:Images/index_gr_8.gif]
[Graphics:Images/index_gr_9.gif]
[Graphics:Images/index_gr_10.gif]

次に凡例を加えます.ShowLegend関数で上のグラフをラップし,凡例の位置・サイズとラベルを定義します.


例 3.1

[Graphics:Images/index_gr_11.gif]
[Graphics:Images/index_gr_12.gif]

もちろん,この方法はあらゆるグラフィックス,一連の配列に使えます.


例 3.2

ShowLegend[
DisplayTogetherArray[
  {{PieChart[{11, 18, 12}, PieLabels -> None, PlotLabel -> "Q1 `98"],
    PieChart[{13, 21, 12}, PieLabels -> None, PlotLabel -> "Q2 `98"],
    PieChart[{14, 20, 13}, PieLabels -> None, PlotLabel -> "Q3 `98"]},
   {PieChart[{14, 26, 10}, PieLabels -> None, PlotLabel -> "Q4 `98"],
    PieChart[{18, 23, 16}, PieLabels -> None, PlotLabel -> "Q1 `99"],
    PieChart[{17, 25, 16}, PieLabels -> None, PieExploded -> All, PlotLabel -> "Q2 `99"]}}],
   {{{RGBColor[1, 0, 0], "Helen"},
     {RGBColor[0, 1, 0], "Joe"},
     {RGBColor[0, 0, 1], "Bob"}},
   LegendPosition -> {-0.5, .75},
   LegendSize -> {1, .4},
   LegendLabel -> "Acme Company Sales",
   LegendOrientation -> Horizontal,
   LegendShadow -> None}
]


[Graphics:Images/index_gr_13.gif]


 © 2008 Wolfram Research, Inc.  Terms of Use  Privacy Policy | [en] |
ニュースレターのご登録