When exporting a RegionPlot to PDF, spikes appear on the boundary:

plot = RegionPlot[-x < y < x, {x, 0, 2}, {y, -2, 2}];
Export["plot.PDF", plot]

I am not concerned with the lines in the interior of the region, which has its own question (2629). However I do note that every spike (except the two spikes at the origin) is located at the intersection of multiple interior lines with the boundary.

Exporting Style[plot, Antialiasing -> False] removes some but not all of the spikes, although on closer inspection it appears that some of the spikes haven't been removed, but rather a change in orientation has caused them to be hidden or more covered under the boundary. Jens' replacement has no effect on the spikes (nor the interior lines, but again the issue isn't the lines).

Export["plotAF.PDF", Style[plot, Antialiasing -> False]]
(* fewer spikes / rotated so they are less apparent *)
Export["plotJens.PDF", plot /. {EdgeForm[], r_?ColorQ, i___} :> {EdgeForm[r], r, i}]
(* no change *)

(Using Version 10.2.0 for Microsoft Windows (32-bit))

Spikes (or pricks) along the boundary

  • 1
    $\begingroup$ I don't know how to fix it, but a workaround is possible. Just set BoundaryStyle->None and add the lines manually again (Epilog or Show[Graphics[Line[....). $\endgroup$ – Julien Kluge Jul 19 '17 at 20:37

I'm not exactly sure why this is happening, but it looks like the aberrations occur where multiple triangles meet.

So one approach could be to merge all triangles into one polygon:

Original data:

plot = RegionPlot[-x < y < x, {x, 0, 2}, {y, -2, 2}];
Export["plot.PDF", plot];

Modified plot:

plot2 = BoundaryDiscretizeGraphics[plot, 
  MeshCellStyle -> {
    1 -> {AbsoluteThickness[1.6], ColorData[97][1]}, 
    2 -> {Opacity[.3], ColorData[97][1]}}, 
Export["plot2.PDF", plot2];

Visual comparison:

enter image description here

Also note that the file size is smaller too:

FileByteCount /@ {"plot.PDF", "plot2.PDF"}
{25436, 14577}


My guess as to why the original plot has these aberrations is that there are very small triangles on the boundary. So small that the thicknesses of their boundaries extends past the triangles themselves.

mr = DiscretizeGraphics[plot];

areas = PropertyValue[{mr, 2}, MeshCellMeasure];

{1.6511*10^-10, 0.0110803}
Histogram[areas, {.000025}, PlotRange -> {{0, .002}, Automatic}]

enter image description here

If we export the centroids of these tiny faces, we can see they line up with the aberrations and so I think they are the culprit:

pts = PropertyValue[
  {mr, {2, Flatten[Position[areas, _?(LessThan[10^-8])]]}}, 
Export["plot.PDF", Show[plot, Epilog -> {Red, Opacity[.2], Point[pts]}]]

enter image description here

BoundaryDiscretizeGraphics gets rid of these small triangles, but does leave small edges on the boundary. This seems alright for exporting though.


I don't know why this is happening, but the samplist way to avoid it is to change the axes ranges and specify the plot range.

RegionPlot[-x <= y <= x, {x, -0.1, 2}, {y, -2, 2},BoundaryStyle -> Directive[Thickness[0.01], Blue],PlotRange -> {{0, 2.1}, {-2.1, 2.1}}]  

enter image description here


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy