5
$\begingroup$

I have been having problems with exported plots cutting off a pixel or two of the text in the title of my plots.

Let's assume we have some data (AR(1) noise with a high autoregressive parameter makes good fake economic data):

testdata =  FoldList[0.99 #1 + #2 &, 1. , 
 RandomVariate[NormalDistribution[0, 0.5], {100}] ];

And a title for our graph. We can try:

title =
 Column[{Style["This is a test", 24, Bold, FontFamily -> "Arial", Black], 
    Style["And so is this", 20, FontFamily -> "Arial", Black]}, 
  Center, 0.3, BaselinePosition -> Top]

enter image description here

which does give a bit of whitespace at the top. We can also try another setting for BaselinePosition as below. As far as I can tell, all the settings other than Top look like this.

titleb = Column[{Style["This is a test", 24, Bold,  FontFamily -> "Arial", Black],
   Style["And so is this", 20, FontFamily -> "Arial", Black]}, 
  Center, 0.3, BaselinePosition -> Baseline]

enter image description here

Now consider what this looks like as a title to a plot:

ListLinePlot[testdata, 
 BaseStyle -> {FontFamily -> "Arial", FontSize -> 16}, 
 ImageSize -> 400, PlotLabel -> title, Frame -> True, 
 ImagePadding -> {{Automatic, Automatic}, {Automatic, 1}}]

enter image description here

The linespacing is suddenly a bit wider than required, and there is far too much whitespace above the title. (This would be obvious if the whole plot were Framed, but if I do that, the next issue doesn't arise, and I don't want a border around the whole plot anyway.)

Now consider the case where the title has BaselinePosition other than top. Now the linespacing is still wider than the original column, and there is no whitespace at all above the text. This causes problems when I export the graphic (especially to EMF under Windows for use in PowerPoint or Word) because then the top pixel or so of the text is cut off. (This seems to be related to this issue.)

testplotb = 
 ListLinePlot[testdata, 
  BaseStyle -> {FontFamily -> "Arial", FontSize -> 16}, 
  ImageSize -> 400, PlotLabel -> titleb, Frame -> True, 
  ImagePadding -> {{Automatic, Automatic}, {Automatic, 1}}]

enter image description here

If I adjust ImagePadding, it increases the space between the plot frame and the title, not above the title. I get the same problem even if I change Column to Grid or GridBox with the relevant options. Adjusting Spacings doesn't seem to work for whitespace at the top of the first element, only for the spacing between rows/elements.

How can I add just a pixel or two to the whitespace between the top of my custom plotlabel and the edge of an exported graphic? Ideally I'd like to keep the linespacing a little tighter, consistent with how it looks outside the plot, as well.

$\endgroup$
3
  • $\begingroup$ Possible duplicate of mathematica.stackexchange.com/q/7237/245 ? $\endgroup$
    – Mr.Wizard
    Jul 6, 2012 at 21:54
  • $\begingroup$ @Mr.Wizard The main new issue I see in this question is the last paragraph: we look at the line spacing that is set in the Column of title and see it's intentionally narrow. But it appears ever so slightly wider when used in the PlotLabel. So that's the issue I focused my attention on in the answer below: how can the two appearances be kept the same? $\endgroup$
    – Jens
    Jul 6, 2012 at 21:58
  • $\begingroup$ Related but not duplicate. Neither ImageMargins nor ImagePadding put white space above the title in a way that prevents cropping in EMF export, and the line spacing issue is weird. Because I have a custom function to make the title and I use it in both PlotLabel and as a separate grid element, I want some consistency. $\endgroup$
    – Verbeia
    Jul 6, 2012 at 22:06

5 Answers 5

1
$\begingroup$

I don't have Windows at the moment so cannot test the exporting but just throwing some other ideas out there that will add pixels and may (or may not?) prevent the cropping that you have observed:

1.Use Pane and adjust the top image margin.

newtitle=Pane[title, 400, ImageMargins -> {{0, 0}, {0, 5}}]

ListLinePlot[testdata, 
 BaseStyle -> {FontFamily -> "Arial", FontSize -> 16}, 
 ImageSize -> 400, PlotLabel -> newtitle, Frame -> True, 
 ImagePadding -> {{Automatic, Automatic}, {Automatic, 1}}]

2.Use Labeled with either the original title or the "paned" title

