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?

  • 1
    $\begingroup$ please post the supporting code so we can see what can be done with it to solve your issue. $\endgroup$ – R Hall Apr 8 '12 at 16:43
  • $\begingroup$ @RHall Note the use of ShowLegend instead Show. I did roll my own custom graphics, because I found it easier to archive prettier results. $\endgroup$ – Margus Sep 22 '15 at 13:37

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

  • $\begingroup$ +1 for the cool plot. I'll keep quiet about the legend aesthetics... $\endgroup$ – Jens Apr 8 '12 at 19:16
  • $\begingroup$ @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). $\endgroup$ – rm -rf Apr 8 '12 at 19:23
  • $\begingroup$ +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}];' $\endgroup$ – 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

  • $\begingroup$ 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. $\endgroup$ – rm -rf Apr 8 '12 at 19:27
  • $\begingroup$ It seems this once was the state of the art for creating legends. Since quite a few years have passed since this answer was written and we are a few Mathematica versions further I was wondering whether this is this still the state of the art way to create labels. Or is using built in Mathematica functions now the way to go? $\endgroup$ – Kvothe Mar 7 '19 at 11:18
  • $\begingroup$ @Kvothe I still use the approach in my linked answer because it's less verbose than the built-in functionality - and because it gives you Graphics objects as output, instead of Legended which can cause confusion because it's an invisible wrapper (i.e., you get plot outputs that are of different Head merely by adding legends with the built-in option to the same plot command - which I think is terrible design). What I really use mostly is autoLegend from my other answer. $\endgroup$ – Jens Mar 7 '19 at 15:21

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