# PDF export: Lines extending past plot frame area

I recognized recently that a lot of my PDF exported plots have lines extending past the actual (framed) plot area. Here is a simple example:

p = ListLinePlot[
Table[{x, 2 x^2}, {x, -1, 1, .01}],
Frame -> True,
PlotRange -> {{0, 1}, {0, 1}}
]
Export["test2.pdf", p]


The results looks like this (screenshot):

and if we zoom-in to the top edge:

Can this be avoided? The effect is not visible in PNG exports, but I prefer working with vector graphics.

• Look around PlotRangePadding and PlotRangeClipping – Rom38 Jul 27 '17 at 3:56
• @Rom38, Thanks, I forgot to mention, I tried PlotRangePadding -> None as well as PlotRangeClipping -> False and PlotRangeClipping -> True, with no improvement. – Felix Jul 27 '17 at 4:09
• PlotRangePadding->Scaled[0.01] can solve the problem with intersection at the top-edge but the left side is still problematic.. – Rom38 Jul 27 '17 at 4:50
• I think this is related to the bug here, and the fix could also be similar. I'll try to write an answer with a workaround, but the real solution is to get this bug fixed... – Jens Jul 27 '17 at 4:52
• I did report it, let's see what they say. – Felix Jul 27 '17 at 16:50

There is a related question where it's observed that the magnitude of the overhang depends on the ImageSize. In that thread, we decided it's a bug.

A work-around for this bug is to surround the plot by a "picture frame" or "passepartout" that crops the overhanging piece of the curve. Here I try to do that:

p = With[{t = .0004, d = .5, background = White},
ListLinePlot[Table[{x, 2 x^2}, {x, -1, 1, .01}], Frame -> True,
FrameStyle -> Thickness[2 t], Axes -> None,
PlotRange -> {{0, 1}, {0, 1}}, PlotRangePadding -> 0,
Epilog -> {background,
FilledCurve[{{Line[
ImageScaled /@ {{-d, -d}, {-d, 1 + d}, {1 + d, 1 + d}, {1 + d, -d}}]},
{Line[
Scaled /@ {{-t, -t}, {-t, 1 + t}, {1 + t, 1 + t}, {1 + t, -t}}]}}]}]]


The plot is the same as in the question, but the overhang is now hidden in the exported PDF. The variable d contains the thickness of the buffer zone around the frame that is overwritten with a frame of color background. The issue then arises that this also crops half of the plot frame (if Frame->True is set). To fix this, I need to specify the frame thickness explicitly. The variable t is half the desired frame thickness (in scaled coordinates, not absolute points). With this, I offset the cropping rectangle by t so that it shouldn't overlap with the plot frame. In the notebook, it now looks as if there is in fact some undesirable cropping of the frame. But in the exported PDF, I don't see any problems: in particular, the frame and the ticks appear to be of the same thickness, as they should be.

To illustrate the cropping I did above, here is a modified plot that intentionally shows the location of the cropping rectangle in orange, to see where the overhang regions are and how they are blotted out:

You get this plot by changing background to Orange and exporting to PDF.

• Thanks, it's nice to have a workaround for some simple plots. In my present case, I have a multi panel scidraw figure with insets and all of that, so manually framing all of them is not really an option. – Felix Jul 27 '17 at 15:57
• I know, your bug report is the only real way forward. – Jens Jul 27 '17 at 17:13