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):

enter image description here

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

enter image description here

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.

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

4 Answers 4


Legends can be put inside the Graphics, for example:

Plot[{Sin[x], Cos[x]}, {x, 0, 5}, 
 PlotLegends -> Placed[LineLegend["Expressions"], {Right, Top}]]
  • $\begingroup$ This still doesn't resize along with the plot though. $\endgroup$ Dec 13, 2012 at 21:08
  • $\begingroup$ Exactly what I am looking for after 3 hours!!!!! $\endgroup$ Dec 18, 2014 at 15:02

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.

  • 1
    $\begingroup$ Right. But, the Graphics expression is the first argument to Legended, so it can easily be extracted and used without the legend. $\endgroup$
    – rcollyer
    Dec 13, 2012 at 16:52
  • $\begingroup$ @Szabolcs, Anyway to combine without rasterizing? $\endgroup$
    – kale
    Dec 13, 2012 at 16:56
  • 1
    $\begingroup$ @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 ... $\endgroup$
    – Szabolcs
    Dec 13, 2012 at 17:40
  • 1
    $\begingroup$ @kale Use Export, not the context menu, to export to PDF. It'll contain both the legend and the graphic. $\endgroup$
    – Szabolcs
    Dec 13, 2012 at 18:41
  • 1
    $\begingroup$ @kale you can also select the whole thing with the mouse (the same way you select text) and use File -> Save Selection As $\endgroup$
    – Szabolcs
    Dec 13, 2012 at 18:45

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},
    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]);


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.

  • $\begingroup$ 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! $\endgroup$
    – kale
    Dec 14, 2012 at 17:48
  • $\begingroup$ @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 $\endgroup$
    – Rojo
    Dec 14, 2012 at 17:55
  • $\begingroup$ 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. $\endgroup$
    – kale
    Dec 14, 2012 at 18:19
  • $\begingroup$ @kale that's what matters then :) $\endgroup$
    – Rojo
    Dec 14, 2012 at 18:20
  • $\begingroup$ I added an update on my question (which in hindsight was poorly worded) addressing this. $\endgroup$
    – kale
    Dec 14, 2012 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.

  • $\begingroup$ 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 $\endgroup$
    – Chris
    Sep 27, 2014 at 19:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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