0
$\begingroup$

I am using the following Mathematica snippet to generate a contour plot:

Needs["MaTeX`"]
press[x_, fo_] = (fo + (18.369*Exp[-2.491*x]))/(29.512*Exp[-9.691*x])
fs[x_] = (7.14286/x + 51.0204 Log[7. - 50. x] - 51.0204 Log[x])/10
plot = ContourPlot[press[x, fo], {x, 0.05, 0.35}, {fo, 0, 80}, 
  PlotRange -> {2, 8}, 
  RegionFunction -> 
   Function[{x, fo, press}, If[x < 0.14, fs[x] < fo, 0 < fo]], 
  BoundaryStyle -> Black, 
  Contours -> {2, 2.5, 3, 3.5, 4, 4.5, 5, 5.5, 6, 6.5, 7, 7.5, 8}, 
  FrameLabel -> 
   MaTeX[{"Clearance \, (mm)\, stick-slip", "Force \, (N)"}], 
  PlotLegends -> 
   Placed[
     BarLegend[{Automatic, {2, 8}}, LegendLayout -> "Row", 
      LegendMarkerSize -> {50, 15},
      LegendFunction -> 
       (Framed[#, RoundingRadius -> 5, 
         Background -> Opacity[0.7, White]] &), 
      LegendMargins -> 5, 
      LegendLabel -> Placed[MaTeX["Pressure \, (Bar)"], Below], 
      BaseStyle -> {FontFamily -> "Times", FontSize -> 8}], {0.75, 
     0.75}], 
  LabelStyle -> Directive[Black, Bold, Medium], 
  AspectRatio -> 1/2, 
  BaseStyle -> {FontFamily -> "Times", FontSize -> 10}]

The plot renders correctly in the Mathematica notebook, although I need to manually stretch it to ensure everything displays properly. When exporting as a PNG using the following command:

Export[FileNameJoin[{NotebookDirectory[], "output.png"}], plot, "PNG", 
  ImageResolution -> 600, ImageSize -> 3000]

The output seems fine.

enter image description here

However, when I export as a PDF with Export[FileNameJoin[{NotebookDirectory[], "output.pdf"}], plot, "PDF", ImageResolution -> 600, "AllowRasterization" -> True] to achieve a vectorized output, I encounter an issue where the plot shows an unwanted triangular mesh or grid pattern:

enter image description here

and the legend is out of the plot partially!

I tried increasing PlotPoints and MaxRecursion, which led to much longer processing times and a significantly larger PDF file size without resolving the issue.

My goal is to generate a smooth and clean vectorized output in PDF format without the triangular mesh artifacts, preferably with reasonable processing time and file size.

Is there a way to achieve this, or any alternative approach I should consider?

$\endgroup$
2
  • $\begingroup$ Also, there are several other questions about this issue on the site. Please search for "pdf mesh". $\endgroup$
    – Domen
    Commented Aug 26 at 8:59
  • $\begingroup$ @Domen I had seen that post and others similar but they did not solve my issue. For example, I just added SetOptions[$FrontEndSession, Antialiasing -> Automatic] at the top of my code but it did not solve the problem. And the legend is still out of bounds! $\endgroup$
    – Foad
    Commented Aug 26 at 9:17

1 Answer 1

1
$\begingroup$

I was able to resolve the issue with the triangular mesh artifacts in my PDF export by rasterizing the plot before exporting. Here’s the updated code that worked for me:

press[x_, fo_] = (fo + (18.369*Exp[-2.491*x]))/(29.512*Exp[-9.691*x])
fs[x_] = (7.14286/x + 51.0204 Log[7. - 50. x] - 51.0204 Log[x])/10

plot = ContourPlot[
  press[x, fo], {x, 0.05, 0.35}, {fo, 0, 80}, 
  PlotRange -> {2, 8}, 
  RegionFunction -> 
   Function[{x, fo, press}, If[x < 0.14, fs[x] < fo, 0 < fo]], 
  BoundaryStyle -> Black, 
  Contours -> {2, 2.5, 3, 3.5, 4, 4.5, 5, 5.5, 6, 6.5, 7, 7.5, 8}, 
  FrameLabel -> 
   MaTeX[{"Clearance \, (mm)\, stick-slip", "Force \, (N)"}], 
  PlotLegends -> 
   Placed[BarLegend[{Automatic, {2, 8}}, LegendLayout -> "Row", 
     LegendMarkerSize -> {30, 10}, 
     LegendFunction -> (Framed[#, RoundingRadius -> 5, 
         Background -> Opacity[0.7, White]] &), 
     LegendMargins -> {{1, 1}, {1, 1}}, 
     LegendLabel -> Placed[MaTeX["Pressure \, (Bar)"], Below], 
     BaseStyle -> {FontFamily -> "Times", FontSize -> 4}, 
     ImageSize -> {200, 40}], {0.5, 0.75}], 
  LabelStyle -> Directive[Black, Medium], 
  AspectRatio -> 1/2, 
  BaseStyle -> {FontFamily -> "Times", FontSize -> 4}
]

(* Rasterize the plot to eliminate vectorization artifacts *)
plot = Rasterize[plot, "Image", ImageResolution -> 300];

(* Export the rasterized plot as a PDF *)
Export[FileNameJoin[{NotebookDirectory[], "output.pdf"}], plot]

Explanation:

To ensure smooth and clean output, I rasterized the plot using Rasterize before exporting it as a PDF. This eliminated the artifacts and produced a much cleaner result.

This resolved the issue and provided a clean vectorized output in PDF format with reasonable processing time and file size.

$\endgroup$

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