46
$\begingroup$

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.

$\endgroup$
2
  • $\begingroup$ This is still an issue in Mathematica 11. Has this bug been reported? $\endgroup$
    – a06e
    Commented Sep 27, 2016 at 20:19
  • $\begingroup$ I happens also with ListContourPlot, not just RegionPlot $\endgroup$
    – a06e
    Commented Sep 27, 2016 at 20:31

3 Answers 3

32
$\begingroup$

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.

$\endgroup$
9
  • $\begingroup$ 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. $\endgroup$
    – celtschk
    Commented Mar 6, 2012 at 16:38
  • $\begingroup$ Thanks for adding the rule definition - I'll just add a little explanation then. $\endgroup$
    – Jens
    Commented Mar 6, 2012 at 16:55
  • $\begingroup$ 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. $\endgroup$
    – s0rce
    Commented Mar 6, 2012 at 18:28
  • $\begingroup$ 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. $\endgroup$
    – Jens
    Commented Mar 6, 2012 at 18:59
  • $\begingroup$ 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) $\endgroup$
    – Mr.Wizard
    Commented Mar 6, 2012 at 23:16
12
$\begingroup$

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?

$\endgroup$
4
  • 1
    $\begingroup$ 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). $\endgroup$
    – celtschk
    Commented Mar 6, 2012 at 14:37
  • $\begingroup$ The fact that he still needs to mess with the display in gv means that the export without antialiasing cannot have solved the problem. $\endgroup$
    – Jens
    Commented Mar 6, 2012 at 16:10
  • $\begingroup$ 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. $\endgroup$
    – Jens
    Commented Mar 9, 2012 at 4:03
  • 1
    $\begingroup$ The Show[plot,plot, ...] approach works fine, except that if plot contains other stuff, like legends, the legends are repeated in the exported pdf. $\endgroup$
    – a06e
    Commented Sep 27, 2016 at 20:29
4
$\begingroup$

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.

$\endgroup$
5
  • 3
    $\begingroup$ I really need to get MMA 9. $\endgroup$
    – s0rce
    Commented Dec 1, 2012 at 0:25
  • $\begingroup$ MMA 9 on mac also works without problems. $\endgroup$ Commented May 12, 2013 at 21:34
  • 1
    $\begingroup$ The problem shows up when you have multiple functions, or you use opacity! $\endgroup$
    – Lina
    Commented Apr 21, 2014 at 18:20
  • $\begingroup$ @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. $\endgroup$
    – halirutan
    Commented Apr 21, 2014 at 19:16
  • 2
    $\begingroup$ I still observe this issue on MMA 10 over linux, depending on the pdf reader. (In particular, evince is generally OK but the TexMaker inbuilt one is not.) $\endgroup$ Commented May 22, 2015 at 13:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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