Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

Possible Duplicate:
Using PlotLegends with Show messes up the graphics

I have created a composite plot function:

enter image description here

I want to add a legend to it, but << PlotLegends` seems not to work, when inside a Show (Mathematica 8). Plots will probably be printed, so Tooltip won't work. How can I create a legend for a plot?

share|improve this question
1  
please post the supporting code so we can see what can be done with it to solve your issue. –  R Hall Apr 8 '12 at 16:43
add comment

marked as duplicate by Mr.Wizard Apr 8 '12 at 20:22

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

2 Answers

up vote 4 down vote accepted

Use ShowLegend when you need to use the package's functionality with Show. Here's an example:

eqns = {Sin[3 Tanh[#]], Sin[Erf[#]]} &;
colors = {Blue, Red};
legends = TraditionalForm /@ eqns[x];

plot1 = Plot[Evaluate@eqns[x], {x, 0, π}, PlotStyle -> colors, 
    Filling -> {1 -> {{2}, LightGreen}}];
plot2 = ListPlot[Select[RandomReal[{0, Pi}, {1000, 2}], 
    IntervalMemberQ[Interval[Sort[eqns[First@#]]], Last@#] &], PlotStyle -> Darker@Green];

ShowLegend[Show[plot1, plot2], {{Graphics[{#1, Thick, Line[{{0, 0}, {1, 0}}]}], #2} & @@@ 
   Transpose[{colors, legends}], LegendPosition -> {-0.65, -0.5}, 
   LegendSpacing -> 0, LegendShadow -> None, LegendSize -> 0.6}]

enter image description here

share|improve this answer
    
+1 for the cool plot. I'll keep quiet about the legend aesthetics... –  Jens Apr 8 '12 at 19:16
    
@Jens I had to restrain myself heavily to not say anything about the package and the quality of the output. I absolutely dislike PlotLegends, but for some reason, people continue to use it (and most likely don't want to be bothered with using primitives). –  rm -rf Apr 8 '12 at 19:23
    
+1 for great answer. Exactly what I was hoping for :D Btw, to get default 'colors = Table[Hue[FractionalPart[0.67 + 2.0 (i - 1)/GoldenRatio], 0.6, 0.6], {i, 1, 2}];' –  Margus Apr 8 '12 at 20:18
add comment

You could perhaps use the legendMaker function I posted here (or a modified version of it). I use it with Overlay instead of Show. So you would do:

legendedPlot = Overlay[{plot, legendmaker[...]},Alignment->{...}]

Then you can export the result as

Export["file.pdf", legendedPlot]

Instead of exporting, the output of Overlay can also be copied into other applications (even though the overlay box isn't an editable graphic by default). Just highlight the output graphic and do a usual copy operation.

Edit

Thanks to R.M. for the nice example plot, which I couldn't resist trying to label my way, too... so here I repeat R.M.'s definition first,

eqns = {Sin[3 Tanh[#]], Sin[Erf[#]]} &;
colors = {Blue, Red};
legends = TraditionalForm /@ eqns[x];

plot1 = Plot[Evaluate@eqns[x], {x, 0, Pi}, PlotStyle -> colors, 
   Filling -> {1 -> {{2}, LightGreen}}];
plot2 = ListPlot[
   Select[RandomReal[{0, Pi}, {1000, 2}], 
    IntervalMemberQ[Interval[Sort[eqns[First@#]]], Last@#] &], 
   PlotStyle -> Darker@Green];

and now I label it to show the difference to the Legends package:

Overlay[{Show[plot1, plot2], 
  legendMaker[legends, PlotStyle -> colors]}, Alignment -> {-.5, -.8}]

labeled plot

share|improve this answer
    
Nice, you could also make your legend a Locator like in Simon's answer here, so that it can also be moved around where you want it — sometimes simpler than fiddling with positioning. –  rm -rf Apr 8 '12 at 19:27
add comment

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