plot = Labeled[
  ListLinePlot[testdata, 
   BaseStyle -> {FontFamily -> "Arial", FontSize -> 16}, 
   ImageSize -> 400, Frame -> True, 
   ImagePadding -> {{Automatic, Automatic}, {Automatic, 
      1}}], {newtitle}, {Top}]

Export["test.emf", plot]

Since I cannot test this you'll have to provide feedback...

$\endgroup$
6
  • $\begingroup$ Can solution 1 be used without specifying the size of the Pane? $\endgroup$
    – Verbeia
    Jul 6, 2012 at 23:20
  • $\begingroup$ For starters I do not know if it is a solution -- it needs to be tested on Windows. But why not set the pane size the same as the image size -- you don't want titles hanging over the edge of the plot ...right? How about #2? $\endgroup$ Jul 6, 2012 at 23:23
  • $\begingroup$ @Verbeia forgot to tag my comment. $\endgroup$ Jul 7, 2012 at 1:36
  • $\begingroup$ Finally got around to testing this. I think it's the vagaries of the EMF coordinate rounding that cause it to happen. Your solution #1 works on the cropping (but not the linespacing), but you don't need to specify the Pane width. Labeled isn't really an option because I want to allow users to use the PlotLabel option for unusual graph types that aren't covered by my custom plotting functions. $\endgroup$
    – Verbeia
    Jul 27, 2012 at 2:54
  • $\begingroup$ @Verbeia it is not clear from your comment why Labeled cannot be used for "unusual" graph types. It is more flexible than PlotLabel. $\endgroup$ Jul 31, 2012 at 0:57
2
$\begingroup$

To get both the full control over the line spacing and over the margins around the label, it seems you have to wrap the whole title in a couple of layers. It's not pretty, but gives you total control:

title = Graphics[Inset[
   (* the original title: *)
   Column[{Style["This is a test", 24, Bold, FontFamily -> "Arial", 
      Black], Style["And so is this", 20, FontFamily -> "Arial", 
      Black]}, Center, 0.3]
   (*  placement in wrapper graphic: *)
   , {0, -.2}], 
  PlotRange -> {{-1, 1}, {-1, 1}}, AspectRatio -> .15]

title

The orange border highlights the dimensions of the actual title. The white space margins can be controlled by the AspectRatio and the second argument of Inset.

Now to use this as a PlotLabel, I insert the Graphics from above into a Text because that will put it on the baseline of the plot label:

ListLinePlot[testdata, 
 BaseStyle -> {FontFamily -> "Arial", FontSize -> 16}, 
 ImageSize -> 400, PlotLabel -> Text[title], Frame -> True, 
 ImagePadding -> {{Automatic, Automatic}, {Automatic, 1}}]

plot label

Again I use the orange edges to show that the border of the title is correctly reproduced. Moreover, the narrower line spacing has been preserved, too.

$\endgroup$
1
  • $\begingroup$ This got me on the way to a solution, but using Graphics and AspectRatio requires too much knowledge about the dimensions of the title for my purposes. $\endgroup$
    – Verbeia
    Jul 27, 2012 at 2:31
2
$\begingroup$

Maybe I've missed something, but why not just this?

testdata = 
  FoldList[0.99 #1 + #2 &, 1., 
   RandomVariate[NormalDistribution[0, 0.5], {100}]];

title = Column[{Style["This is a test", 24, Bold, 
    FontFamily -> "Arial", Black], 
   Style["And so is this", 20, FontFamily -> "Arial", Black]}, Center,
   0.3, BaselinePosition -> Top];

Column[{
  title,
  ListLinePlot[testdata, 
   BaseStyle -> {FontFamily -> "Arial", FontSize -> 16}, 
   ImageSize -> 400, Frame -> True, 
   ImagePadding -> {{Automatic, Automatic}, {Automatic, 1}}]}, 
 Alignment -> Center]

<code>highlighted output</code>

Note: I just highlighted the output before taking the screen shot to show what one can select.

