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}, 
LegendShadow -> {0, 0}};

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

It only outputs this with no legends present. ShowLegends showleg

  • 2
    $\begingroup$ Did you load the legend package Needs["PlotLegends`"]? $\endgroup$ – xslittlegrass May 14 '13 at 23:13
  • $\begingroup$ I don't think I need to? I have Mathematica 9.0 where it's not required? $\endgroup$ – l3win May 14 '13 at 23:32
  • $\begingroup$ @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) $\endgroup$ – rm -rf May 14 '13 at 23:36
  • $\begingroup$ 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.? $\endgroup$ – l3win May 15 '13 at 1:35
  • $\begingroup$ The whole DisplayFunction maneuvering that the code is doing is very pre version 6. [cont...] $\endgroup$ – Sjoerd C. de Vries May 15 '13 at 8:45

The package PlotLegends` is obsolete in v9, and so is the package Graphics`Legend` 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.

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

enter image description here

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:

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

When applied to CLegend from the notebook, we get


enter image description here

This simplifies the conversion to

  Show[CScatPlot, CAbsPlot, CExtPlot],

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

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.