45

Bug introduced in 8.0 or earlier and fixed in 13.2 or later


When doing a RegionPlot and 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]:
output of RegionPlot after saving as EPS

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.

bmf
  • 15,157
  • 2
  • 26
  • 63
celtschk
  • 19,133
  • 1
  • 51
  • 106

3 Answers3

32

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}

Edit 3

Thanks to @becko for pointing out that there is a new command ColorQ in version 10 that can do the same as above. So you can replace colorQ in the previous edit with ColorQ.

Jens
  • 97,245
  • 7
  • 213
  • 499
  • 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 06 '12 at 16:38
  • Thanks for adding the rule definition - I'll just add a little explanation then. – Jens Mar 06 '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 06 '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 06 '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 06 '12 at 23:16
  • All I can say is: the mesh density depends on PlotPoints and MaxRecursion, which makes sense in the process of obtaining the plot, but not in the presentation of the final result. In RegionPlot (or ContourPlot with a small number of contours), it just seems extremely wasteful to leave the computational mesh in the output graphic. The cleanest solution would perhaps be to consolidate all the interior polygons of a single color into one big polygon by removing interior edges. That would be an interesting project... – Jens Mar 07 '12 at 00:30
  • Looking at this post just now [1], I wondered if holes in filled regions would be a problem (making Polygon implementation impossible). My feeling is: not really, because you can in principle just stack the consolidated polygons on top of each other if you keep track of which contour level they correspond to. Then holes are never actually needed. [1] http://mathematica.stackexchange.com/questions/644/how-can-all-those-tiny-polygons-generated-by-regionplot-be-joined-into-a-single – Jens Mar 07 '12 at 01:11
  • Should this be updated to use ColorQ? I don't understand the code well enough to do it myself. – a06e Sep 27 '16 at 20:21
  • @becko yes indeed, it works! I'll update it - thanks for digging this up. – Jens Sep 27 '16 at 21:16
12

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.


The cause is as I thought but my suggestion Antialiasing -> False was naive as that would only affect the antialiasing in the Mathematica Front End, and the problem is the result of antialiasing in the PDF or EPS viewer.

Jens already provided an excellent solution. However in an effort to make this answer useful a generic solution is to export a stack of copies of the graphic which should serve to fill in the partially transparent gaps in the rendering. Using Sumatra PDF reader only a second copy is needed.

plot = RegionPlot[x^2 + y^2 < 1, {x, -1, 1}, {y, -1, 1}, PlotStyle -> Black];

Export["plot.pdf", Show[plot, plot]]

SystemOpen["plot.pdf"]

Of course of the plot contains meaningful transparency this will damage it, but in most cases it is a quick shorthand that is easy to remember and works pretty well.


Somehow I had missed a closely related question which 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?

Mr.Wizard
  • 271,378
  • 34
  • 587
  • 1,371
  • 1
    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 06 '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 06 '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 09 '12 at 04:03
  • 1
    The Show[plot,plot, ...] approach works fine, except that if plot contains other stuff, like legends, the legends are repeated in the exported pdf. – a06e Sep 27 '16 at 20:29
4

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", %]

enter image description here

Maybe someone can confirm this for other systems.

halirutan
  • 112,764
  • 7
  • 263
  • 474