$\endgroup$
5
  • $\begingroup$ Ironically this is how I do it for multi panel graphs. See mathematica.stackexchange.com/a/7695/8 $\endgroup$
    – Verbeia
    Jul 6, 2012 at 22:14
  • $\begingroup$ Yes, but isn't the question about PlotLabel? $\endgroup$
    – Jens
    Jul 6, 2012 at 22:20
  • $\begingroup$ @Verbeia -- Sometimes simple works. I really like your multi-panel graphic solution. $\endgroup$
    – Jagra
    Jul 6, 2012 at 22:22
  • $\begingroup$ Yes, I want to use PlotLabel for single panel plots like this one, but in the case of a multi panel graph as in that other question, I have to do it differently. $\endgroup$
    – Verbeia
    Jul 6, 2012 at 22:23
  • $\begingroup$ @Jens - I focused on the 1st sentence of the question, particularly "...problems with exported plots cutting off a pixel or two of the text in the title..." Hence, I took the question as more of a concern with what he exports than an exploration of the use of PlotLabel. My answer provides a workaround. Maybe Verbeia could edit the title and body of the post to make it less ambiguous. $\endgroup$
    – Jagra
    Jul 6, 2012 at 23:08
1
$\begingroup$

Isn't this what ImageMargins is for?

ListLinePlot[testdata, 
 BaseStyle -> {FontFamily -> "Arial", FontSize -> 16}, 
 ImageSize -> 400, PlotLabel -> titleb, Frame -> True,
 ImageMargins -> {{0, 0}, {0, 5}}]

Here it is with a wider margin and a light blue background to better show the space. Column spacing also set to 0.2.

ListLinePlot[testdata, 
 BaseStyle -> {FontFamily -> "Arial", FontSize -> 16}, 
 ImageSize -> 400, PlotLabel -> titleb, Frame -> True, 
 ImageMargins -> {{0, 0}, {0, 10}},
 ImagePadding -> {{Automatic, Automatic}, {Automatic, 1}}, 
 Background -> LightBlue]

Mathematica graphics

$\endgroup$
7
  • $\begingroup$ The problem here is still that the appearance of titleb is different inside and outside the plot label. Of course you can ignore it and only care about the outcome. But in that case I guess this question is a duplicate of "Cropped pdf file after export" $\endgroup$
    – Jens
    Jul 6, 2012 at 21:50
  • $\begingroup$ @Jens this one did seem familiar somehow. I guess I'm not understanding the issue with the text. $\endgroup$
    – Mr.Wizard
    Jul 6, 2012 at 21:53
  • $\begingroup$ I tried ImageMargins but the text is still cropped, even though there is whitespace. $\endgroup$
    – Verbeia
    Jul 6, 2012 at 22:02
  • $\begingroup$ @Verbeia Surprising. On your original plot, what happens if you select it, go to the top handle of the orange frame, hold Control, and drag upward to add space; if you export that Graphic does it still clip the text? $\endgroup$
    – Mr.Wizard
    Jul 6, 2012 at 22:10
  • $\begingroup$ @mr.wizard i can't test EMF export until I'm back at work on Monday and near a Windows machine. Dragging the boundaries doesn't really scale to my application, where I want other users to be able to plot things with minimal fiddling. $\endgroup$
    – Verbeia
    Jul 6, 2012 at 22:21
0
$\begingroup$

It seems from my testing that setting ImageMargins on the plot doesn't help (in fact in my custom plotting function in my real application, I already had that option set to put some whitespace at the top). The padding needs to be between the text and the inner orange border, but ImageMargins puts it between the inner and outer orange borders. I think this cropping is an artefact of the rounding that goes on in EMF/WMF graphic coordinates, so it's not a duplicate of the question about PDFs.

Setting ImageMargins on a wrapper to the title itself does work. @Jens' answer was close, but the number of additional layers turned out to be a bit simpler, involving just Pane and its ImageMargins and Alignment options. @Mike Honeychurch's answer was almost there but it is not necessary to specify the width of the pane.

titleb = Pane[Column[{Style["This is a test", 24, Bold, FontFamily -> "Arial", 
    Black], Style["And so is this", 20, FontFamily -> "Arial", Black]}, 
    Spacings -> 0.3, BaselinePosition -> Baseline], Alignment-> Center, ImageMargins -> 2]

This requires sacrificing consistency in line spacing but unlike Jens' solution, doesn't require any knowledge about the width of the text (to get the AspectRatio right).

I welcome any solution that allows both consistent line spacing and doesn't require knowledge of the required AspectRatio, but for now this is sufficient.

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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