This is documentation for Mathematica 5, which was
based on an earlier version of the Wolfram Language.
View current documentation (Version 11.1)

Documentation / Mathematica / Add-ons & Links / Standard Packages / Graphics /


There are two ways to use the functions in this package to place a legend in a graphic; the first can only be used as an option to the built-in function Plot, the second can be applied to any two-dimensional graphic. To use the PlotLegend option to Plot, you simply specify the text for each curve. If there are more curves than text, the text is used cyclically. The second way of placing a legend in a graphic is to use ShowLegend. With ShowLegend you specify the graphic and legend as arguments.

Functions for placing a legend or key in a graphic.

This loads the package.

In[1]:= << Graphics`Legend`

Here are sine and cosine curves with a legend.

In[2]:= Plot[{Sin[x], Cos[x]},
{x, -2 Pi, 2 Pi},
PlotStyle ->
{GrayLevel[0], Dashing[{.03}]},
PlotLegend -> {"Sine", "Cosine"}]


Options for use with legends.

There are various options that can be used to control the shape and characteristics of a legend. The most important of these is LegendPosition, which places the lower-left corner of a legend box in a position specified by a coordinate system scaled so the center of the graphic is at 0, 0, and the longest side of the graphic runs from to . This same coordinate system is used for LegendSize and LegendShadow.

Here is a density plot with a legend positioned using LegendPosition.

In[3]:= ShowLegend[
DensityPlot[Sin[x y], {x, 0, Pi},
{y, 0, Pi},
Mesh -> False, PlotPoints -> 30,
DisplayFunction -> Identity],
{GrayLevel[1 - #]&, 10, " 1", "-1",
LegendPosition -> {1.1, -.4}}]


More options for use with legends.

Several of the options that determine the amount of blank space around the various elements of a legend accept a number that is the ratio of the size of the space desired to the size of one of the key boxes—the colored boxes that denote a key in the legend. These options include LegendSpacing, LegendTextSpace, LegendLabelSpace, and LegendBorderSpace.

This example uses many of the options for legends.

In[4]:= Plot[{Sin[x], Cos[x]}, {x, 0, 2 Pi},
PlotStyle -> {GrayLevel[0],
{GrayLevel[0], Dashing[{.03}]}},
PlotLegend -> {"sin", "cos"},
LegendPosition -> {.5, -.7},
LegendTextSpace -> .5,
LegendLabel -> "Trig Funcs",
LegendLabelSpace -> .5,
LegendOrientation -> Horizontal,
LegendBackground -> GrayLevel[.5],
LegendShadow -> {.1, -.2},
Background -> GrayLevel[.8]]


Legend functions that do not display a graphic.

The arguments for Legend are the same as those inside the first set of braces in the description of a legend given in ShowLegend. Using Legend or ShadowBox, you can generate just the graphics primitives of a legend or box with a drop shadow to be used elsewhere in your own graphics.

This represents a box with a shadow.

In[5]:= ShadowBox[{0, 0}, {1, 1},
ShadowBackground -> GrayLevel[.7]]


Here is what the box looks like.

In[6]:= Show[Graphics[%]]


Options for ShadowBox.