# Concerning ShowLegend in Mathematica

I found a code for Mie Scattering from 2007, written for Mathematica.

I have edited it quite a bit, since there are package definitions that we don't need, etc.

But I am having difficulties figuring out why the plot with ShowLegends does not work.

The command is

CLegend = {{{RGBColor[1, 0, 0],
"\!$$\*SubscriptBox[\(C$$, $$scat$$]\)"}, {RGBColor[0, 0, 1],
"\!$$\*SubscriptBox[\(C$$, $$abs$$]\)"}, {RGBColor[0, 0, 0],
"\!$$\*SubscriptBox[\(C$$, $$ext$$]\)"}},
LegendPosition -> {-.55, 0.3}, LegendSize -> {0.25, 0.25},

ShowLegend[
Show[CScatPlot, CAbsPlot, CExtPlot,
DisplayFunction -> Identity], DisplayFunction,
CLegend -> \$DisplayFunction ]


It only outputs this with no legends present.

-
Did you load the legend package Needs["PlotLegends"]? –  xslittlegrass May 14 '13 at 23:13
I don't think I need to? I have Mathematica 9.0 where it's not required? –  l3win May 14 '13 at 23:32
@l3win You are using old functions from the PlotLegends package, which xslittlegrass mentioned. If you want to use v9 functions, you'll have to translate them yourself to use the newer ones. Did you try loading it first to see what happens? (ignore the warning) –  rm -rf May 14 '13 at 23:36
rm I just loaded the command. See my original post. I am not sure, I think I don't have to change the LegendSize, LegendPosition or LegendShadow. Does it have to do with RBGColor etc.? –  l3win May 15 '13 at 1:35
The whole DisplayFunction maneuvering that the code is doing is very pre version 6. [cont...] –  Sjoerd C. de Vries May 15 '13 at 8:45

The package PlotLegends is obsolete in v9, and so is the package GraphicsLegend that the Mie code notebook was using. Since you are running v9, you might as well use the new stuff which is integrated much better. Of course, as there are several plots to combine, the method is going to look similar, but not as convoluted as the old form. The high level object which represents a legend is Legended, and it can be used as follows:

Legended[ graphics object, Placed[legend]]


In this case, the graphics object is generated by Show, but note there is no need to supply DisplayFunction -> Identity to it anymore. The legend specification is also fairly straightforward, and I felt LineLegend was the most appropriate.

Legended[
Show[CScatPlot, CAbsPlot, CExtPlot],
Placed[
LineLegend[
{Red, Blue, Black},
{"\!$$\*SubscriptBox[\(C$$, $$scat$$]\)",
"\!$$\*SubscriptBox[\(C$$, $$abs$$]\)",
"\!$$\*SubscriptBox[\(C$$, $$ext$$]\)"}
],
Right
]
]


You will have to alter each plot by hand, but it seems all of the legend specifications in the notebook have the same form:

{{{color, label} ..}, options}


So, we can easily create a conversion function:

Clear[ToNewLegend]
ToNewLegend[{spec_, ___}, pos_:Right, opts:OptionsPattern[LineLegend]] :=
Placed[LineLegend[##, opts], pos]& @@ Transpose[spec]


When applied to CLegend from the notebook, we get

ToNewLegend[CLegend]


This simplifies the conversion to

Legended[
Show[CScatPlot, CAbsPlot, CExtPlot],
ToNewLegend[CLegend]
]


with the possibility of changing positions of the legend or supplying alternative options to it, as desired.

-