6
$\begingroup$

I have the following RegionPlot (which code I don't present since it comes from a long expression):

enter image description here

When I right click on it (within the notebook) and try to save it as PDF, for some reason I get the following image as the exported PDF

enter image description here

With the mesh-like effect on it. PNG works fine, but I would prefer to have it as a PDF. Any idea why this is happening and how to fix it? Other region plots I've done didn't yield this effect.

$\endgroup$
3
6
$\begingroup$

As a workaround we can express the Polygon complex through its boundary representation:

plot = RegionPlot[x^2 + y^3 < 2, {x, -2, 2}, {y, -2, 2}];

Export["/Users/ghurst/Desktop/plot.pdf", plot];

gcps = Position[plot, gc_GraphicsComplex, ∞];

breps = BoundaryDiscretizeGraphics /@ Extract[plot, gcps];

faceform = Append[ColorData[97, 1], 0.3];
edgeform = Directive[AbsoluteThickness[1.6], ColorData[97, 1]];

newgcs = 
  Show[BoundaryMeshRegion[#, 
    MeshCellStyle -> {2 -> {FaceForm[faceform], 
      EdgeForm[edgeform]}}]] & /@ breps;

plot2 = ReplacePart[plot, Thread[gcps -> newgcs[[All, 1]]]];

Export["/Users/ghurst/Desktop/plot2.pdf", plot2];

Comparison:

enter image description here

$\endgroup$
3
$\begingroup$

This problem is one of those things that comes up a lot, but each question has slight differences that make agreement about being a duplicate is hard to reach. Here is a solution that can achieved by a slight tweak to one line in @Szabolcs' answer:

cleanRegionPlot@RegionPlot[..]

For instance:

Export[FileNameJoin[{$TemporaryDirectory, "clean.pdf"}], 
  cleanRegionPlot@RegionPlot[x^2 + y^3 < 2, {x, -2, 2}, {y, -2, 2}]];
First@Import[%]

From @Szabolcs' code:

cleanRegionPlot[cp_Graphics] :=
  Module[{points, groups, regions, lines},
   groups = 
    Cases[cp, {style__, g_GraphicsGroup} :> {{style}, g}, Infinity];
   points = 
    First@Cases[cp, GraphicsComplex[pts_, ___] :> pts, Infinity];
   regions = Table[
     Module[{group, style, polys, edges, cover, graph},
      {style, group} = g;
      polys = 
       Join @@ Cases[group, Polygon[pt_, ___] :> pt, Infinity];
      edges = Join @@ (Partition[#, 2, 1, 1] & /@ polys);
      cover = Cases[Tally[Sort /@ edges], {e_, 1} :> e];
      graph = Graph[UndirectedEdge @@@ cover];
      {Sequence @@ style, 
       FilledCurve[
        List /@  Line /@ First /@ 
           Map[First, 
            FindEulerianCycle /@ (Subgraph[graph, #] &) /@ 
              ConnectedComponents[graph], {3}]]}], {g, groups}];
   lines = Cases[cp, {__, _Line}, Infinity]; (* only change *)
   Graphics[GraphicsComplex[points, {regions, lines}], 
    Sequence @@ Options[cp]]
   ];

Possible duplicates:

Many related:

$\endgroup$
1
3
$\begingroup$

specifying the PlotStyle solve it too

RegionPlot[x^2 + y^3 < 2, {x, -2, 2}, {y, -2, 2}, PlotStyle -> Yellow]

enter image description here

$\endgroup$
1
  • 1
    $\begingroup$ (+1) That's funny that there is such a simple workaround that everyone overlooked. What actually gives the desired effect here is setting PlotStyle -> Opacity[1] or PlotStyle -> Opacity[0]. Another way is to make a replacement: /. Opacity[_] -> Opacity[1]. PlotStyle -> Yellow just removes the default Opacity[0.3] styling directive. $\endgroup$ Nov 10 at 1:02
2
$\begingroup$

Since the problem appears due to the default Opacity[0.3] color directive, we can avoid it by converting color directives with transparency into the corresponding color directives without transparency. By inspecting the internal structure of the default plot we can find that the default style is:

Directive[RGBColor[0.368417, 0.506779, 0.709798], AbsoluteThickness[1.6], Opacity[0.3]]

The corresponding color without transparency (assuming white background) is:

opacity = 0.3;
color = RGBColor[0.368417, 0.506779, 0.709798];
equivalentColor = RGBColor[opacity*List @@ color + (1 - opacity)]
RGBColor[{0.8105251, 0.8520337, 0.9129394}]

Hence the default style will be reproduced with:

pl = RegionPlot[x^2 + y^3 < 2, {x, -2, 2}, {y, -2, 2}, 
  PlotStyle -> RGBColor[{0.8105251, 0.8520337, 0.9129394}]]

plot

Here is how it looks exported to PDF:

Export["pl.pdf", pl] // SystemOpen

screenshot

$\endgroup$

Your Answer

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

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