MATLAB Answers

John Ho
0

R2014b tesselates contour plots when saving to eps

Asked by John Ho
on 6 Jan 2015
Latest activity Answered by Moises Jezzini on 13 Jan 2017
When saving a contour plot to a vector format, the eps vector plot is "tesselated", i.e. it has dense triangular divisions along the fill of the contour.
This problem can be reproduced by
Z=peaks(50);
contourf(Z);
print(gcf,'-depsc','-painters','contours.eps');
Most viewers will show a white grid cut into many triangles on the figure. The file size is also very large (i.e. 532 KB vs 27 KB on previous versions). This makes a vector export of filled regions nearly useless in R2014b.
Running the same commands on previous versions results in a figure without tesselation. My operating system is OSX Yosemite 10.10.1.

  1 Comment

In my work I rely heavily on contour map plots. Since version 2014b I cannot create publication quality vector figures anymore. The filled contours will have visible dividing lines on screen and on paper. So I use MATLAB 2014a, as it is the last version of MATLAB that works for me. What a pity!

Sign in to comment.

5 Answers

Answer by Moises Jezzini on 13 Jan 2017

As today, the problem persist. This is one more workaround option.
The idea is to save all vectorial elements in an svg, and the colormap in a bitmap (in this case png). Then use Inkscape to fit the svg with the png.
Z = peaks(20);
hold on;
[~, hcf] = contourf(Z, 100, 'LineColor', 'none');
shading interp;
[~, hc] = contour(Z, 'ShowText','on', 'LineColor', 'black');
title('An example');
hold off;
set(hcf, 'Fill', 'off'); % Removes the contourf
print('Vector', '-dsvg'); % Saves the svg
set(hcf, 'Fill', 'on'); % Shows again the coutourf
set(hc, 'Visible', 'off'); % Hides the coutour lines
% When turning off the axis, the dimensions of the plot will change
% We will save the dimensions to perserve them
OriginalPosition = get(gca, 'Position'); % Save dimensions
axis off;
% Next lines to remove title
h_ch = get(gcf,'Children');
h_str = get(h_ch, 'Title');
set(h_str, 'String',''); % Remove title
set(gca, 'Position', OriginalPosition); % Now reset the plot dimensions
print('Bitmap', '-dpng');
Now, you need to open the svg file in Inkscape (free and open source). Select all (Ctrl-A), ungroup (Ctrl-U) and remove the backgrounds. Then import the png (File->Import), send it to the back (End), then you will need to fit the sizes and align. In this way the background is a bitmap and the numbers, axis and lines are vectors.

  0 Comments

Sign in to comment.


Answer by Dene Farrell on 21 Dec 2016
Edited by Dene Farrell on 22 Dec 2016

Previously Answered Here.
[Basically it says: Another work around is to use illustrator programatically fix_matlab_vector_graphics]
Mathworks have been answering these inquiries with excuses that it's an 'antialiasing algorithm' bug in the viewer and that the new export is better because it's simpler to break up these vector graphics into subcomponents. I'm really confused as to how they can convince themselves that this is true and how they convince others.
It's not acceptable for paid, proprietary software to make huge mistakes like this and then falsely blame it on an anti aliasing algorithm. Mathworks, you need to treat your paying customers with more respect! We are almost 6 versions past the original problem and they still haven't fixed it (2014b - 2017a).

  2 Comments

Jan Simon does not work for Mathworks.
Okay, thanks, I updated the post.

Sign in to comment.


Answer by Mike Garrity
on 6 Jan 2015

We've been discussing that change in the comments section of this question .
It was done to avoid problems we were seeing with downstream apps choking on very complex polygons. We've probably dialed it too far in that direction. We'll probably move it back towards less subdivision than R2014b, but not as far as earlier releases.

  4 Comments

Show 1 older comment
I don't think that there's a simple way to put Humpty-Dumpty back together once he's been subdivided.
And yes, Illustrator is one of the better apps at handling complex polys. But the easiest way to encounter the buggy cases with contourf is to start using XData & YData to define grids which have self intersections, like this example .
I really hope you can bring in an option to get rid of the subdivisions, because the way it is now creates a problem for people using software such as Illustrator. I am seeing a 25-fold increase in my .eps file sizes, to the point that they are completely intractable in Illustrator.
Dividing a vector patch into a discrete number of uniform triangles completely defeats the point of saving a vector image, as it is now essentially a (very poor and inefficient) bitmap.
I agree with Ben, the subdivisions make the vector image very difficult to process with editing tools such as Illustrator. There are a number of other annoyances introduced in R2104b that I do hope gets fixed. For example, axis tick labels are now (somewhat arbitrarily) grouped together in the same text box. If I change the font size on the labels, they now lose their positions relative to the markers. These details are crucial for the many who use MATLAB to produce scientific figures.

Sign in to comment.


Answer by Michael S on 21 May 2015
Edited by Michael S on 21 May 2015

One (slightly complicated) workaround:
  • use contour (not contourf)
  • save as eps / pdf (the latter is even better for me since the objects are grouped better than in the eps case - at least in Illustrator)
  • now you have your contour-lines as hundreds of small line segments (i guess also one effect of the mentioned tesselation problem, in older Matlab versions this worked better)
  • but you can select all the lines from one contour and join them. Now you can nicely change their appearance and also fill them to get something like a contourf plot (of course this is very complicated if you have a complex contour-landscape ... but in my case it was feasible)
I discovered that these contour lines have various export appearances for different OS and Matlab/Illustrator versions. I use pdf export and have R2014b + Illustrator CS5 on Windows7. On our Mac with R2012b we use eps export.

  2 Comments

Good suggestion!
I think that you'll find that it joins the line segments if you set the contour's LineStyle to something other than solid. The reason is that there's a performance cost to joining them, we skip that when we don't think it's going to matter.
So try creating your contour with a different LineStyle, then after you export it, set it back to solid in Illustrator.
Nice workaround, thanks for sharing!
It would be difficult for my case because my figure has a very large number of contours. Here's an example:
Prior to this change, contourf() produces much nicer looking images than plotting individual pixels with image(), especially if there are not too many sample points.

Sign in to comment.


Answer by John Ho
on 22 Dec 2016
Edited by John Ho
on 22 Dec 2016

Thank you @Dene Farrell for revisiting this issue.
The previous answer by @Mike Garrity was accepted based on the implied promise that the issue would be fixed in future versions of MATLAB. Almost two years and 6 versions later, it has not. Meanwhile, researchers and engineers that rely on MATLAB to generate publication-quality vector graphics have had to rely on workarounds. The fix could be as simple as putting an option in the export that reverts to the previous function - I have had no problems with the eps export function in the past, and the decision to split into subpolygons is damaging and mystifying. The vector graphics support has become significantly worse after R2014b for almost every common eps task (circle markers are practically hexagonal!?!) and it is unacceptable that no effort has been made to fix this.

  0 Comments

Sign in to comment.