# How can PlotLegends be added to Show

Every once in a while I would like to add PlotLegends to identify graphics combined with the Show command, but Show does not accept PlotLegends as an option. Generally, I think it better to handle the legends individually in the component plots, but every once in a while I have this need.

p1 = Plot[Sin[x], {x, 0, 4 Pi}, PlotStyle -> Red];

p2 = ListLinePlot[Table[{x, Cos[x]}, {x, 0, 2 Pi, .2 Pi}],
PlotStyle -> Green];

p3 = ListLinePlot[Table[{x, Cos[x + Pi/3]}, {x, 0, 3 Pi, .2 Pi}],
PlotStyle -> Blue];

Legended[Show[p1, p2, p3],
Placed[SwatchLegend[{Red, Green, Blue}, {"A", "B", "C"}], {.9, .8}]]


• Thank you, @Okkes Dulgerci. I accepted you answer because it shows how to do exactly what I was trying to do, by using the standard tools and not resorting to a fake plot. I is very useful. – David Keith Jul 6 '18 at 20:32

And I answer this myself, to share one method. But I hope for better answers. I find that I can generate a blank plot by Plotting the imaginary Unit I. Plot will accept that and allow PlotLegends to be added to the blank plot, which can then be added to the sequence of graphics given to Show.

p1 = Plot[Sin[x], {x, 0, 4 Pi}, PlotStyle -> Red];

p2 = ListLinePlot[Table[{x, Cos[x]}, {x, 0, 2 Pi, .2 Pi}],
PlotStyle -> Green];

p3 = ListLinePlot[Table[{x, Cos[x + Pi/3]}, {x, 0, 3 Pi, .2 Pi}],
PlotStyle -> Blue];

lp = Plot[I, {x, 0, 1},
PlotLegends ->
Placed[SwatchLegend[{Red, Green, Blue}, {"A", "B",
"C"}], {.9, .8}]];

Show[p1, p2, p3, lp]


• What is the "I" in lp? – David G. Stork Jul 5 '18 at 22:58
• The I is the imaginary unit. Plot accepts this as an expression, but cannot plot it. And I agree with kglr. I usually put PlotLegend options in each plot and let Show combine them. – David Keith Jul 5 '18 at 23:13
• @kglr: Right. But didn't the OP explicitly reject going back to re-plot existing plots to add legends? Or (from his example code) NOT put in the option to begin with? If the option is already in each component graph, well then the problem is solved. – David G. Stork Jul 5 '18 at 23:13
• Also, note that if the blank plot is listed first, Show will get its options from it. So to force a lot of options onto the combined plot -- like a PlotLabel, PlotRange, etc -- You can add them to the blank plot and list it first. – David Keith Jul 5 '18 at 23:15
• thank you @DavidG.Stork; amazing how i managed to miss the main requirement in a three-line question:) – kglr Jul 5 '18 at 23:15

An alternative to generating a fake plot is to build a graphics object directly from your SwatchLegend:

Show[
p1, p2, p3,
Graphics[
Inset[SwatchLegend[{Red, Green, Blue}, {"A", "B", "C"}], Scaled@{.9, .8}]
]
]


• Thank you @MarcoB. That works. And it works for much more than adding a legend. I frequently want to add thing to a plot. For example, I doing work now in which I process images fit derived data to a model. I will use this to add the name of the processed file and the determined parameters to the plot. – David Keith Jul 6 '18 at 20:35