7
$\begingroup$

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.

$\endgroup$
7
$\begingroup$

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}]]

enter image description here

$\endgroup$
  • $\begingroup$ 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. $\endgroup$ – David Keith Jul 6 '18 at 20:32
4
$\begingroup$

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]

enter image description here

$\endgroup$
  • $\begingroup$ What is the "I" in lp? $\endgroup$ – David G. Stork Jul 5 '18 at 22:58
  • $\begingroup$ 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. $\endgroup$ – David Keith Jul 5 '18 at 23:13
  • $\begingroup$ @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. $\endgroup$ – David G. Stork Jul 5 '18 at 23:13
  • $\begingroup$ 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. $\endgroup$ – David Keith Jul 5 '18 at 23:15
  • $\begingroup$ thank you @DavidG.Stork; amazing how i managed to miss the main requirement in a three-line question:) $\endgroup$ – kglr Jul 5 '18 at 23:15
2
$\begingroup$

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}]
 ]
]

Mathematica graphics

$\endgroup$
  • $\begingroup$ 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. $\endgroup$ – David Keith Jul 6 '18 at 20:35

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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