Triangular split spots with painters renderer in MATLAB 2014b and higher

The new MATLABs graphics engine, HG2, doesn’t allow patches to be printed correctly by rendering artists:

hist(randn(1,1000)); colorbar('Location','SouthOutside'); print('test.pdf','-dpdf'); 

The resulting patches created using hist or colorbar have triangular splits:

Triangular splits in patches

The issue was discussed at MATLAB Central here and here , where it was proposed to disable the option "smooth linear art" in the pdf-viewer. This hides the problem for some readers (for example, in Adobe Reader, but not in Apple Preview), but this solution is unlikely to ask co-authors and readers to use a specific pdf viewer with non-standard settings for the graphics to display correctly. If you look at the resulting file in Inkscape, it is clear that the split is present in the output vector graphics. Here I moved one half of the color panel, proving that it is actually split in half, and not just misinterpreted by the pdf viewer:

enter image description here

The problem does not arise with the OpenGL rendering tool ( print('test.pdf','-opengl') , but then the output is not vectorized). The problem persists in MATLAB 2015a.

Is there a way to export vector art without artifacts in MATLAB 2014b or later?

+6
source share
3 answers

Doubtful work arises here until the real problem is solved:

Diagonal lines are just the empty space between the triangles, so what we see is a space behind the patches. Silly idea: Let’s fill this space with appropriate colors instead of white.

To do this, we copy all the objects and shift them with just a small bit.

Code:

 hist(randn(1,1000)); colorbar('Location','SouthOutside'); print('test.pdf','-dpdf'); %// print original for comparison f1 = gcf; g = get(f1,'children'); n = length(g); copyobj(g,f1); %// copy all figure children 

The copied objects are now the first n elements in the 2*n f1.Children . They are definitely on top of old objects.

 g=get(f1,'children'); for i=1:n; if strcmpi(g(i).Type,'axes'); set(g(i),'color','none','position',g(i).Position+[0.0001 0 0 0]); set(g(i+n),'position',g(i+n).Position); %// important! end; end; print('test2.pdf','-dpdf'); 

Explanation:

g = get(f1,'children'); gets all axes, color panels, etc. within the current drawing.

colorbar objects are bound to axes, so we only need to move the axes children.

Setting color to none makes the background of the new axes transparent (since they are on top of the old ones).

g(i).Position+[0.0001 0 0 0] shifts the new axes by 0.0001 normalized units to the right.

set(g(i+n),'position',g(i+n).Position); This line seems unnecessary, but the last image below shows what happens when you print if you do not turn it on.

Depending on the types of graphic objects you have built, you may need to customize it to suit your needs, but this code should work if you only have colorbar and axes objects.

Original:

original

Using hack:

enter image description here

Without the string %// important! :

enter image description here

+1
source

In R2015b, the histogram did not seem to show white lines, but fill .

For simple graphs, just insert the data again:

 x = 0:pi/100:pi; y = sin(x); f = fill(x,y,'r'); hold on; f2 = fill(x,y,'r'); %// Worked like magic 

If the magic doesn't work, try answering Jeff's question: f2 = fill(x+0.0001,y,'r');

0
source

Depending on which version of Matlab you are using, you may try using epsclean . It does not seem to work with the latest versions of Matlab like 2017a.

Otherwise, epsclean can be run in an existing eps file (not pdf) exported with the -painters option to create a vectorized shape, and it will overwrite (or create another file) with the white lines removed.

0
source

Source: https://habr.com/ru/post/989218/


All Articles