Mathematica Stack Exchange is a question and answer site for users of Mathematica. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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, however << 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 this plot?

share|improve this question

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.

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
@RHall Note the use of ShowLegend instead Show. I did roll my own custom graphics, because I found it easier to archive prettier results. – Margus Sep 22 '15 at 13:37
up vote 7 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). – R. M. 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

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.


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. – R. M. Apr 8 '12 at 19:27

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