1
$\begingroup$

I want to export a plot with axes as a graphic where the plot is centred within a white/transparent background region such that the centre of the plot region coincides with the centre of the background. By default, if one exports a plot, then centres the graphic within another program (Word/LaTeX etc) the plot region is not centred but the overall image.

plot = Plot[x^2, {x, 0, 5}, Frame -> True, FrameLabel -> {x, x^2}]
Export["plot.pdf", plot];

As can be seen above, the plot region (the black rectangle) is not centred within the image (the orange box).

$\endgroup$
  • 3
    $\begingroup$ Use ImagePadding, f.e. plot = Plot[x^2, {x, 0, 5}, Frame -> True, FrameLabel -> {x, x^2}, ImagePadding -> 50] $\endgroup$ – C. E. Aug 8 '16 at 14:38
  • $\begingroup$ Ahh of course, so simple. Thank you. $\endgroup$ – Tom Aug 8 '16 at 14:40
3
$\begingroup$

The straightforward way is to add equal ImagePadding to all sides of the plot what is as easy as adding ImagePadding -> 40:

plot = Plot[x^2, {x, 0, 5}, Frame -> True, FrameLabel -> {x, x^2}, 
  ImagePadding -> 40]

screenshot

Here is how exported PDF file looks when opened by Adobe Acrobat:

Export["plot.pdf", plot];

screenshot

$\endgroup$
3
$\begingroup$

If I wanted the bounding box to be the smallest size while keeping the frame in the center, I would add phantom elements to the right and top borders.

Plot[x^2, {x, 0, 5}, Frame -> True, FrameTicks -> All, 
  FrameTicksStyle -> {{Directive[Black], 
  Directive[Opacity[0]]}, {Directive[Black], 
  Directive[Opacity[0]]}}, 
  FrameLabel -> {{x, Style[x, White]}, {x^2, Style[x^2, White]}}]

See m_goldberg's comment below for better code.

$\endgroup$
  • 1
    $\begingroup$ This is good idea, but the execution could be improved. You have the frame label order wrong and the frame tick style could be written more concisely. Consider editing your code like so: Plot[x^2, {x, 0, 5}, Frame -> True, FrameTicks -> All, FrameTicksStyle -> {{Black, Transparent}, {Black, Transparent}}, FrameLabel -> {{x^2, Style[x^2, White]}, {x, Style[x, White]}}] $\endgroup$ – m_goldberg Aug 8 '16 at 17:49

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