# How do I prevent plot range clipping errors in a pdf printout?

Bug introduced in 11.3 or earlier

I am using Mathematica 11.3 on a Windows 10 computer. I plotted several data sets of mine in a ListLinePlot. While the .pdf file looks perfectly in the current Adobe Acrobat Reader DC version 2019.010.20098, the printout on three different printers and two different computers always shows weird additional lines crossing the whole image (see below).

This error seems to be related to: Mathematica Stack Exchange, question 129805. However, as I consider it to be very general (see below) and the question was not precise enough I opened up a new question. I think these errors result from a clipping error/bug in mathematica of the PlotRange command for large data sets outside the limits given.

Here is a minimum example given:

Let's calculate a simple normal distribution...

TestSet=Table[{x, PDF[NormalDistribution[0, 0.2], x]},{x, -100, 100, 0.1}];


Now we plot our data set only in a very small region...

SimpleExample=ListLinePlot[TestSet,PlotRange->{{-0.5, 1}, {0, 1}},ImageSize->400]


Subsequently, I exported the plot to a pdf file:

Export["Test.pdf", SimpleExample, ImageResolution -> 600];


While the .pdf file looks perfect, the scanned-in printout shows weird additional lines at the outer PlotRange limits. Apparently, the truncated edges of the data set plotted are arbitrarily connected with an invisible outer frame...

By trial-and-error I figured out that the problem must be related to the amount of data, which is outside the limits of the plot given by PlotRange. In a simple workaround I just chopped the data set down to only one next neighbor outside the PlotRange limits using the home-built function ChopLists:

    ChopLists[datalists_, xmin_, xmax_, ymin_, ymax_] :=(*ChopLists assumes that datalists is a list of sorted 2D-lists.*)
Module[{xmin2Next, xmax2Next, ymin2Next, ymax2Next},
(*If data outside the limits given by xmin, xmax,... exist, at least one data point (next neighbour) outside has to be still included by Chop List. Otherwise solid lines in ListLinePlot would stop before the frame boundary or might be falsely connected! E.g. truncated edges of a peak would be connected *)

xmin2Next = Min[If[Length[Select[##, #[[1]] < xmin &]] > 0, Select[##, #[[1]] < xmin &][[-2, 1]], xmin] & /@datalists]; (*calculate 2nd next neighbour in each direction, if it exists*)
xmax2Next = Max[If[Length[Select[##, #[[1]] > xmax &]] > 0, Select[##, #[[1]] > xmax &][[2, 1]], xmax] & /@datalists];
ymin2Next = Min[If[Length[Select[##, #[[2]] < ymin &]] > 0, Select[##, #[[2]] < ymin &][[-2, 2]], ymin] & /@datalists];
ymax2Next = Max[If[Length[Select[##, #[[2]] > ymax &]] > 0, Select[##, #[[2]] > ymax &][[2, 2]], ymax] & /@datalists];

Select[#, xmin2Next < #[[1]] < xmax2Next && ymin2Next < #[[2]] < ymax2Next &] & /@datalists]
(*As the 2nd next neighbour is the limit, the next neighbour in each direction is included, if it exists*)


Now, if I apply ChopLists[...] to TestSet and use the same limits as for PlotRange...

SimpleExampleChopped=ListLinePlot[ChopLists[{TestSet}, -0.5, 1, 0, 1],PlotRange -> {{-0.5, 1}, {0, 1}}, ImageSize -> 400]


...and export to pdf again...

Export["Test.pdf", SimpleExampleChopped, ImageResolution -> 600];


...finally, the weird lines are gone:

The same procedure also solved my initial problem: By chopping the data set using ChopLists[] before plotting, the weird lines are gone:

My question is now: Is this a bug of PlotRange or does it result from the pdf export? Is this work-around needed or does any standard option exist to solve similar issues? Are there any options or combinations of them e.g. PlotRangeClipping, PlotRangePadding or ClippingStyle to prevent these clipping errors (simple combination of these three didn't work for me)?

UPDATE (26.03.2019) - Further testing on Windows and Mac

As suggested by @MassDefect I tested the same simple example also on a MAC (MAC OS 10.10.1 with Mathematica 9.0.1 and Adobe Reader 2018.011.2006). However, like him I was not able to reproduce the error. The printout from the .pdf file looked perfectly. I also copied the problematic .pdf file, which I had exported on my Windows 10 system, to the Mac. Surprisingly in the printout from the Mac no weird additional lines are visible and the printout is perfect.

Subsequently I copied both .pdf files, which were generated on the Windows 10 and the Mac to another different Windows 10 computer (Adobe Reader XI, Version 11.0.10) and printed them on another different printer. ...Again, the printed sheets from both .pdf files (Mac and Windows) showed the weird additional lines!

In conclusion: The problem must be related to the Windows .pdf format.

• It is the job of PlotRange to create a visual graphics object with... the specified PlotRange. If that's being done correctly in the visual representation of the notebook and then it looks incorrect on PDF Export, then the bug is in PDF Export. – user6014 Mar 24 '19 at 23:57