8
$\begingroup$

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?

$\endgroup$
  • $\begingroup$ 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. $\endgroup$ – chuy Apr 7 '14 at 14:42
  • $\begingroup$ 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] $\endgroup$ – Pincopallino Apr 7 '14 at 15:14
  • $\begingroup$ Ah very true indeed. $\endgroup$ – chuy Apr 7 '14 at 15:37
  • $\begingroup$ Problem still there in V 10.1... $\endgroup$ – Alexey Bobrick Apr 1 '15 at 12:20
3
$\begingroup$

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]];
$\endgroup$
  • $\begingroup$ 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! $\endgroup$ – Pincopallino Apr 7 '14 at 16:12
  • $\begingroup$ Yes, sorry I see I was using a slightly different plot. I will correct this. $\endgroup$ – chuy Apr 7 '14 at 16:15
  • 1
    $\begingroup$ On Mathematica 11.2 this works: makeInset[plot_Legended] := Show[{plot[[1]], Graphics[Inset[plot[[2, 1]], Scaled[plot[[2, 2]]]]]}]. (The code in the current answer fails with "Coordinate {"OU", "FBM"} should be a pair of numbers.") $\endgroup$ – pcworld Jun 28 '18 at 17:19
0
$\begingroup$

The problem is still present in Mathematica 10.3. and it appears also if plots are combined with Grid (or Column, ...) before being exported to a pdf. So maybe PlotLegend uses internally a Grid-like function.

Interestingly, the scaling factor 6.48 cm / 9 cm = 0.72 matches exactly the Magnification value of the default Printout environment (Format > Edit Stylesheet... > Default.nb > section Style Environment Names > select Printout > open Option Inspector > search Magnification). Therefore, this problem can be solved by choosing

SetOptions[$FrontEnd, PrintingStyleEnvironment -> "Working"]

the Working environment according to https://mathematica.stackexchange.com/a/11988/38534.

$\endgroup$

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.