2
$\begingroup$

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):

enter image description here

and if we zoom-in to the top edge:

enter image description here

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

$\endgroup$
  • $\begingroup$ Look around PlotRangePadding and PlotRangeClipping $\endgroup$ – Rom38 Jul 27 '17 at 3:56
  • $\begingroup$ @Rom38, Thanks, I forgot to mention, I tried PlotRangePadding -> None as well as PlotRangeClipping -> False and PlotRangeClipping -> True, with no improvement. $\endgroup$ – Felix Jul 27 '17 at 4:09
  • $\begingroup$ PlotRangePadding->Scaled[0.01] can solve the problem with intersection at the top-edge but the left side is still problematic.. $\endgroup$ – Rom38 Jul 27 '17 at 4:50
  • $\begingroup$ 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... $\endgroup$ – Jens Jul 27 '17 at 4:52
  • 1
    $\begingroup$ I did report it, let's see what they say. $\endgroup$ – Felix Jul 27 '17 at 16:50
1
$\begingroup$

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:

orange

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

$\endgroup$
  • $\begingroup$ 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. $\endgroup$ – Felix Jul 27 '17 at 15:57
  • $\begingroup$ I know, your bug report is the only real way forward. $\endgroup$ – Jens Jul 27 '17 at 17:13

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.