2

I use the following code to generate and export a simple combined figure with an epilog of the long horizontal arrow.

inten = (x y (z^3 + 0.2))/(x^2 + y^2 + z^2 + 0.01);
max = 1; zlst = Range[2, -2, -4/(5 - 1)];
frameticks = {-0.5, 0.5};

plot0 = Table[ ContourPlot[ inten /. z -> zlst[[-i]], {x, -max, max}, {y, -max, max}, FrameTicks -> {{frameticks, None}, {frameticks, None}}, ImagePadding -> {{Automatic, Automatic}, {23, 36}}, ImageSize -> 160], {i, Length@zlst}]; plot = GraphicsRow[plot0, Epilog -> {Gray, Thickness[0.01/3], Arrowheads[0.015], Arrow[{Scaled[{0, 0.96}], Scaled[{0.94, 0.96}]}]}, ImageSize -> 1000]

Export["Fig_try.pdf", plot]

It looks good in the front end, but is weirdly damaged in two aspects in the exported pdf.

  1. The arrowhead at the top right corner is cut out;
  2. There appear many glitches around the boundary of each plot.

From the comments received, it seems not limited to my machine or to PDF format. Any remedy or workaround?

enter image description here

Michael E2
  • 235,386
  • 17
  • 334
  • 747
xiaohuamao
  • 4,728
  • 18
  • 36
  • Please include your data (or fake sample data) so that we can run your code, otherwise it may be difficult to troubleshoot. A shot in the dark: Does using Row instead of GraphicsRow improve the situation at all? – MarcoB Jun 18 '22 at 13:29
  • @MarcoB The code is self-contained. And I need GraphicsRow instead of Row to add various epilogs. – xiaohuamao Jun 18 '22 at 13:35
  • This seems like a bug—it happens with other vector graphics formats like .svg and .eps, and isn't specific to the .pdf export. I'm guessing you want vector graphics—but if not, then exporting to .png (with e.g. Export["Fig_try.png", plot, ImageResolution -> 600]) seems to work. (There are still some misalignments with the contour lines starting too early and ending too late, but if you look closely, you'll notice these are already present in the notebook itself!) – thorimur Jun 18 '22 at 23:39
  • @thorimur Thanks for the info on other formats. Yes, I want vector graphics basically. BTW, I don't see any contour lines overplotted in my front end as far as I've zoomed in. – xiaohuamao Jun 19 '22 at 00:18
  • Oh, that's good! maybe it's a version difference or an OS difference. As for getting vector graphics...hmm. might be worth it to contact wolfram support about it. There also might be ways to refine the shapes or translate them to a format that's more amenable to svg export. By examining FullForm, we see that the regions of the plots are currently GraphicsComplexes of GraphicsGroups of Polygons. I wonder if converting them to some other form, like meshes(?), or messing with their vertex-related options would have any effect on the export. – thorimur Jun 19 '22 at 00:26
  • Duplicate?: https://mathematica.stackexchange.com/questions/226599/how-to-stop-mathematica-12-1-from-chopping-off-the-axes-arrows (just the arrow part, I guess) – Michael E2 Jun 19 '22 at 00:30
  • Just as pdf, the svg format also doesn't work. – cvgmt Jun 19 '22 at 00:35
  • cleanContourPlot[] from https://mathematica.stackexchange.com/questions/3190/saner-alternative-to-contourplot-fill/3279#3279 will fix the contour plots. – Michael E2 Jun 19 '22 at 00:39
  • ah, should have checked before I worked on this However, I couldn't actually get cleanContourPlot to work in this case, even with plot /. g_Graphics :> cleanContourPlot[g]. I did however find my own solution using RegionUnion which fits in a comment: Export["Fig_try_fixed.svg", plot /. GraphicsComplex[coords_, g___] :> GraphicsComplex[coords, Quiet[g /. GraphicsGroup[l : {__Polygon}] :> (RegionUnion[MeshRegion[coords, #] & /@ l]), MeshRegion::dgcellr]]]. It could probably be sped up, but isn't too slow. Should I still post it as a solution...? I worry that's bad form, but it is new. – thorimur Jun 19 '22 at 01:22
  • @thorimur Why not? Look forward to your answer. – xiaohuamao Jun 19 '22 at 01:26
  • @MichaelE2 Thanks a lot for the links! cleanContourPlot[] seems good. But I don't see how to put the Thickness option aside in my case after some tries... Any hint? – xiaohuamao Jun 19 '22 at 01:40
  • Hmm, the arrow seems to need some attention, as it doesn't fit naturally into the solutions given at the link—my solution would be to split it up into an unthickened arrow plus a thickened line. I don't have time to post both the above and that as a solution now, but I'll do so later if it works and if still relevant. – thorimur Jun 19 '22 at 01:43

1 Answers1

1

Following the chat above, here I post a workaround. The boundary glitches are solved by cleanContourPlot, as suggested by @Michael E2. The arrowhead is a bit trickier. But as suggested by @thorimur (who probably will not post an answer), splitting the command of arrow and line and making the arrow as short as an arrowhead can work, which means we replace the real long Arrow with a long Line and a very short Arrow. This arrow issue seems to be a bug to me.

inten = (x y (z^3 + 0.2))/(x^2 + y^2 + z^2 + 0.01);
max = 1; zlst = Range[2, -2, -4/(5 - 1)];
frameticks = {-0.5, 0.5};

plot0 = Table[ cleanContourPlot@ ContourPlot[ inten /. z -> zlst[[-i]], {x, -max, max}, {y, -max, max}, FrameTicks -> {{frameticks, None}, {frameticks, None}}, ImagePadding -> {{Automatic, Automatic}, {23, 36}}, ImageSize -> 160], {i, Length@zlst}]; plot = GraphicsRow[plot0, Epilog -> {Gray, Arrowheads[0.015], Arrow[{Scaled[{0.93, 0.96}], Scaled[{0.94, 0.96}]}], Gray, Thickness[0.01/3], Line[{Scaled[{0, 0.96}], Scaled[{0.93, 0.96}]}]}, ImageSize -> 1000]

Export["Fig_try.pdf", plot]

xiaohuamao
  • 4,728
  • 18
  • 36