# Avoiding white lines inside filled area in RegionPlot exported as PDF or PS

When doing a RegionPlotand then exporting (or saving from the front end) as PDF or EPS, there are small white lines in the filling of the region. Here's an example created with RegionPlot[x^2+y^2<1,{x,-1,1},{y,-1,1},PlotStyle->Black]:

Is there any way to prevent those white lines while keeping it vector graphics? Note that those lines do not appear on screen, only in the exported file.

Also note that my actual graphics is more complex and especially has several different-coloured regions.

-

The suggestion to use Antialiasing->False doesn't really solve the problem. I don't have a single solution that's appropriate in all cases, but I think one of the approaches I list on the following web page will work: Avoiding artifacts in shaded contour and density plots

Edit:

The following method from the linked article solves the problem:

Instead of exporting the image (assumed to be stored in im1), export the modified graphics

im1 /. {EdgeForm[], r_?(MemberQ[{RGBColor, Hue, CMYKColor, GrayLevel}, Head[#]] &), i___} :> {EdgeForm[r], r, i}


This replaces the invisible edges of the polygons (EdgeForm[]) in your graphic (called im1 here) by edges of default thickness and with a color matching at least one of the neighboring polygons. The new edges then help fill any empty space between the shaded areas.

Edit 2

My solution relies on finding colored polygons without colored edges by looking for EdgeForm[] followed by a color in the graphic im1.

Based on the answer by Mr. Wizard and kguler here, one can also make the above work better with custom colors and future additions to the built-in color choices:

colorQ = FreeQ[Quiet@Darker@#, Darker] &;
im1 /. {EdgeForm[], r_?colorQ, i___} :> {EdgeForm[r], r, i}

-
Thanks. The replacement rule given in that file works great, and now I even get PDFs which render correctly in acroread. Therefore I've switched the accepted answer to yours. To avoid depending on external links, I'll add that rule to your answer. –  celtschk Mar 6 '12 at 16:38
Thanks for adding the rule definition - I'll just add a little explanation then. –  Jens Mar 6 '12 at 16:55
I had the same problem and the replacement rule solved it except when I assembled and converted the files to PDF 1.4 (with Adobe Illustrator) for inclusion in a TeX document the problem reappeared. –  s0rce Mar 6 '12 at 18:28
With Adobe Illustrator CS3, I have no problem exporting to PDF 1.4. Not knowing your specific issue, I can only suggest to replace EdgeForm[r] by EdgeForm[{r,Thickness[Small]}] above. –  Jens Mar 6 '12 at 18:59
Jens, since you've obviously put some thought into this I'd like to ask: is there is a reason the fills are triangulated like that to begin with, rather than complete vector shapes? (+1, btw) –  Mr.Wizard Mar 6 '12 at 23:16

Surely this is a manifestation of the problem described in:

Antialiasing option behaves weird (polygon edges visible in ContourPlot)

Therefore, you should try using Style[plot, Antialiasing -> False] or other methods to disable anti-aliasing.

Somehow I had missed a closely related question witch Jens directed me to. I believe it is a solution to this problem:

How can all those tiny polygons generated by RegionPlot be joined into a single FilledCurve?

-
Thanks, it indeed was an antialiasing problem. Except that at least for the EPS, it was not Mathematica, but gv doing the antialiasing (so the eps file is actually fine; only the display was wrong; switching off antialiasing in gv fixed the problem). –  celtschk Mar 6 '12 at 14:37
The fact that he still needs to mess with the display in gv means that the export without antialiasing cannot have solved the problem. –  Jens Mar 6 '12 at 16:10
I agree that the FilledCurve approach also solves this problem for RegionPlot. But it's worth keeping in mind that the underlying issue also affects ContourPlot, DensityPlot and related functions. I don't think the FilledLine trick can easily be generalized to these types of plots because their colors aren't "binary" and they can't even be guaranteed to have lines that enclose the shaded areas. –  Jens Mar 9 '12 at 4:03

One should note, that in Mathematica Version 9 this issue no longer appears. I tried it here on Linux x86 (64-bit):

RegionPlot[x^2 + y^2 < 1, {x, -1, 1}, {y, -1, 1},
PlotStyle -> Black];
Export["tmp/region.pdf", %]


Maybe someone can confirm this for other systems.

-
I really need to get MMA 9. –  s0rce Dec 1 '12 at 0:25
MMA 9 on mac also works without problems. –  xslittlegrass May 12 '13 at 21:34
The problem shows up when you have multiple functions, or you use opacity! –  Lina Apr 21 at 18:20
@Lina The question was not about Opacity, but I agree that there are still a lot of issues about vector graphics in Mathematica. You may want to read here and here. –  halirutan Apr 21 at 19:16