How can PlotLegends be added to Show
How about this?
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}]]
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]
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}]
]
]