views:

76

answers:

4

How do you plot legends for functions without using the PlotLegends package?

A: 

You could use other graphics primitives like Line[] to build up the legend and then use Text[] to place text. This is slow and painful. On the very rare occasion that I have not been able to get Mathematica to produce an acceptable legend, I have exported the plot in a vector (e.g. eps) format and used Corel Draw to make my own.

EDIT: If this is about having an older version of Mathematica, then try the Graphics'Legend' package via, e.g.

<< Graphics`Legend`;
Plot[{x, x^2}, {x, 0, 1}, PlotLegend -> {"one", "two"}]

which should be good for versions >= 5.2 of Mathematica.

Timo
A: 

@Timo I think the OP means if you don't have access to the PlotLegends package. Try using ToolTip.

Bebe
A: 

I would also be very interested in an answer to this question.

To tell you what is wrong with PlotLegends: It is terribly unstable and in many instances doesn't work at all.

Here is an example where PlotLegends screws up completely. Output is from Mathematica 7.0:

Assume that we have measured some data points corresponding to a number of functions, and we want to show how well they compare to the ideal function, or maybe how well they match with a calculated fit. No problem! We'll just Show[] the smooth plot together with a ListPlot of the data points, right?

It could look something like this:

Show[
 Plot[{Sin[x], Sinh[x]}, {x, -Pi, Pi}],
 ListPlot[Join[{#, Sin[#]} & /@ Range[-Pi, Pi, .5], 
               {#, Sinh[#]} & /@ Range[-Pi, Pi, .5]]]
 ]

alt text

Now we'd like to put a legend on the plot, so readers will know what on earth they're looking at. Easier said than done, mister! Let's add the PlotLegend to the Plot[]:

Show[
  Plot[{Sin[x], Sinh[x]}, {x, -Pi, Pi}, PlotLegend -> {Sin[x], Sinh[x]}],
  ListPlot[Join[{#, Sin[#]} & /@ Range[-Pi, Pi, .5], 
                {#, Sinh[#]} & /@ Range[-Pi, Pi, .5]]]
 ]

alt text

This looks GREAT! Publish immediately!

For such a basic and ubiquitously needed functionality, it sure has been a lot of work to find an alternative to PlotLegend that just works. The best alternative I've found so far has been to meticulously construct a list of plotstyles, then construct the legend by hand, and finally to show it together with the plot using ShowLegend[]. (See for example here) It's possible, but a lot of work.

So if anyone knows of a workaround to make PlotLegend work, an alternative package that works better, or just a neat way to get legends that can be automated easily, I would be very grateful! It would certainly make life a little bit easier.

James
A: 

I, too, was disappointed by the difficulty of getting PlotLegend work correctly. I wrote my own brief function to make my own custom figure legends:

makePlotLegend[names_, markers_, origin_, markerSize_, 
   fontSize_, font_] := 
  Join @@ Table[{Text[Style[names[[i]], FontSize -> fontSize, font], 
      Offset[{1.5*markerSize, -(i - 0.5)*
         Max[markerSize, fontSize]*1.25}, Scaled[origin]], {-1, 0}], 
     Inset[Show[markers[[i]], ImageSize -> markerSize], 
      Offset[{markerSize/2, -(i - 0.5)*
         Max[markerSize, fontSize]*1.25}, Scaled[origin]], {0, 0}, 
      Background -> Directive[Opacity[0], White]]}, {i, 1, 
     Length[names]}];

It is flexible, but not so easy to use. "names" is a list of strings to render in the legend; "markers" is a list with the same length as "names" of Graphics objects representing the plot markers or graphics to render; "origin" is a two-element list with the absolute horizontal and vertical position of the upper-left corner of the legend; "markerSize" is the number of points to scale the markers to; "fontSize" is the font size; "font" is the name of the font to use. Here is an example:

Plot[
 {x, x^2},
 {x, 0, 2},
 PlotStyle -> {Blue, Red},
 Epilog -> makePlotLegend[
   {"x", "x^2"},
   (Graphics[{#, Line[{{-1, 0}, {1, 0}}]}]) & /@ {Blue, Red},
   {0.9, 0.3},
   12,
   12,
   "Arial"
   ]
 ]
the_one_smiley