3

I have a couture plot which I want to export in vector format (e.g.PDF) and use in my latex. The example code from here:

plot = ContourPlot[Sin[x + y^2], {x, -3, 3}, {y, -2, 2}, 
  Contours -> 8, BoundaryStyle -> Black, 
  PlotLegends -> 
   Placed[BarLegend[Automatic, LegendMarkerSize -> 150, 
     LegendFunction -> (Framed[#, RoundingRadius -> 5] &), 
     LegendLabel -> "Z"], {After, Top}], FrameLabel -> {"X", "Y"}, 
  LabelStyle -> Directive[Black, Bold, Medium],  AspectRatio -> Full]

It looks fine on Adobe products, but on macOS preview, gostscript and other LaTeX editors built-in PFD viewers it seems like this:

enter image description here

This is actually a very common problem and there are already many similar posts:

  1. ContourPlot has extra mesh in PDF:
  2. Saner alternative to ContourPlot fill
  3. Avoiding white lines inside filled area in RegionPlot exported as PDF or PS
  4. Antialiasing option behaves weird (polygon edges visible in ContourPlot)
  5. ContourPlot has extra mesh

I have tried all of them:

  • From comments of this page I tried adding Method -> {"TransparentPolygonMesh" -> True}
  • From here I tried Style[plot, Antialiasing -> False]
  • From this page I tried /. {EdgeForm[], r_?(MemberQ[{RGBColor, Hue, CMYKColor, GrayLevel}, Head[#]] &), i___} :> {EdgeForm[r], r, i} this lessens the issue but white lines are still there:

enter image description here

  • Using contourRegionPlotfunction defined here solves the issue, however it removes the BarLegend in the format I want!

I would appreciate if you could help me solve the issue.

Foad
  • 605
  • 4
  • 13

1 Answers1

2

Use cleanContourPlot from one of the threads you linked. It does not always work, but it can handle your example very well.

Without legends, a plot has the structure Graphics[...]. With legends, it has the structure Legended[Graphics[...], ...]. Therefore cleanContourPlot must be used only on the first part of Legended.


Head[plot]
(* Legended *)

fixedPlot = MapAt[cleanContourPlot, plot, {1}]

Avoid resizing figures with plots when exporting:

SetOptions[$FrontEndSession, PrintingStyleEnvironment -> "Working"]

This setting will keep its effect until you restart the front end.

Export["plot.pdf", fixedPlot]
Szabolcs
  • 234,956
  • 30
  • 623
  • 1,263
  • 1
    I updated the cleanContourPlot function now so it would work automatically on Legended. – Szabolcs Sep 06 '17 at 11:17
  • Novice here, would you mind ELI5? – Foad Sep 06 '17 at 11:17
  • It works like a charm. thanks a lot. you saved my day :) – Foad Sep 06 '17 at 11:22
  • 1
    @Foad I don't understand "ELI5" – Szabolcs Sep 06 '17 at 11:24
  • Explain Like I'm Five :) Reddit lingo I think :) – Foad Sep 06 '17 at 11:27
  • @Foad There's a lot in there ... Which specific part do you not understand? – Szabolcs Sep 06 '17 at 11:38
  • I could not understand it first, but then I just used it and it worked. you may ignore my comment. – Foad Sep 06 '17 at 12:37
  • I have one more issue, the cleanContourPlot function removes the BoundaryStyle -> Black – Foad Sep 06 '17 at 12:37
  • 1
    @Foad I do not have time to fix it now. I would use some quick and dirty workaround like Epilog -> {FaceForm[], EdgeForm[Black], Rectangle[{-3, -2}, {3, 2}]} where I took the rectangle dimensions from the ContourPlot variable bounds. – Szabolcs Sep 06 '17 at 12:42
  • well actually my real plot is different and there I'm using a RegionFunction – Foad Sep 06 '17 at 12:44
  • 1
    @Foad Can you plot the RegionFunction separately and superimpose? E.g. if you have x^2+y^2 < 5 then ContourPlot[x^2+y^2 == 5, ...] with black style and superimpose with Show. – Szabolcs Sep 06 '17 at 12:46