11
$\begingroup$

I need to export plots as PDF with a given image and font size. Everything is working fine if there is no legend for that plot. However, if I want to export legended plots, the font size is decreased in the exported PDF. Here is a small working example:

plotWithoutLegend = 
 ListPlot[Table[f, {f, {Sin[x], Cos[x]}}, {x, 0, 2 \[Pi], 0.1}], 
  Frame -> True, FrameLabel -> "Frame label font size is 12", 
  ImageSize -> 200, BaseStyle -> {FontSize -> 12}];
Export["plotWithoutLegend.pdf", plotWithoutLegend]

This yields the expected font size. However, when trying to export:

plotWithLegend = 
 ListPlot[Table[f, {f, {Sin[x], Cos[x]}}, {x, 0, 2 \[Pi], 0.1}], 
  Frame -> True, PlotLegends -> {"sin(x)", "cos(x)"}, 
  FrameLabel -> "Frame label font size is about 8 pt", 
  ImageSize -> 200, BaseStyle -> {FontSize -> 12}];
Export["plotWithLegend.pdf", plotWithLegend]

the font size of the frame label is about 8 pt. This can be seen by importing the PDFs in a LaTeX document or by verifying the font size with Acrobat Pro.

Is this a bug? Is there a workaround for this behavior?

$\endgroup$
2
  • $\begingroup$ I suspect this may have to do with stackoverflow.com/questions/6091553/… , even Mr.Wizard's proposed solution doesn't immediately fix it in this case. To be precise, the font size in the second case is 8.64 pt (according to Acrobat). However, if you export to PNG, the pixel size of fonts seems identical. $\endgroup$
    – Szabolcs
    Commented Mar 4, 2013 at 19:56
  • $\begingroup$ Speculating on the reason for the difference: if you plot without a legend, the output is a Graphics object. If you plot with a legend, it's more akin to a Row (i.e. a formatted notebook expression) $\endgroup$
    – Szabolcs
    Commented Mar 4, 2013 at 20:02

1 Answer 1

6
$\begingroup$

I think the problem is that the exported PDF file doesn't contain the correct bounding box information appropriate for the image size that you specified. Maybe an acceptable work-around is to specify this information by hand as follows:

fixSize[plt_, width_, height_] := Graphics[
  {
   Inset[plt]
   },
  ImageSize -> {width, height},
  AspectRatio -> Full
  ]

g1 = fixSize[plotWithLegend, 260, 150]

g1

g2=fixSize[plotWithoutLegend,260,150]

g2

By wrapping both plots as Insets in another Graphics with the same fixed dimensions in both cases, the output of Export is guaranteed to have the same size, measured in pixels.

This is where the manual adjustment is required: Here I chose the dimensions 260$\times$150 such that the legend fits into the given bounding box. That is why the pictures now look shifted horizontally with respect to each other: they are centered in the same amount of space, and the one without legend uses less room.

It's easy to modify this so that the plots are both left-aligned (e.g.), by using the additional arguments of Inset. Just replace it by Inset[plt, {Left, Bottom}, {Left, Bottom}]

In the exported PDF file, all the fonts will have their expected size in points.

$\endgroup$
1
  • 1
    $\begingroup$ Thanks, this works for me. However, tuning the dimensions of the bounding box manually is quite cumbersome... $\endgroup$
    – Robinaut
    Commented Mar 5, 2013 at 12:22

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.