在MATLAB 2014b及更高版本中具有Painter渲染器的三角形分割面片 [英] Triangular split patches with painters renderer in MATLAB 2014b and above

查看:136
本文介绍了在MATLAB 2014b及更高版本中具有Painter渲染器的三角形分割面片的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

MATLAB的新图形引擎HG2无法使用Painter渲染器正确打印补丁:

MATLABs new graphics engine, HG2, fails to properly print patches using the painters renderer:

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

生成的补丁(由histcolorbar生成)具有三角形裂口:

The resulting patches, whether generated by hist or colorbar, have triangular splits:

已在MATLAB Central ,建议在pdf-viewer中禁用平滑线条艺术"选项即可解决此问题.这在某些阅读器中(例如在Adobe Reader中而不是在Apple Preview中)隐藏了该问题,但是要合作者和阅读器使用具有非默认设置的特定pdf-viewer来正确显示图形几乎不是一种解决方案.在Inkscape中查看生成的文件,很明显,拆分出现在输出矢量图形中.在这里,我移动了颜色条的一半,证明它实际上被分成了两半,而不仅仅是pdf查看器对它的误解:

The issue has been discussed on MATLAB Central here and here, where it was suggested that disabling the "smooth line art" option in the pdf-viewer should solve it. This conceals the problem in some readers (e.g. in Adobe Reader but not in Apple Preview), but it is hardly a solution to ask collaborators and readers to use a specific pdf-viewer with non-default settings for graphics to appear correctly. Looking 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 colorbar, proving that it is in fact split in half, and not just misinterpreted by the pdf-viewer:

使用OpenGL渲染器(print('test.pdf','-opengl'),但不对输出进行矢量化)时不存在此问题.该问题在MATLAB 2015a中仍然存在.

The problem is not present using the OpenGL renderer (print('test.pdf','-opengl'), but then the output is not vectorized). The problem persists in MATLAB 2015a.

在MATLAB 2014b或更高版本中是否可以导出无伪影的矢量图形?

Is there a way to export artifact-free vector graphics in MATLAB 2014b or later?

推荐答案

在解决实际问题之前,这里是一个有问题的解决方法:

Here's a questionable work-around until the actual problem is solved:

对角线只是三角形之间的空白区域,因此我们看到的是窥视补丁后面的空白区域. 愚蠢的想法: 让我们用匹配的颜色而不是白色填充该空间.

The diagonal lines are simply the empty space between the triangles, so what we are seeing is the white space behind the patches peek through. Silly idea: Let's fill that space with matching colors instead of white.

为此,我们将复制所有对象并将新对象偏移一点点.

To do so, we'll copy all objects and offset the new ones by just a tiiiiny bit.

代码:

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

复制的对象现在是2*n f1.Children数组中的第一个n元素.它们恰好在旧对象的顶部.

The copied objects are now the first n elements in the 2*n f1.Children array. They are exactly on top of the 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');

说明:

g = get(f1,'children');获取当前图形中的所有轴,颜色条等.

g = get(f1,'children'); gets all axes, colorbars, etc., within the current figure.

colorbar对象链接到轴,这就是为什么我们只需要移动axes类型的子代的原因.

colorbar objects are linked to an axes, which is why we'll only have to move the axes type children.

color设置为none可使新轴的背景透明(因为它们位于旧轴的顶部).

Setting the 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]将新轴向右移动0.0001归一化单位.

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);该行似乎是不必要的,但是下面的最后一个图像显示了如果不包含它,在打印时会发生什么.

set(g(i+n),'position',g(i+n).Position); This line seems unnecessary, but the last image below shows what happens when printing if you don't include it.

根据您绘制的图形对象的类型,您可能需要对其进行调整以满足自己的需要,但是如果您只有颜色条和轴对象,则此代码应该可以使用.

Depending on the types of graphics objects you have plotted, you may need to tweak this to fit your own needs, but this code should work if you have only colorbar and axes objects.

原文:

受到黑客攻击:

没有%// important!行:

Without %// important! line:

这篇关于在MATLAB 2014b及更高版本中具有Painter渲染器的三角形分割面片的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