How to | グラフの凡例を作成する方法
Wolfram言語は,グラフの外観を制御するための豊富で使いやすいツールを提供している.記号的なラッパーであるLegendedを使ってグラフの凡例を作成している場合でも,ChartLegends オプションを使って作成している場合でも,作成する凡例にはグラフ用に選択したスタイルが継承される.
ChartLegendsを使ってグラフの凡例を作成することができる.
BarChart[{1, 2, 3, 4}, ChartStyle -> "Pastel", ChartLegends -> {"Mary", "Bob", "Susan", "John"}]凡例からデータ要素を削除する場合は,そのラベルの代りにNoneを使うとよい:
BarChart[{1, 2, 3, 4}, ChartStyle -> "Pastel", ChartLegends -> {None, "Bob", None, "John"}]BarChart[{{1, 2, 3}, {4, 5, 6}}, ChartLegends -> {{"Aグループ", "Bグループ"}, None}, ChartStyle -> {"Pastel", None}]ChartLabelsを使って各グループのデータ要素について反復するラベルを作成する:
BarChart[{{1, 2, 3}, {4, 5, 6}}, ChartLegends -> {{"Aグループ", "Bグループ"}, None}, ChartStyle -> {"Pastel", None}, ChartLabels -> {"r1", "r2", "r3"}]LegendAppearanceを使って凡例内のラベルのレイアウトを変更する.この場合は,凡例の位置を結果として変更する:
BarChart[{{1, 2, 3}, {4, 5, 6}}, ChartLegends -> {{"Aグループ", "Bグループ"}, None}, ChartStyle -> {"Pastel", None}, ChartLabels -> {"r1", "r2", "r3"}, LegendAppearance -> "Row"]Placedを使ってグラフの下に凡例を置くこともできる:
BarChart[{{1, 2, 3}, {4, 5, 6}}, ChartLegends -> {Placed[{"Aグループ", "Bグループ"}, Bottom], None}, ChartStyle -> {"Pastel", None}, ChartLabels -> {"r1", "r2", "r3"}]ChartLegendsは3Dのグラフにも使える:
BarChart3D[{{1, 2, 3}, {4, 5, 6}}, ChartLegends -> {Placed[{"Aグループ", "Bグループ"}, Bottom], None}, ChartStyle -> {"Pastel", None}, ChartLabels -> {"r1", "r2", "r3"}]以下ではExampleDataからのテクスチャ画像を設定して,絵の棒として使っている:
textures = ExampleData[{"Texture", #}]& /@ {"Bark", "Bricks", "Wood", "Bubbles", "Straw", "Gravel"};colors = {Brown, Red, Orange, Green, Yellow, LightBlue};coloredTextures = MapThread[Function[{elem, c}, ImageResize[ImageApply[List@@Blend[{Black, c}, #]&, elem], 50]], {textures, colors}];GraphicsGrid[{coloredTextures}]テクスチャの棒で棒グラフを作成する.テクスチャは凡例にも表示される:
BarChart[{7, 6, 8, 5, 4, 3}, ChartElements -> coloredTextures, ChartLegends -> Placed[CharacterRange["A", "Z"], Bottom]]記号的なラッパーであるLegendedを使ってグラフの凡例を作成することもできる.
Legendedを使って,グラフの特定のデータ要素に凡例項目を加えることができる:
PieChart[{1, Legended[2, "Bob"], 3, Legended[4, "John"]}]BarChart[{1, Legended[2, "Bob"], 3, Legended[4, "John"]}, ChartStyle -> "Pastel"]Legendedは3Dのグラフ作成関数にも使用できる:
Row[{PieChart3D[{1, Legended[2, "Bob"], 3, Legended[4, "John"]}]}, {BarChart3D[{1, Legended[2, "Bob"], 3, Legended[4, "John"]}, ChartStyle -> "Pastel"]}]