"Plot Legends" Package

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 functions Plot, ListPlot, and ListLinePlot, while the second can be applied to any graphic. To use the PlotLegend option, 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.

PlotLegend->{text1,text2,...}option for Plot to place a legend with text for each curve
ShowLegend[graphic,legend1,legend2,...]place the in the given graphic

Methods for placing a legend in a graphic.

This loads the package.
In[1]:=
Click for copyable input
Here are sine and cosine curves with a legend.
In[2]:=
Click for copyable input
Out[2]=
Here are list plots with a legend.
In[3]:=
Click for copyable input
Out[3]=
{{{box_(1),text_(1)},... },opts}specification of a legend, with color primitives or graphics for the and expression suitable for placement in Text primitives in the
{colorfunction,n,minstring,maxstring,opts}specification of a legend with n boxes, each colored with the colorfunction with optional strings placed by the end

Legend formats for ShowLegend.

Here is a density plot with a legend.
In[4]:=
Click for copyable input
Out[4]=
Here is a surface plot with a legend.
In[5]:=
Click for copyable input
Out[5]=
Plot with a legend for the filling styles.
In[6]:=
Click for copyable input
Out[6]=
LegendPositionspecify the position of the legend in relation to the graphic, where the center of the graphic is at
LegendSizegive the length or dimensions in the same coordinate system as LegendPosition
LegendShadow None gives no drop shadow for the box, gives an offset for the drop shadow
LegendOrientation or determines the orientation of the boxes
LegendLabellabel for the legend
LegendTextDirectiondirection of the text is rotated, as in a Text graphics primitive
LegendTextOffsetoffset of text, as a Text graphics primitive

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 , and the longest side of the graphic runs from to . This same coordinate system is used for LegendSize and LegendShadow.

LegendSpacingspace around each key box in the legend, expressed as the ratio of the space to the size of the key box
LegendTextSpacespace allocated next to the key boxes for text, expressed as in LegendSpacing
LegendLabelSpacespace allocated for the legend label, as in LegendSpacing
LegendBorderSpacespace allocated around the entire legend, as in LegendSpacing
LegendBorderstyle for a border line around the legend box; can only be used if LegendShadow is not None
LegendBackgroundstyle for a background to the legend; can only be used if LegendShadow is not None
ShadowBackgroundcolor for the legend's drop shadow

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[7]:=
Click for copyable input
Out[7]=
Legend[legendargs,opts]give the graphics primitives that describe an individual legend
ShadowBox[pos,size,opts]give the graphics primitives for a box with a drop shadow

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[8]:=
Click for copyable input
Out[8]=
Here is what the box looks like.
In[9]:=
Click for copyable input
Out[9]=
ShadowOffsetgive the and offset of the shadow from the box
ShadowBackgroundgive the color of the shadow
ShadowForegroundgive the color of the box with the shadow
ShadowBordergive a graphics primitive or list of primitives denoting the style of the line around the box

Options for ShadowBox.

New to Mathematica? Find your learning path »
Have a question? Ask support »