Bug introduced in 12 or earlier and persisting through 13.1.0 [CASE:4899765]

How can I remove the outline in my export PDF here which seems to be caused by the overlapping of the shape and the grid lines?

enter image description here

It doesn't seem to show on the Mathematica image (or just because I couldn't zoom in enough) but when I exported PDF and zoom in I can see it.

polygon = {{1, 1}, {3, 1}, {4, 2}, {2, 4}, {1, 3}}
image = ListLinePlot[polygon, 
    GridLines -> {Range[1, 5, 1/2], Range[0, 5, 1/2]}, 
    PlotRange -> {{1, 5}, {0, 5}}, Filling -> {1 -> Axis}, 
    FillingStyle -> Directive[Opacity[0.2], Orange], 
    GridLines -> {Range[1, 5, 1], Range[1, 5, 1]}, ImageSize -> 250, 
    AspectRatio -> 1, 
    Frame -> {{None, Automatic}, {Automatic, None}}] /. _Line -> 
Export["image.pdf", image]

This is the output in my 13.0.1 version which is even worse than the 12.1 version. I'm going to check for the update.
I checked for the update but unfortunately we here we don't have the latest version for support. Is there any way to work around this?

enter image description here

  • $\begingroup$ @cvgmt I think I have the same problem in my 13.1 version. The above is from 12.1.1 in my PC but with I had the same problem yesterday in a 13.1 version on a different computer. $\endgroup$
    – hana
    Jun 30, 2022 at 4:49
  • $\begingroup$ Anyway I'll check it again when I get to my 13.1 version. Is there any fix for the 12.1? $\endgroup$
    – hana
    Jun 30, 2022 at 4:55
  • $\begingroup$ You can make the grid lines non-transparent, and apply the option "GridLinesInFront" (search for it on the site). $\endgroup$ Jun 30, 2022 at 7:45
  • $\begingroup$ @AlexeyPopkov what do you mean non-transparent grid lines? What seems to be opposite with what I'm trying to do now. I want to show the grid lines but I don't want to show the outline around the orange region as in the image above. $\endgroup$
    – hana
    Jun 30, 2022 at 7:56
  • $\begingroup$ You question formulation is a bit misleading. If the problem is not with grid lines, but with the region, you could try to adjust the style of the region boundary using the PlotStyle option. $\endgroup$ Jun 30, 2022 at 8:13

1 Answer 1



Here is a universal version of polygon2FilledCurve which should work correctly independently of the presence of GraphicsComplex inside of Graphics:

polygon2FilledCurve = # /. 
     g_GraphicsComplex :> (g /. {
         Polygon[pts : {{__Integer} ..}] :> FilledCurve /@ Thread[Line[pts]],
         Polygon[pts : {__Integer}] :> FilledCurve[Line[pts]]}) /.
    {Polygon[pts : {_?(MatrixQ[#, NumericQ] &) ..}] :> FilledCurve /@ Thread[Line[pts]],
     Polygon[pts_ /; MatrixQ[pts, NumericQ]] :> FilledCurve[Line[pts]]} &;

(original version below works incorrectly in more complicated cases than the one given in the OP, as was found here.)

Original answer

Your problem is not related to grid lines, it is just the known bug with exporting Polygon primitive to PDF format. The workaround is to represent the Polygon as a FilledCurve object which doesn't suffer from the bug:

polygon2FilledCurve = # /. {
     Polygon[pts_ /; Depth[pts] == 3] :> FilledCurve[Thread[Line[pts]]],         
     Polygon[pts_ /; Depth[pts] == 2] :> FilledCurve[Line[pts]]} &;

Export["image.pdf", polygon2FilledCurve[image]] // SystemOpen

Here is how exported figure is rendered by Adobe Acrobat:



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