# Why are PlotLegends not combined with Plots?

I'm using PlotLegends in v9. It seems that in the documentation examples, plots and legends are combined into one graphic:

Here's an example from LineLegend in the documentation:

Plot[{Sin[x], Cos[x]}, {x, 0, 5}, PlotLegends -> LineLegend["Expressions"]]


Which outputs this (notice how the legend is selected within the graph):

However, if this same line is evaluated by me, it becomes this:

Is this default behavior? Did Wolfram put graphics in place of output for their documentation? If this is default behavior, can they be combined without creating a rasterized image?

UPDATE: It turns out that Mathematica documentation sometimes contain rasterized images for output which was causing the initial confusion. The main point of the question is the behavior of the Legended (and related) function and exporting images with it included. The context menu and Export command treat this different.

-
The first example for Legended is not rasterized... –  JxB Dec 13 '12 at 19:22
Note that you can select the entire graphic, including the legend, then copy/paste it, use it as further operations, etc. –  murray Dec 13 '12 at 21:37

The reason for this is that the output in the documentation is rasterized. It is just an image, not the actual object that Plot would produce.

Legends are contained in a Legended expression, not in Graphics, which I agree does bring a number of difficulties when using these plots.

-
Right. But, the Graphics expression is the first argument to Legended, so it can easily be extracted and used without the legend. –  rcollyer Dec 13 '12 at 16:52
@Szabolcs, Anyway to combine without rasterizing? –  kale Dec 13 '12 at 16:56
@kale No, there's no way to do that with the built-in legends support. This is one of the reasons I'm not very happy with the new legending. Unfortunately I believe that it's simply impossible to implement a resize-friendly version of this. I got quite frustrated with these things in the past (see e.g. here), and I also came to the conclusion that Row/Column/Grid-like constructs are easier to handle than GraphicsGrid/Inset. So within the constraints of the current Graphics implementation Legended might actually be the best choice ... –  Szabolcs Dec 13 '12 at 17:40
@kale Use Export, not the context menu, to export to PDF. It'll contain both the legend and the graphic. –  Szabolcs Dec 13 '12 at 18:41
@kale you can also select the whole thing with the mouse (the same way you select text) and use File -> Save Selection As –  Szabolcs Dec 13 '12 at 18:45

Legends can be put inside the Graphics, for example:

Plot[{Sin[x], Cos[x]}, {x, 0, 5},
PlotLegends -> Placed[LineLegend["Expressions"], {Right, Top}]]

-
This still doesn't resize along with the plot though. –  MinHsuan Peng Dec 13 '12 at 21:08
Exactly what I am looking for after 3 hours!!!!! –  Chen Stats Yu Dec 18 '14 at 15:02

Legends seem to display as grids, unless you specify a Placed[..., Center] or some coordinates Placed[..., {0.3, 0.6}].

In these cases, they are still Legended objects, but you can extract the graphics they display like. If you used other directives such as Placed[..., Right] or After, or Left, etc, perhaps you could replace it with some approximate coordinates. For example

getTheGraphics[g_Graphics] := g;
getTheGraphics[plot_Legended] := Module[{gr},
ToBoxes[
plot /. Placed[i_, coords : _Symbol, f_] :> Placed[i, coords /. {
Left -> {-0.1, 0.5},
Right | After -> {1.1, 0.5},
Top -> {0.5, 1.1},
Bottom -> {0.5, -0.1}}, f]] /.
g_GraphicsBox :> (gr = g; Break[Null, ReplaceAll]);
ToExpression[gr]
]


so

Plot[{Sin[x], Cos[x]}, {x, 0, 5},
PlotLegends ->
Placed[LineLegend["Expressions"], Top]] // getTheGraphics


gives a Graphics

I don't know what is the desired resizing behaviour, and I also didn't read that you cared about that so far.

-
It was more a question trying to elicit the behavior of Legened. Why sometimes it combined; other times it didn't. In practice, I'll generate a plot and fidget with the legend until I like it and then have it scale with the plot. Thanks! –  kale Dec 14 '12 at 17:48
@kale, it is never combined in the sense of the output expression. It is always a Legended thing. But those Legended things are treated nicely by some functions that noremally receive graphics, for example by Show. However, not all. For example, try //FullGraphics on the above plot placed in Center (so it looks like it is combined), with and without getTheGraphics. I thought these issues were your concern –  Rojo Dec 14 '12 at 17:55
Yeah, your example helps. My problem was in Right Click > Save Graphic As... If the Legend is contained in the Plot spatially, it is saved with it. However, if it is place Below or After, it isn't saved with the exported image. Moral of the story, if I quit being lazy and wrap Export around the entire output, all problems are solved. –  kale Dec 14 '12 at 18:19
@kale that's what matters then :) –  Rojo Dec 14 '12 at 18:20
I added an update on my question (which in hindsight was poorly worded) addressing this. –  kale Dec 14 '12 at 18:24

Here's a workaround for the lazy (and those trying to copy from a Manipulate output):

1. Copy the Legend like you would copy text
2. Edit the plot via Drawing Tools (Right-Click on the plot, then "Drawing Tools")
3. Insert a text frame, paste the Legend and resize and move it as you like

Hope this helps.

-
In fact: Don't add it using a text-field, just open the plot in the Drawing perspective, then paste the legend. This way you get everything as a graphic –  Chris Sep 27 '14 at 19:33