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 have Mathematica 9.0.1, and I have problems with PlotLegends breaking the ImageSize directive.

I defined

cm = 72 / 2.54;

to work with centimeters. I would like an output PDF with a width of, say, 9 cm.

Plot[Evaluate[
  Exp[-4 f] /. {{f -> t}, {f -> t^2}}], {t, 0, 3}, 
  BaseStyle -> {FontSize -> 11, AbsoluteThickness[1.5]}, 
  PlotStyle -> {Blue, Red}, 
  AxesLabel -> {"t"},  
  PlotLabel -> "Negativity", 
  ImageSize -> 9 cm,  
  PlotLegends -> Placed[{"OU", "FBM"}, {.8, .5}]]
Export["Neg_long_OU_FBM.pdf", %];

With this command, I obtain a PDF with size 6.48 × 4.31 cm. If I remove the PlotLegends option, I get the correct width of 9 cm. If I leave PlotLegends without the Placed directive (the legend is placed at the right of the Plot), I get 9.42 × 4.38 cm.

Is this an expected behavior? How can I produce an output PDF with a 9 cm width and the legend inside the plot?

share|improve this question
    
Not a complete answer, but using Export["Neg_long_OU_FBM.pdf", %, ImageSize -> 9 cm]and not setting the ImageSize in the Plot gets you much closer. –  chuy Apr 7 at 14:42
    
I think that this is just a coincidence, as the output of Plot (without setting ImageSize) has a size of 9.15 cm. In fact I obtain the same result with Export["...", %], Export["...",%, ImageSize -> 9 cm ] or Export["...", %, ImageSize -> 2 cm] –  Pincopallino Apr 7 at 15:14
    
Ah very true indeed. –  chuy Apr 7 at 15:37

1 Answer 1

up vote 2 down vote accepted

This might help. Here I have a helper function to make the legend into an Inset:

plot = Plot[Evaluate[Exp[-4 f] /. {{f -> t}, {f -> t^2}}], {t, 0, 3}, 
   BaseStyle -> {FontSize -> 11, AbsoluteThickness[1.5]}, 
   PlotStyle -> {Blue, Red}, AxesLabel -> {"t"}, 
   PlotLabel -> "Negativity", 
   PlotLegends -> Placed[{"OU", "FBM"}, {.8, .5}], ImageSize -> 9 cm];

makeInset[plot_Legended] := 
 Show[{plot[[1]], 
   Graphics[
    Inset[plot[[2, 1, 1]], Scaled[plot[[2, 1, 2]]]]]}]

and then wrap plot with this function:

Export["Neg_long_OU_FBM.pdf", makeInset[plot]];
share|improve this answer
    
This workaround works like charm! I just had to slightly modify the Part of like this makeInset[plot_Legended] := Show[{plot[[1]], Graphics[Inset[plot[[2, 1, 1]], Scaled[plot[[2, 1, 2]]]]]}] to make it work with my code. Thank you! I'm still wondering if there are cleaner solutions! –  Pincopallino Apr 7 at 16:12
    
Yes, sorry I see I was using a slightly different plot. I will correct this. –  chuy Apr 7 at 16:15

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.