How Do I Add Legends to Charts and Plots?
With Mathematica, you can easily create plots of two-dimensional
data or represent data in graphics such as bar charts or pie charts.
Very often it will be useful to create legends for these graphics to aid
in the interpretation of your data. The standard add-on package
Graphics`Legend` offers a set of flexible graphics objects to do
just that.
Legends for Plots of Functions
When using the function Plot, the option PlotLegend may be used to
quickly and automatically add a legend.
Example 1.1
![[Graphics:Images/index_gr_1.gif]](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"}]
By default, Mathematica will automatically choose a size and
location for the legend box. You may use LegendPosition and
LegendSize to specify where you want your legend
to go and how big it should be. These specifications should be made using
the coordinate system below. Note that the coordinate system for
LegendPosition and LegendSize is
independent of your coordinate system for your plot.
Below is an example of manually defining the size (0.5 wide by 0.25 high) and
position (0.5, 0.3) for the legend.
Example 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}]
Legends for Plots of Data
Legends can also be added to data plots using the function
ListPlot in essentially the same way as function plots.
Example 2.1
![[Graphics:Images/index_gr_5.gif]](Images/index_gr_5.gif)
![[Graphics:Images/index_gr_6.gif]](Images/index_gr_6.gif)
MultipleListPlot[{datalist1, datalist2}, PlotJoined -> {True, False}, PlotStyle -> {RGBColor[1, 0, 0], RGBColor[0, 0, 1]}, PlotLegend -> {"Sine", "Cosine"}]
Legends for Charts and Other Graphics
The option PlotLegend cannot be used to add legends to other
objects such as BarChart, PieChart, etc. For these objects,
the legend must be created using the function
ShowLegend[graphics
object, legend object] and Mathematica's library of
graphics primitives.
Start with a simple bar chart as follows.
![[Graphics:Images/index_gr_8.gif]](Images/index_gr_8.gif)
![[Graphics:Images/index_gr_9.gif]](Images/index_gr_9.gif)
And now add a legend by wrapping the previous chart in the
function ShowLegend and defining the legend's fields and labels.
Example 3.1
![[Graphics:Images/index_gr_11.gif]](Images/index_gr_11.gif)
Of course the same technique can be used on any graphic or array of graphics.
Example 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} ]
| |