Working with ShowLegend

ShowLegend is an easy function to use once you understand how it works. ShowLegend creates a rectangle which can be thought of as composed of a number of cells. The number of cells in the rectangle is automatically determined by the length of the list of arguments fed to it. There are two way of giving arguments.

Loading the package

This makes all the Graphics functions available to the kernel.

<<Graphics`

Automatic arguments

The example given in the HelpBrowser is an example of giving arguments automatically. You give a color function as the first argument, the number of cells as a second argument and the first and last labels as the third and fourth arguments. Here is an example with six values with the largest 8 and the smallest -11. The DensityPlot in the example fills the rectangle with absolute coordinates {-1,-1} and {1,1}. The position of the lower left corner of the Legend is given with respect to this coordinate system. Thus a LegendPosition of {1.1, -4} places the lower left corner of the legend rectangle to the right of the plot and below the center of the vertical dimension.

ShowLegend[DensityPlot[Sin[x y],{x,0,Pi},{y,0,Pi},ColorFunction -> Hue,Mesh->False,PlotPoints->30,DisplayFunction->Identity],{Hue[1-#]&,6," 8","-11",LegendPosition->{1.1,-.4}}]

Getting more values listed

The automated method limits the number of values you can display in the legend to two. It is possible to give as many values as you wish. To use this form we need to give a list of lists as an argument to the ShowLegend function. First a simple case with three rectangles in the legend to see how the function works. We make our list using the Table function. Notice that each element of the list is a list containing a graphics object and a label.

legndA = Table[{Graphics[{Line[{{0,0},{2,0},{2,1},{0,1},{0,0}}]}],StringForm["Label[``]",i]},{i,3}];

Here we look at the form of each of the arguments for the ShowLegend list of arguments. Notice that each element of our list is a graphics object (in this case a line to make an empty box) and a label.

legndA[[1]]

legndA[[1,1,1]]

Here we use this legend with ParametricPlot. Notice the change in position of the legend. Depending on how large you make your plot you will want to experiment with the LegendSize option to get the best appearance for your legend.

ShowLegend[ParametricPlot[{{Log[10,x],Log[10,(x^2)]},{Log[10,x],Log[10,(x^3)]},{Log[10,x],Log[10,(x^4)]}},{x,1,5},Ticks -> {LogScale,LogScale},DisplayFunction -> Identity,ImageSize -> 360],{legndA,LegendPosition -> {-1.7,-.3},LegendSize -> {.62,.5}}]

We can use any graphics primitive for our legend. Here we use two lines, one dashed and the other solid. All of the style options for graphics primitives are available here. The style options should be given as the first elements of a list with the graphics object(s) as the last elements in the list.

ShowLegend[ParametricPlot[{{Log[10,x],Log[10,(x^2)]},{Log[10,x],Log[10,(x^3)]}},{x,10,1000},PlotPoints->30,PlotStyle-> {AbsoluteDashing[{2,5}],GrayLevel[0]},Ticks ->{LogScale,Map[ScientificForm[#,3]&,LogScale,{2}]},DisplayFunction->Identity,ImageSize -> 480],{{{Graphics[{AbsoluteDashing[{2,5}],Line[{{.05,.7},{.15,.7}}]}],"MyPlot"},
{Graphics[Line[{{.05,.7},{.15,.7}}]],"YourPlot"}},LegendPosition->{1.1,-.4},LegendSize -> .8}]

Here we create a list of GrayLevel squares and put a label on each. This is basically the same example as in the HelpBrowser with the values changed to go from 1 to 3.

legndB =Table[{Graphics[{GrayLevel[i],Rectangle[{0,0},{1,1}]}],StringForm["``",PaddedForm[2 i + 1,{3,2}]]},{i,0,1,.1}];

ShowLegend[DensityPlot[Sin[x y],{x,0,Pi},{y,0,Pi},Mesh->False,PlotPoints->30,DisplayFunction->Identity,ImageSize -> 480],{legndB,LegendPosition->{1.1,-.4},LegendSize -> {.5,.9}}]