9

Patches in Matlab are glued together from individual triangles. Their edges are normally not visible, but when I export the figure in a vector graphics format they can clearly be seen (not the whole picture is shown, just a zoomed-in portion)

edges of individual triangles

Code to generate this MWE was:

xx = [0:1:100, 100:-1:0];
yy = [zeros(1,101), ones(1,101)];
p1 = patch(xx,yy,'b'); 
print('testPatch','-dpdf','-painters')

Alternative function fill behaves the same. Is there a way to avoid this bug or do I have to live with bitmaps in this case?

EDIT: One workaround is to hatch the area instead of filling it. That obviously isn't always possible, but in my case it worked nicely and I could stick to vector graphics. The relevant FEX submission is http://www.mathworks.com/matlabcentral/fileexchange/30733-hatchfill

Trisoloriansunscreen
  • 1,543
  • 1
  • 15
  • 27
  • I'm not sure what causes this, but I have observed similar behavior when exporting plots with circular markers. The circular markers end up looking like patchy polygons made up of several triangles. – Ryan J. Smith Apr 23 '15 at 14:12
  • 1
    I think that the problem you describe has a simple solution: don't use 'o' as marker type, but rather '.'. That did the trick for me as far as i can remember. – FliegenderZirkus Apr 23 '15 at 14:17
  • 2
    I will never get tired of reccomending `export_fig` from Matlab FEX. It does an amazing job exporting figures, way better than Maltab itself. try it out. – Ander Biguri Apr 23 '15 at 15:03
  • 2
    Ander, thanks for the tip. I also use export_fig on a regular basis, but it does not have any effect on this issue. The problem is with the 'painters' renderer, which is used by export_fig to generate vector graphics also. – FliegenderZirkus Apr 23 '15 at 15:11
  • 2
    @AnderBiguri: Unfortunately, `export_fig test.pdf -painters` produces the same, patchy output. I've had my fair share of grief with Matlab's vector output, and I gave up on trying to convince Matlab to make beautiful figures. Instead, I save as eps, and then edit it in Illustrator or Inkscape to fix the fonts, linewidths, ... – Martin J.H. Apr 23 '15 at 15:35
  • @MartinJ.H. I usually code fonts linewidths,... with Matlab code, or else open the figure editor ones ploted. In my case, generally it does the trick – Ander Biguri Apr 23 '15 at 15:38
  • Very odd- the example works for me with Matlab 2013b; but with the new plotting engine in 2014b, (even with options for disabling the new graphics) it seems something was broken – chepyle Apr 24 '15 at 00:37
  • Before 2014b, Matlab exported each patch as a single polygon. Now it splits each patch into multiple triangles. This policy is discussed here http://www.mathworks.com/matlabcentral/answers/162257-problem-with-patch-graphics-in-2014b-splits-in-two-along-diagonal but Mathworks' employees blame Adobe instead of admitting they broke the vector graphics export. – Trisoloriansunscreen Jun 16 '15 at 12:10
  • @chepyle - what options are these...? I'm curious... – Dev-iL Nov 28 '15 at 22:36
  • @Dev-iL I've needed to use the figure properties `GraphicsSmoothing` and `Renderer` , setting them to `'off'` and `'painters'` respectively – chepyle Nov 30 '15 at 01:07

3 Answers3

0

plot2svg allows creating vector graphics files (SVGs) with unbroken patches. I'd stay tuned also for export_fig updates, it seems that Yair Altman is working on this issue. Wouldn't hold my breath for a solution from The Mathworks.

Trisoloriansunscreen
  • 1,543
  • 1
  • 15
  • 27
0

Scattered vector graphics output (slices, triangles, rectangles instead of joint objects) is one of the secret feature after R2014b graphics engine update.

I was able to join those triangles together with following python script thatuses system call of Inkscape https://github.com/Sbte/fix_matlab_eps. But that is workaround, that works for simple plots. This is not solution.

Either use raster output, or save your data and use python matplot software.

0

This is an annoying issue. The only answer I have seen from Matlab is that it has to do with antialiasing in your PDF viewer. It is discussed extensively here: https://www.mathworks.com/matlabcentral/answers/162257-problem-with-patch-graphics-in-2014b-splits-in-two-along-diagonal

Mathworks recommends that we change our PDF viewer options as described here: https://www.stata.com/support/faqs/graphics/line-artifacts-in-filled-contour-plot/ In case the link breaks, here is the key information: Here are instructions for turning off anti-aliasing on some popular PostScript and PDF viewers:

  • GSView: From the Media > Display Settings menu, change "Graphics Alpha" to 1 Bit.
  • Adobe Reader 9 and 10: From the Adobe Reader > Preferences... menu, select the “Page Display” pane, uncheck “Use 2D graphics acceleration”, and uncheck “Smooth line art”.
  • Adobe Illustrator: From the Illustrator > Preferences... > General menu, unselect “Anti-Aliased Artwork”.
  • Mac Preview: From the Preview > Preferences... menu, select the “PDF” pane and uncheck “Smooth text and line art”.
  • FoxIt Reader: File>Preferences>Page Display, uncheck "smooth line art"

This works for me for most purposes, but, as one person on the Mathworks discussion said, we cannot expect our colleagues to change their PDF viewer settings to view our plots.

This seems like a viable workaround (using a script with Adobe Illustrator or Inkscape+python to join the patches): https://github.com/dfarrel1/fix_matlab_vector_graphics

Tom F
  • 111
  • 1
  • 8