How to| 为我的图表标注图例
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 -> {{"Group A", "Group B"}, None}, ChartStyle -> {"Pastel", None}]使用 ChartLabels 为各组数据元素创建重复的标签:
BarChart[{{1, 2, 3}, {4, 5, 6}}, ChartLegends -> {{"Group A", "Group B"}, None}, ChartStyle -> {"Pastel", None}, ChartLabels -> {"r1", "r2", "r3"}]使用 LegendAppearance 改变图例内部的标签布局,在该例中实际上是改变了图例的位置:
BarChart[{{1, 2, 3}, {4, 5, 6}}, ChartLegends -> {{"Group A", "Group B"}, None}, ChartStyle -> {"Pastel", None}, ChartLabels -> {"r1", "r2", "r3"}, LegendAppearance -> "Row"]另外,也可替代使用 Placed 将图例放到图表的底部:
BarChart[{{1, 2, 3}, {4, 5, 6}}, ChartLegends -> {Placed[{"Group A", "Group B"}, Bottom], None}, ChartStyle -> {"Pastel", None}, ChartLabels -> {"r1", "r2", "r3"}]ChartLegends 对三维图表同样有效:
BarChart3D[{{1, 2, 3}, {4, 5, 6}}, ChartLegends -> {Placed[{"Group A", "Group 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 对三维图表函数也同样有效:
Row[{PieChart3D[{1, Legended[2, "Bob"], 3, Legended[4, "John"]}]}, {BarChart3D[{1, Legended[2, "Bob"], 3, Legended[4, "John"]}, ChartStyle -> "Pastel"]}]