Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

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.

share|improve this question
    
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

3 Answers 3

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.

share|improve this answer
    
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 I also have to ask: why do you want Graphics? This Legended thing can be exported to graphics formats just fine. It can also be included in Graphics as Inset. The "only" problem you'll have is resizing. You can't create figures which have legends and will look good even after resizing. This is partly due to how Mathematica has both graphics coordinates and offset coordinates (actual physical untis), which sometimes conlfict .... –  Szabolcs Dec 13 '12 at 17:42
1  
@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

Legends can be put inside the Graphics, for example:

Plot[{Sin[x], Cos[x]}, {x, 0, 5}, 
 PlotLegends -> Placed[LineLegend["Expressions"], {Right, Top}]]
share|improve this answer
    
This still doesn't resize along with the plot though. –  MinHsuan Peng Dec 13 '12 at 21:08

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.

share|improve this answer
    
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

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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